Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legendgroup visibility not consistent with shape #6929

Open
mattviguier opened this issue Oct 23, 2023 · 3 comments
Open

Legendgroup visibility not consistent with shape #6929

mattviguier opened this issue Oct 23, 2023 · 3 comments
Labels

Comments

@mattviguier
Copy link

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

@mboll
Copy link

mboll commented Mar 12, 2024

+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

@Coding-with-Adam
Copy link
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

@archmoj
Copy link
Contributor

archmoj commented Mar 14, 2024

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

@Coding-with-Adam Coding-with-Adam transferred this issue from plotly/plotly.py Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants