【发布时间】:2021-01-16 20:32:02
【问题描述】:
我正在使用 plotly.subplots 生成 2 个 plotly.express 元素的子图。结果打印在下面。我想获得 2 个不同的图例,每个图都有相关的元素。
这是用于生成图的代码:
fig1 =px.line(df, x=df.index, y='average')
fig1.add_scatter(x=df.index, y=df['ave_10'],mode='lines', name='MA 10',opacity=0.6)
fig1.add_scatter(x=df.index, y=df['ave_50'],mode='lines', name='MA 50',opacity=0.6)
# plot volume
fig2= px.line(df, x=df.index, y='Volume')
# combine the 2 graphs
fig = make_subplots(rows=2,cols=1,subplot_titles = ['Average values of {} with moving average at 10 and 50 lags'.format(code),'Volume'])
for data in fig1.data:
fig.add_trace((go.Scatter(x=data['x'],y=data['y'], name = data['name'])),row=1,col=1)
for data in fig2.data:
fig.add_trace((go.Scatter(x=data['x'],y=data['y'], name = data['name'])),row=2,col=1)
fig.show()
【问题讨论】: