How to avoid duplicate legend labels in plotly or pass custom legend labels

后端 未结 3 876
深忆病人
深忆病人 2021-01-11 15:32

How can I avoid duplicate legend labels in subplots? One way I would go about it in matplotlib would be to pass custom legend labels to an legend object. I couldn\'t find an

3条回答
  •  一整个雨季
    2021-01-11 15:48

    Plotly controls this on the trace level. Try passing in showlegend=False inside the Histogram traces that you don't want to appear in the legend.

    Reference: https://plot.ly/python/reference/#Histogram-showlegend

    Example: https://plot.ly/python/legend/#Hiding-Legend-Entries

    Direct copy-paste from the link above.

    import plotly.plotly as py
    from plotly.graph_objs import *
    # Fill in with your personal username and API key
    # or, use this public demo account
    py.sign_in('Python-Demo-Account', 'gwt101uhh0')
    
    trace1 = Scatter(
        x=[0, 1, 2],
        y=[1, 2, 3],
        name='First Trace',
        showlegend=False
    )
    trace2 = Scatter(
        x=[0, 1, 2, 3],
        y=[8, 4, 2, 0],
        name='Second Trace',
        showlegend=True
    )
    data = Data([trace1, trace2])
    plot_url = py.plot(data, filename='show-legend')
    

    The usage you want to see is shown in trace1 above.

提交回复
热议问题