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

后端 未结 3 875
深忆病人
深忆病人 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:43

    A better way:

    Set the legendgroup option to the legend label you want for each trace. This will allow you to filter everything in the same group.

    Hide the remaining traces' legends using the showlegend=False option.

    This will give the exact behaviour you want.

    Old Solution (Not recommended):

    There is a another solution by adding "dummy" traces and hiding the data but only showing the legend. With this method you cannot slice any of the data (which is not a bad thing).

    trace_dummy = Scatter(
        x=[0, 0, 0], # Data is irrelevant since it won't be shown
        y=[0, 0, 0],
        name='Whatever Trace',
        showlegend=True,
        visible="legendonly"
    )
    

提交回复
热议问题