Skip to content

cannot hide all shape entries in a group by its legend #4896

Open
@Honghe

Description

@Honghe

Envivonment:

  • OS: Windows 10
  • Plotly.py: 5.24.1
  • plotly.js: 2.35.2

Bug recording:

Video.mp4

Bug code:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_shape(
    type="line",
    xref="x",
    yref="y",
    x0=4,
    y0=0,
    x1=8,
    y1=1,
    line=dict(
        color="LightSeaGreen",
        width=3,
    ),
    legendgroup="group",  # this can be any string, not just "group"
    name="first legend group",
    showlegend=True,
)
fig.add_shape(
    type="line",
    xref="paper",
    yref="paper",
    x0=0,
    y0=0,
    x1=0.5,
    y1=0.5,
    line=dict(
        color="DarkOrange",
        width=3,
    ),
    legendgroup="group",  # this can be any string, not just "group"
    name="first legend group B",
    showlegend=False,
)

fig.show()

Reference:
https://plotly.com/python/legend/#grouped-legend-items Says: You can also hide entries in grouped legends, preserving the grouped show/hide behaviour. This is what Plotly Express does with its legends.

Excepted(The demo in the document):

Video.mp4

The demo Code in the document:

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[1, 2, 3],
    y=[2, 1, 3],
    legendgroup="group",  # this can be any string, not just "group"
    name="first legend group",
    mode="markers",
    marker=dict(color="Crimson", size=10)
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3],
    y=[2, 2, 2],
    legendgroup="group",
    name="first legend group - average",
    mode="lines",
    line=dict(color="Crimson"),
    showlegend=False,
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3],
    y=[4, 9, 2],
    legendgroup="group2",
    name="second legend group",
    mode="markers",
    marker=dict(color="MediumPurple", size=10)
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3],
    y=[5, 5, 5],
    legendgroup="group2",
    name="second legend group - average",
    mode="lines",
    line=dict(color="MediumPurple"),
    showlegend=False
))

fig.update_layout(title=dict(text="Try Clicking on the Legend Items!"))
fig.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething broken

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions