如何在 Plotly 中旋转绘图的轴? [英] How do you rotate the axis of a plot in Plotly?
问题描述
我在 Plotly 中完成了以下绘图
I have the following plot done in Plotly
如您所见,X、Y 轴采用传统方式.如何旋转轴,以便垂直绘制 X,水平绘制 Y(向左)?
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)
为了使其可重现,我将代码修改为
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()
情节是
推荐答案
我不知道 Plotly 有任何内置方法来切换 x 轴和 y 轴.但是,您可以自己实现这一点,只需切换 x 轴和 y 轴的标题,然后切换与您的点相对应的参数 x
和 y
.
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.
然后您可以将 y 轴(包含您的 x 坐标)放在布局的右侧,并反转 x 轴(包含您的 y 坐标)的范围,使原点位于下方-右侧.
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()
这篇关于如何在 Plotly 中旋转绘图的轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!