Skip to content

[BUG] Legend does not display when trace has no real data points #293

Open
@mhangaard

Description

@mhangaard

Describe the bug 🖍️
When plotting a trace with empty x and y, the legend does not display. Plotly has limited options for customizing the legend, so an empty trace is useful in combination with legendgroup to customize the legend.
newplot (31)

Reproducing the bug 🔍

import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly_resampler import register_plotly_resampler, unregister_plotly_resampler

# comment out this line to get native plotly behavior
register_plotly_resampler(mode="auto", default_n_shown_samples=100)

n = 10000
x = np.arange(n)
x_time = pd.date_range("2020-01-01", freq="1s", periods=len(x))
noisy_sine = (3 + np.sin(x / 2000) + np.random.randn(n) / 10) * x / (n / 4)

fig = go.Figure()
fig.add_trace(go.Scattergl(x=[None], y=[None], name="yp2", showlegend=True, legendgroup="yp2"))
fig.add_trace(go.Scattergl(y=noisy_sine + 2, name="yp2", showlegend=False, legendgroup="yp2"))
fig.add_trace(go.Scattergl(y=noisy_sine - 3, name="ym1"))
fig

Expected behavior 🔧
Native plotly behavior is that the legend of the empty trace appears (blue "yp2" on this screenshot). I can rely on the legend group to toggle the red trace by clicking the "yp2" legend.
newplot (32)

I would probably not be able to get the prefixing and suffixing, on the other trace in same legendgroup, but that's also ok.

Environment information:

  • OS: Ubuntu @ WSL2
  • Python environment:
    • Python version: 3.11
    • plotly-resampler environment:
      • Jupyter-lab 4.1.0
      • ipywidgets 8.1.1
  • plotly-resampler version: 0.9.2

Additional context
I have the same behavior in a Dash app.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions