Hi there,
I use Plotly Python.
I have a list of spectrums I want to plot.
This list contains 2d arrays. Each array corresponds to a time frame and contains the spectrums for a certain number of sources (nxm with n number of sources and m length of the spectrums).
Here is a dummy version of that list (random values instead of spectrums):
a = [np.random.rand(20, 5120) for i in range(1000)] # List of 1000 arrays/time frames with 20 sources/spectrums each
So far Iβve been able to plot that for one time frame (one value of the list) only in a plotly plot, by adding n traces to a figure where n is the number of sources I plot.
Now I would like to make it interactive and add a slider to quickly go through the time frames (elements of my list) without having to close the figure, change the frame index and plot for this new time frame.
I found an intermediary solution in this post: plot - Python: Change Custom Control Values in Plotly - Stack Overflow
But it seems, as well as in all the other posts I found on the topic, to just control the visibility parameters of traces. So I guess under the hood it plots everything and just hides or show. And in my case I can have a list of 1000 arrays/time frames, each containing 20 spectrums each. No need to say itβs not doing too wellβ¦
Is there any way to do it otherwise?
Below is a snapshot of what i produce at the moment for a single sequence:
And here is what I acheived by using the soluyion in the stack overflow solution given above, to show just a few sequences that can be switched with a slider:
You can reproduce this plot with the following code and with a list of dummy arrays (not actual real spectrums):
a = [np.random.rand(20, 5120) for i in range(1000)] # List of 1000 arrays/time frames with 20 sources/spectrums each
# Create figure
fig = go.Figure()
# Add traces, one for each slider step
for i in np.arange(len(a)):
for k in range(0, a[i].shape[0]):
fig.add_trace(go.Scatter(mode="lines", x=frequencies, y=a[i][k, :], name=f"Source {k}"))
# Create and add slider
steps = []
for i in range(len(a)):
step = dict(
method="restyle",
args=["visible", [False] * a[i].shape[0]*len(a)],
)
step["args"][1][(i*a[i].shape[0]):(i*a[i].shape[0]+a[i].shape[0])] = [True]*a[i].shape[0] # Toggle i'th trace to "visible"
steps.append(step)
sliders = [dict(
active=1,
currentvalue={"prefix": "Frequency: "},
pad={"t": 50},
steps=steps
)]
fig.update_layout(
sliders=sliders
)
# Edit slider labels
fig['layout']['sliders'][0]['currentvalue']['prefix']='Sequence: '
for i in range(len(a)):
fig['layout']['sliders'][0]['steps'][i]['label']=i
fig.show()
Thanks in advance for your help!
Antoine