predict-idlab / plotly-resampler

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

[BUG] Resampling not updating on all traces using go.Scatter #242

Closed avdcc closed 1 year ago

avdcc commented 1 year ago

Hi,

I am trying to plot using multiple y-axis. To do this, I need to pass go.Scatter() to the ".add_trace" methods. By doing so, the resampling when zooming in only displays in the first trace, but not any of the following traces.

You can see this happening by slightly modifying the code in "03_minimal_cache_dynamic.py", replacing the construct_display_graph with this:

@app.callback(
    ServersideOutput({"type": "store", "index": MATCH}, "data"),
    Output({"type": "dynamic-graph", "index": MATCH}, "figure"),
    State("add-chart", "n_clicks"),
    Trigger({"type": "interval", "index": MATCH}, "n_intervals"),
    prevent_initial_call=True,
)
def construct_display_graph(n_clicks) -> FigureResampler:
    fig = FigureResampler(go.Figure(), default_n_shown_samples=2_000)

    # Figure construction logic based on a state variable, in our case n_clicks
    sigma = n_clicks * 1e-6

    fig.add_trace(
        go.Scatter(
            x=x,
            y=noisy_sin * (1 + sigma) ** x - 100000,
            name="exp - 100000",
            yaxis="y",
            mode="lines"
        )
    )

    fig.add_trace(
        go.Scatter(
            x=x,
            y=noisy_sin * (1 + sigma) ** x + 100000,
            name="exp + 100000",
            yaxis="y2",
            mode="lines"
        )
    )

    fig.update_layout(title=f"<b>graph - {n_clicks}</b>", title_x=0.5)

    fig.update_layout(
        yaxis={
            "title": "exp - 100000",
        },
        yaxis2={
            "title": "exp + 100000",
            "anchor": "free",
            "overlaying": "y",
            "autoshift": True,
        }
    )

    return fig, fig

image

Environment information: (please complete the following information)

jonasvdd commented 1 year ago

Hi, thank you for submitting this Issue!

I can indeed confirm that this is a bug, so I will look into it!

We perform some layout-matching in the back-end, to see which traces need to be aggregated, and we did not (yet) provide support for overlaying y-axes.

I will come back to this issue somewhere later today!

jonasvdd commented 1 year ago

Update: altered the source-code in order that your example works.

TODO:

jonasvdd commented 1 year ago

@avdcc,

I created a PR (#244) with a fix. As of now, I wait till someone will review it. As you can see, the fix itself is not that complex. I suggest you also look at the tests I added, as these give some nice demos on how to use multiple y-axes with subplots :)

Cheers, Jonas

jonasvdd commented 1 year ago

@avdcc

Should be fixed in plotly-resampler==0.9.1 -

avdcc commented 1 year ago

yes, now it works perfectly for my dashboard :) thank you so much, not only for the help and very quick update, but all the work on this project.