【问题标题】:Is there a way to associate for each plot the relative legend using plotly? [duplicate]有没有办法使用 plotly 为每个情节关联相对图例? [复制]
【发布时间】: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()

【问题讨论】:

    标签: python plotly subplot


    【解决方案1】:

    Plotly 仅支持每个图表一个图例。他们建议您改用注释。你可以用 matplotlib 做得更好。

    【讨论】:

    猜你喜欢
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2019-07-18
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多