Scattergeo with sliders - all the data displayed at the beginning

I have searched for an answer to this question, and although there are a couple of similar questions, there isn’t a direct answer.

I have a scattergeo plot, that shows all the time data at the beginning, like this:

I would like it to show on the data at the first step when it first loads, however like this:

Please let me know how to set up the sliders dictionary, or the figure object to make this work. THanks!

 Define your Scattergeo plot objects
scattergeo_plots = data
#print(scattergeo_plots)


steps = []

for i in range(len(data)):
    y = get_formatted_yr_range(year_ranges[i])
    step = dict(
        method='update',
        args=[{'visible': [False] * len(data)}, {'title': f'Time ({y})'}],
        label = y
    )
    step['args'][0]['visible'][i] = True
    steps.append(step)
#print (steps)
# Create the slider configuration
sliders = [{
    'active': 0,
    'currentvalue': {'prefix': 'Trace: '},
    'pad': {'t': 50},
    'steps': steps
}]

print(sliders)

# Create figure
fig = go.Figure(
    data=scattergeo_plots,  # Start with the first plot object
    layout=go.Layout(
        title="Scattergeo Sliders",
        showlegend=False,
        geo=dict(
            projection_type="natural earth"  # Set the desired projection type
        ),
        sliders = sliders
    )
)

# Show the figure
fig.show()

Hey @ama ,

How do you define your Scattergeo ?

Just an idea;

for i in np.arange(0,5,1):
    fig.add_trace(go.Scattergeo(visible=False, ...))


fig.data[0].visible = True

This way you can hide all the traces and make it visible only the first one.

And if you want to keep previous points you can try something like this:

import plotly.graph_objects as go
import numpy as np

x = [16, 11, 18, 33, 49]
y = [12, 32, 41, 33, 26]

# Create figure
fig = go.Figure(go.Scattergeo(lon=[],
                              lat=[],
                              mode="markers",
                              marker=dict(color="red", size=10)
                             )
                )

# Frames
frames = [go.Frame(data= [go.Scattergeo(lon=x[:k+1],
                                        lat=y[:k+1]
                                        )
                          ],
                   traces= [0],
                   name=f'frame{k}'      
                  )for k  in  range(len(x))
          ]

fig.update(frames=frames)


def frame_args(duration):
    return {
            "frame": {"duration": duration},
            "mode": "immediate",
            "fromcurrent": True,
            "transition": {"duration": duration, "easing": "linear"},
            }

sliders = [
    {"pad": {"b": 10, "t": 60},
     "len": 0.9,
     "x": 0.1,
     "y": 0,
     
     "steps": [
                 {"args": [[f.name], frame_args(0)],
                  "label": str(k),
                  "method": "animate",
                  } for k, f in enumerate(fig.frames)
              ]
     }
        ]

fig.update_layout(sliders=sliders,
                  geo=dict(projection_type="natural earth")
                  )
fig.show()

scattergeo

Thank you, this worked perfectly. I had no idea that the sliders could be setup using the frames data, like you did.

1 Like