Python Plotly:如何更改跟踪顺序,或在Plotly中切换轴的侧面?
我正试图让线显示在酒吧。似乎任何具有Python Plotly:如何更改跟踪顺序,或在Plotly中切换轴的侧面?,python,pandas,plotly,Python,Pandas,Plotly,我正试图让线显示在酒吧。似乎任何具有secondary\u y=True的跟踪都将绘制在secondary\u y=False的跟踪之上 这很好,但是对于这个特定的数据集,条轴应该在右边,否则这个图会让人困惑。线是在1-3范围内的线,条是在0-35k范围内的线 换言之,它应该看起来像这样,但y轴已切换。是否有某种方法可以切换轴,或者控制绘制轨迹的顺序,以便我可以强制线条位于条的顶部 如果没有数据集样本,提供完整的解决方案并不容易,但我仍然认为我已经找到了答案。我现在有点赶时间,所以我会简短地说
secondary\u y=True
的跟踪都将绘制在secondary\u y=False
的跟踪之上
这很好,但是对于这个特定的数据集,条轴应该在右边,否则这个图会让人困惑。线是在1-3范围内的线,条是在0-35k范围内的线
换言之,它应该看起来像这样,但y轴已切换。是否有某种方法可以切换轴,或者控制绘制轨迹的顺序,以便我可以强制线条位于条的顶部
如果没有数据集样本,提供完整的解决方案并不容易,但我仍然认为我已经找到了答案。我现在有点赶时间,所以我会简短地说: 条是大数字,线是小数字。开箱即用的
fig=make_子图(specs=[[[{“secondary_y”:True}]])
将提供以下功能:
顶部的行跟踪=良好。左侧的条形图编号=错误。
改变yoy将不同轨迹应用于图形的顺序不会有帮助。但您可以自由指定希望主y轴和次y轴显示在绘图的哪一侧,如下所示:
fig.update_layout(dict(yaxis2={'anchor': 'x', 'overlaying': 'y', 'side': 'left'},
yaxis={'anchor': 'x', 'domain': [0.0, 1.0], 'side':'right'}))
将其添加到混合物中,您将得到:
顶部的行跟踪=良好。右边的条形图编号=良好。
使用数据示例完成代码:
# imports
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots
# set figure twith multiple y axes
fig = make_subplots(specs=[[{"secondary_y": True}]])
# blue line with numbers from 1 to 3
fig.add_trace(
go.Scatter(x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1.0, 1.3, 2.7, 1.8, 2.9]),secondary_y=True)
# red bars with big numbers
fig.add_trace(
go.Bar(x=[0, 1, 2, 3, 4, 5],
y=[np.nan, np.nan, np.nan, 100000, 20000, 250000]))
# update layout to put axes and values in the desired positions
fig.update_layout(dict(yaxis2={'anchor': 'x', 'overlaying': 'y', 'side': 'left'},
yaxis={'anchor': 'x', 'domain': [0.0, 1.0], 'side':'right'}))
fig.show()
下次有空我会试试的。我想这应该行。@vestland有可能关闭y1轴网格吗?我应用了这个解决方案,它适用于跟踪。但是,我希望网格现在只显示y2轴,而不是y1轴。@EliTurasky这可以通过
fig.update\u布局(yaxis1=dict(showgrid=False))
实现。可选fig.update\u布局(yaxis2=dict(showgrid=False))
关于您想显示还是不想显示。我正在做yaxis1\u showgrid。命令是yaxis_showgrid。谢谢
# imports
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots
# set figure twith multiple y axes
fig = make_subplots(specs=[[{"secondary_y": True}]])
# blue line with numbers from 1 to 3
fig.add_trace(
go.Scatter(x=[0, 1, 2, 3, 4, 5],
y=[1.5, 1.0, 1.3, 2.7, 1.8, 2.9]),secondary_y=True)
# red bars with big numbers
fig.add_trace(
go.Bar(x=[0, 1, 2, 3, 4, 5],
y=[np.nan, np.nan, np.nan, 100000, 20000, 250000]))
# update layout to put axes and values in the desired positions
fig.update_layout(dict(yaxis2={'anchor': 'x', 'overlaying': 'y', 'side': 'left'},
yaxis={'anchor': 'x', 'domain': [0.0, 1.0], 'side':'right'}))
fig.show()