3

I have managed to make a plotly graph based on this code sample from Plotly documentation:

import plotly.graph_objects as go
import numpy as np

# Create figure
fig = go.Figure()

# Add traces, one for each slider step
for step in np.arange(0, 5, 0.1):
    fig.add_trace(
        go.Scatter(
            visible=False,
            line=dict(color="#00CED1", width=6),
            name=" = " + str(step),
            x=np.arange(0, 10, 0.01),
            y=np.sin(step * np.arange(0, 10, 0.01))))

# Make 10th trace visible
fig.data[10].visible = True

# Create and add slider
steps = []
for i in range(len(fig.data)):
    step = dict(
        method="update",
        args=[{"visible": [False] * len(fig.data)},
              {"title": "Slider switched to step: " + str(i)}],  # layout attribute
    )
    step["args"][0]["visible"][i] = True  # Toggle i'th trace to "visible"
    steps.append(step)

sliders = [dict(
    active=10,
    currentvalue={"prefix": "Frequency: "},
    pad={"t": 50},
    steps=steps
)]

fig.update_layout(
    sliders=sliders
)

fig.show()

Problem is that, as I am instead visualizing relatively big go.Images, the preparation of the plots is extremely slow and not suitable for the final interactive Streamlit app that I am trying to create.

To make it faster, I thought to have non-interactive plots which (I guess so at least) should be much faster. There is, however, no interactive=False option for the plotly traces, or at least I cannot find them. There is option to export figure to a static image, but that would make no sense, as the figure would have to be prepared anyway before it would be exported to png and visualized as a static image.

So, is there any way to modify this Plotly example in a way that keeps the sliders but directly creates non-interactive plot from a Numpy array?

Or do I have to switch do a different library completely? Thanks.

Valeria
  • 1,508
  • 4
  • 20
  • 44
  • UPD: I had to switch to Bokeh to make it work asap and can recommend it. However, I am still curious if it would be also possible in Plotly. – Valeria Jan 02 '23 at 13:51
  • Which IDE are you using? – Hamzah Jan 04 '23 at 14:18
  • Does this link help? https://stackoverflow.com/a/74191965/16733101 – Hamzah Jan 04 '23 at 14:23
  • You can use streamline caching and/or embed the pre-generated html string of the interactive chart into the streamlit app – Kaymal Jan 06 '23 at 20:24
  • @Kaymal in no way does streamlit caching helps in this situation. The question was how to get directly non-interactive background image so that user doesn't have to wait every time some parameter is changed... Pre-generating ~10000 different images for all possible user choices seem mildly sub-optimal as well. As I mentioned, I used Bokeh instead and can recommend it for this use-case over plotly. – Valeria Mar 08 '23 at 11:47

0 Answers0