[英]Plotly - Using "secondary_y" attribute with a list of traces?
所以我有一些在列表data
中的traces
。 然后我將它們添加到我的圖中,如下所示:
data = [trace1,trace2]
fig = go.Figure(data=data,layout=layout)
但是,我想在我的一條軌跡上添加一個輔助軸,我找到了這個方法:
fig.add_trace( trace, secondary_y=True)
fig.update_yaxes(title_text='Second Y-axis',secondary_y=True)
但是,我將如何在跟蹤列表中執行此操作? 或者是不可能使用這個secondary_y
方法並且必須使用其他方法,例如明確地將軸y1,y2
添加到軌跡中?
您可以使用繪圖圖的update_traces
方法及其選擇器屬性來選擇特定的軌跡,如下例所示
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
traces = [go.Scatter(x=[1, 2], y=[3, 4], name='one'),
go.Bar(x=[1, 2], y=[10, 12], name='two')]
fig.add_traces(traces)
fig.update_traces(yaxis='y2', selector={'type':'bar'})
fig.show()
您還可以在 for 循環中使用您的跟蹤列表,您可以在其中為secondary_y
的值設置條件,這將更加符合情節習慣。
secondary_y
不是 plotly traces 的屬性,而是在某些函數中使用的便利屬性,因此您不能將其直接傳遞給 trace。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.