🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Customie sliders in Python

I am fairly new to the world of Plotly, and I have been playing around with the example script provided by plotly here: https://plotly.com/python/sliders/. However I quickly came across some studs I don’t understand.

  1. I can not figure out how to customize simple things as changing the “step” annotation on the slider. Can I change it to show just the steps without the text?

  2. How would I go about having the user select one value in the slider that actually corresponds to another value in the expression in drawing the trace? Can it be done with condlist and choicelist? Or can I set steps = somethingelse in the slider dict?

Hi @NRVA, I updated the example below to show how to change the label of the step

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="restyle",
        args=["visible", [False] * len(fig.data)],
        label=str(i),
    )
    step["args"][1][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()

You can take a look at the API documentation of go.layout.Slider and go.layout.slider.Step to see the available options.

1 Like