predict-idlab / plotly-resampler

Visualize large time series data with plotly.py
https://predict-idlab.github.io/plotly-resampler/latest
MIT License
1.03k stars 68 forks source link

FigureResampler removes annotations #61

Closed prokie closed 2 years ago

prokie commented 2 years ago

Don't know if this is an enhancement or a bug. But it seems that calling FigureResampler removes the text labels from the plot. At least if the number of points exceeds default_n_shown_samples. Here is a simple example illustrating the issue in a dash application.

To see the labels you can uncomment the fig without the FigureResampler function.

I guess the wanted behavior if lttb removes the point with the label is to not show that label. That is also probably the easiest to implement.

import numpy as np
import plotly.graph_objects as go
import trace_updater
from dash import Dash, dcc, html
from plotly_resampler import FigureResampler

app = Dash(__name__)

# Create labels for the plot, but only on every 500th point
labels = [""] * 1_000_0
labels[500::501] = ["label" for x in labels[500::501]]

# Create the x and y values for the plot
x = np.arange(1_000_0)
noisy_sin = (3 + np.sin(x / 200) + np.random.randn(len(x)) / 10) * x / 1_000

fig = FigureResampler(go.Figure())
fig.add_trace(
    go.Scatter(
        x=x,
        y=noisy_sin,
        mode="lines+text",
        text=labels,
        textposition="top center",
    ),
)
fig.register_update_graph_callback(
    app=app, graph_id="graph-id", trace_updater_id="trace-updater"
)

# fig = go.Figure()
# fig.add_trace(
#     go.Scatter(
#         x=x,
#         y=noisy_sin,
#         mode="lines+text",
#         text=labels,
#         textposition="top center",
#     )
# )

app.layout = html.Div(
    [
        dcc.Graph(id="graph-id", figure=fig, mathjax=True),
        trace_updater.TraceUpdater(
            id="trace-updater",
            gdID="graph-id",
            sequentialUpdate=False,
        ),
    ],
)

if __name__ == "__main__":
    app.run_server(debug=True)
jvdd commented 2 years ago

Hi @prokie,

Thanks for pointing out the unexpected behavior & delivering reproducible code! :smile:

This behavior stemmed from plotly-resampler converting text argument internally into hovertext. I added in a1a535e support for hf_text (as of now, text and hovertext will be both handled independently). The new behavior is in line with the behavior that you described above; text is shown for the selected points from your aggregator.

Once PR #53 is merged, this functionality will be supported in the next release (v0.6.4)

Cheers, Jeroen

jonasvdd commented 2 years ago

Should be fixed in plotly-resampler 0.6.4! 🔥

prokie commented 2 years ago

Great. Fast to fix as always!