plotly / plotly.js

Open-source JavaScript charting library behind Plotly and Dash
https://plotly.com/javascript/
MIT License
16.85k stars 1.85k forks source link

Legendgroup visibility not consistent with shape #6929

Open mattviguier opened 10 months ago

mattviguier commented 10 months ago

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 commented 5 months ago

+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 commented 5 months ago

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 commented 5 months ago

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