How do you rotate the axis of a plot in Plotly?

允我心安 提交于 2020-08-26 04:53:47

问题


I have the following plot done in Plotly

As you can see the X,Y axis are in the traditional way. How can I rotate the axis so that X is plot vertically and Y horizontally (to the left)?

(also I would like to modify the reach and separations of each axis)

I suppose it has to do something with the layout element.

My code if so far

layout= go.Layout(title=go.layout.Title(text=title,x=0.5),
        xaxis={'title':'x[m]'},
        yaxis={'title':'y[m]'})

    point_plot=[
            go.Scatter(x=[series[listoflabels[0]]],y=[series[listoflabels[1]]],name="V0"),
            go.Scatter(x=[series[listoflabels[2]]],y=[series[listoflabels[3]]],name="GT"),
            go.Scatter(x=[0],y=[0],name="egoCar")
            ]

    return go.Figure(data=point_plot, layout=layout)

EDIT:

In order to make it reproducible I modified the code to

layout1= go.Layout(title=go.layout.Title(text="A graph",x=0.5),
        xaxis={'title':'x[m]'},
        yaxis={'title':'y[m]'})

point_plot=[
            go.Scatter(x=[3],y=[1],name="V0"),
            go.Scatter(x=[5],y=[2],name="GT"),
            go.Scatter(x=[0],y=[0],name="egoCar")
    ]
            
go.Figure(data=point_plot, layout=layout1).show()

and the plot is


回答1:


I am not aware that Plotly has any built-in method to switch x- and y-axes. However, you can achieve this yourself but switching the titles of the x- and y-axes, then switching the parameters x and y that correspond to your points.

Then you can place the y-axis (containing your x-coordinates) on the right side in your layout, and reverse the range of the x-axis (containing your y-coordinates) so that the origin is in the lower-right side.

import plotly.graph_objects as go

# switch the axes titles
layout1= go.Layout(title=go.layout.Title(text="A graph",x=0.5),
        xaxis={'title':'y[m]'},
        yaxis={'title':'x[m]', 'side':'right'})

# switch the x- and y-coordinates
point_plot=[
            go.Scatter(y=[3],x=[1],name="V0"),
            go.Scatter(y=[5],x=[2],name="GT"),
            go.Scatter(y=[0],x=[0],name="egoCar")
    ]


fig = go.Figure(data=point_plot, layout=layout1)

# reverse the range of the xaxis (which contains the y values)
fig.update_xaxes(autorange="reversed")
fig.show()




回答2:


Derek O hit on what is the easiest to implement. It's the graph analogy of 1km is 0.6 miles or a, b = b, a

A second option is to abstract the data and your axes. That's essentially saying the same thing but can be easier to read as Plotly has a unique ability to turn clear, well-structured Python into ugly JS without parenthesis pretty quickly.

Option three is to further abstract this and have it set your axes for you.



来源:https://stackoverflow.com/questions/63553744/how-do-you-rotate-the-axis-of-a-plot-in-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!