Python Plotly - 多个 YAXIS 图例布局

问题描述

我正在尝试使用以下代码在同一个图形、一个条形图和一个线条趋势上构建 2 个重叠图:

data = []

for __brand in df_cumsum['brand'].unique():

    data.append(go.Scatter(
        x=df_cumsum.loc[df_cumsum['brand'] == __brand,:]['day'],y=df_cumsum.loc[df_cumsum['brand'] == __brand,:]['ecpm_cum'],name=__brand,yaxis='y1'
    ))

    data.append(go.Bar(
        x=df_cumsum.loc[df_cumsum['brand'] == __brand,:]['impressions_cum'],yaxis='y2',opacity=0.3
    ))

figure = {'data': data,'layout': {'title': 'Delivery Performance Over Test Period (Days)','yaxis': {'title': 'Cumulative eCPM (USD)'},'yaxis2': {'title': 'Cumulative Delivery (Impressions)','side': 'right','overlaying': 'y'}}}

enter image description here

因此,我得到了类似上面的内容,其中左侧 yaxis 和右侧 yaxis 的图例混合在一起。

有谁知道我怎样才能把每一个都放在相关的一边?左侧左侧 yaxis 的图例和右侧右侧 yaxis 的图例?提前致谢!

解决方法

不幸的是,Plotly doesn't support multiple legends。这是一个悬而未决的问题。

您可以从现有的一个图例中隐藏左侧图例项,然后使用带有线趋势变量名称文本的矩形注释框从头开始构建左侧图例。但是,在这种情况下,图例实际上没有任何切换功能,只会用作显示变量名称的框。