If you can use Dash. I think you can do as below:
Option 1: If slider was 1, trace 1 shows, If slider was 2, trace 1 and 2 show, If slider was 3, trace 1 and 2 and 3 show
from dash import Dash, dcc, html, Input, Output
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Slider(1, 3, 1,
value=1,
id='my-slider'
),
dcc.Graph(id='slider-output-container',figure={})
])
@app.callback(
Output('slider-output-container', 'figure'),
Input('my-slider', 'value'))
def update_output(value):
if value == 1:
fig = go.Figure()
# Define the traces
trace1 = go.Scattermapbox(
lat=[40.71, 40.72, 40.73],
lon=[-74.00, -74.01, -74.02],
mode='markers',
marker=dict(size=10, color='blue'),
below='traces',
visible=True)
fig.add_trace(trace1)
fig.update_layout(mapbox_style='carto-positron',mapbox=dict(
center=dict(lat=40.725, lon=-74.005),
zoom=11)
)
elif value == 2:
fig = go.Figure()
# Define the traces
trace1 = go.Scattermapbox(
lat=[40.71, 40.72, 40.73],
lon=[-74.00, -74.01, -74.02],
mode='markers',
marker=dict(size=10, color='blue'),
below='traces',
visible=True)
trace2 = go.Scattermapbox(
lat=[40.72, 40.73, 40.74],
lon=[-74.01, -74.02, -74.03],
mode='markers',
marker=dict(size=10, color='red'),
below='traces',
visible=True
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.update_layout(mapbox_style='carto-positron',mapbox=dict(
center=dict(lat=40.725, lon=-74.005),
zoom=11)
)
elif value == 3:
fig = go.Figure()
# Define the traces
trace1 = go.Scattermapbox(
lat=[40.71, 40.72, 40.73],
lon=[-74.00, -74.01, -74.02],
mode='markers',
marker=dict(size=10, color='blue'),
below='traces',
visible=True)
trace2 = go.Scattermapbox(
lat=[40.72, 40.73, 40.74],
lon=[-74.01, -74.02, -74.03],
mode='markers',
marker=dict(size=10, color='red'),
below='traces',
visible=True
)
trace3 = go.Scattermapbox(
lat=[40.73, 40.74, 40.75],
lon=[-74.02, -74.03, -74.04],
mode='markers',
marker=dict(size=10, color='green'),
below='traces',
visible=True
)
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.add_trace(trace3)
fig.update_layout(mapbox_style='carto-positron',mapbox=dict(
center=dict(lat=40.725, lon=-74.005),
zoom=11)
)
return fig
if __name__ == '__main__':
app.run_server(debug=False)
Option 2: If slider was 1, trace 1 shows, If slider was 2, trace 2 shows, If slider was 3, trace 3 shows
from dash import Dash, dcc, html, Input, Output
import plotly.graph_objs as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Slider(1, 3, 1,
value=1,
id='my-slider'
),
dcc.Graph(id='slider-output-container',figure={})
])
@app.callback(
Output('slider-output-container', 'figure'),
Input('my-slider', 'value'))
def update_output(value):
if value == 1:
fig = go.Figure()
# Define the traces
trace1 = go.Scattermapbox(
lat=[40.71, 40.72, 40.73],
lon=[-74.00, -74.01, -74.02],
mode='markers',
marker=dict(size=10, color='blue'),
below='traces',
visible=True)
fig.add_trace(trace1)
fig.update_layout(mapbox_style='carto-positron',mapbox=dict(
center=dict(lat=40.725, lon=-74.005),
zoom=11)
)
elif value == 2:
fig = go.Figure()
trace2 = go.Scattermapbox(
lat=[40.72, 40.73, 40.74],
lon=[-74.01, -74.02, -74.03],
mode='markers',
marker=dict(size=10, color='red'),
below='traces',
visible=True
)
fig.add_trace(trace2)
fig.update_layout(mapbox_style='carto-positron',mapbox=dict(
center=dict(lat=40.725, lon=-74.005),
zoom=11)
)
elif value == 3:
fig = go.Figure()
trace3 = go.Scattermapbox(
lat=[40.73, 40.74, 40.75],
lon=[-74.02, -74.03, -74.04],
mode='markers',
marker=dict(size=10, color='green'),
below='traces',
visible=True
)
fig.add_trace(trace3)
fig.update_layout(mapbox_style='carto-positron',mapbox=dict(
center=dict(lat=40.725, lon=-74.005),
zoom=11)
)
return fig
if __name__ == '__main__':
app.run_server(debug=False)