Skip to content

Legendgroup visibility not consistent with shape #6929

Description

@mattviguier

When adding a shape to a legendgroup with showlegend=False, he does not toggle the visibility when clicking in the legend which is inconsistent with the expected behaviour of traces.

Here is the code to reproduce the graph.
Plotly version: 5.17.0

import plotly.graph_objects as go
from plotly import data
from plotly.subplots import make_subplots

df = data.gapminder()

fig = make_subplots(
    rows=2,
    cols=1,
)

filtered_data = df.loc[(df.country.isin(["Germany"]))]
fig.add_trace(
    go.Scatter(
        x=filtered_data.year,
        y=filtered_data.lifeExp,
        name="Germany",
        legend="legend",
        legendgroup="EU",
    ),
    row=1,
    col=1,
)
filtered_data = df.loc[(df.country.isin(["United Kingdom"]))]
fig.add_trace(
    go.Scatter(
        x=filtered_data.year,
        y=filtered_data.lifeExp,
        name="United Kingdom",
        legendgroup="not EU",
    ),
    row=1,
    col=1,
)
filtered_data = df.loc[(df.country.isin(["France"]))]
fig.add_trace(
    go.Scatter(
        x=filtered_data.year,
        y=filtered_data.lifeExp,
        name="France",
        legendgroup="EU",
        showlegend=False,
    ),
    row=2,
    col=1,
)

fig.add_hline(
    y=72,
    line_dash="dot",
    label=dict(
        text=f"vline1",
        textposition="end",
    ),
    name="vline",
    legendgroup="vline_group",
    showlegend=True,
    row=1,
    col=1,
)
fig.add_hline(
    y=72,
    line_dash="dot",
    label=dict(
        text=f"vline2",
        textposition="end",
    ),
    name="vline2",
    legendgroup="vline_group",
    showlegend=False,
    row=2,
    col=1,
)

fig.show()

Full graph:
graph_visible
Trace legend group toggle:
graph_trace_hidden
Shape legend group toggle:
graph_vline_hidden

Activity

  1. mboll commented on Mar 12, 2024

    @mboll

    +1 for this issue. Also in 5.19.

    I added another example below. The expected behavior works for the traces group; you click it and both traces turn off even though only one trace is actually in the legend. For the shapes, if you click the group, only the first shape turns invisible.

    import plotly.graph_objects as go
    
    # Make example traces.
    traces = []
    for i in range(2):
        trace = go.Scatter(
            x=[i, i + 1],
            y=[i, i + 1],
            name=f'Traces Group',
            legendgroup='traces_group',
            showlegend=True if i == 0 else False
        )
        traces.append(trace)
    
    # Make example shapes.
    shapes = []
    for i in range(2):
        shape = dict(
            type='rect',
            x0=i,
            x1=i+1,
            name='Shapes Group',
            showlegend=True if i == 0 else False,
        )
        shapes.append(shape)
    
    # Make and show figure.
    fig = go.Figure(
        traces,
        layout=dict(shapes=shapes),
    )
    fig.show()
    

    Base figure:
    image

    Toggle trace group:
    image

    Toggle shape group:
    image

  2. Coding-with-Adam commented on Mar 14, 2024

    @Coding-with-Adam
    Contributor

    Indeed, this looks like a bug. Thank you for reporting, @mattviguier and @mboll.
    @archmoj here's is the codepen. Let me know if we should transfer this to Plotly.js

  3. archmoj commented on Mar 14, 2024

    @archmoj
    Contributor

    Yes please transfer it to plotly.js.
    Thank you! @Coding-with-Adam

  4. self-assigned this
    on Jul 12, 2024
  5. removed their assignment
    on Aug 2, 2024
  6. added
    bugsomething broken
    P2considered for next cycle
    on Aug 9, 2024
  7. dmyronuk commented on Feb 13, 2025

    @dmyronuk
    Contributor

    duplicate of #6771

  8. emilykl commented on Aug 28, 2026

    @emilykl
    Contributor

    Fixed by #7813. Shape toggling should work as expected in the latest Plotly versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2considered for next cyclebugsomething brokensev-2serious problem

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions