I'm using a slider bar to display a heatmap animation and wish to relabel the tick marks appearing on the slider bar.
There are 50 frames in my animation, and each matrix in the heatmap has dimension 7-by-7, where the seven rows/columns correspond to the list,labels=['A','B','C','D','D','F','G']. Moreover, each frame corresponds to a 5-second window in my application, so, for example, frame 3 corresponds to 15 seconds. Here's my simple example so far, where I'm mimicking the documentation at https://plotly.com/python/sliders/
import numpy as np
import plotly.graph_objs as go
N = 50
M = np.random.random((N, 7, 7))
labels=['A','B','C','D','D','F','G']
window_length=5
fig = go.Figure()
for step in range(N):
fig.add_trace(
go.Heatmap(x=labels,y=labels,z=M[step]))
fig.data[1].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": "Time: " + str(window_length*i)+' sec'}],)
step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
steps.append(step)
sliders = [dict(
active=0,
currentvalue={"prefix": "Time "},
pad={"t": 5},
steps=steps
)]
fig.update_layout(
sliders=sliders)
fig.show()
This works fine and I get the image below for one of my frames:
I want to make two adjustments:
I think my problem is that I quite understand is accomplished by
sliders = [dict(
active=0,
currentvalue={"prefix": "Time "},
pad={"t": 5},
steps=steps
)]
To show the seconds on the slider's tick labels you can set label=str(window_length * i)
in the dictionary used to create the slider's steps.
To show "Time: 20 seconds" above the slider you can set currentvalue={"prefix": "Time: ", "suffix": " seconds"}
in the dictionary used to create the slider itself.
import numpy as np
import plotly.graph_objs as go
N = 50
M = np.random.random((N, 7, 7))
labels = ["A", "B", "C", "D", "D", "F", "G"]
window_length = 5
fig = go.Figure()
for step in range(N):
fig.add_trace(go.Heatmap(x=labels,y=labels,z=M[step]))
fig.data[1].visible = True
steps = []
for i in range(len(fig.data)):
step = dict(
method="update",
args=[{"visible": [False] * len(fig.data)},
{"title": "Time: " + str(window_length * i)+" seconds"},],
label=str(window_length * i))
step["args"][0]["visible"][i] = True
steps.append(step)
sliders = [dict(
active=0,
currentvalue={"prefix": "Time: ", "suffix": " seconds"},
pad={"t": 5},
steps=steps
)]
fig.update_layout(sliders=sliders)
fig.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With