Defining default/starting frame with Plotly Express Animation

I have two plotly express plots (one animated choropleth map and one animated barplot, see for which I’d like to display the last frame (the latest date) as the default/starting value.

So far, I’ve been able to change the default slider value, but this does not update the default frame in the plot (which remains the first frame). Is there a way I can force the plot to update after setting the slider value?

Example code in this notebook:

Thanks! :slight_smile:

1 Like

Hi @jeremym,

This line of code updates your fig data and displays the last frame:

1 Like

I also had to update the hovertemplate so that it displayed the correct date.

mtlmap_fig.update_traces(z=mtlmap_fig.frames[-1].data[0].z, hovertemplate=mtlmap_fig.frames[-1].data[0].hovertemplate)

After changing this plot to use a discrete colour scale, it seems this solution doesn’t work anymore. I haven’t been able to figure out the problem, other than that z no longer contains the same data.

This no longer works:

mtlmap_fig.layout.sliders[0]['active'] = len(mtlmap_fig.frames) - 1  # slider
mtlmap_fig.update_traces(z=mtlmap_fig.frames[-1].data[0].z)  # frame

Likewise for the hovertemplate:

    'hovertemplate': # hovertemplate str
for frame in mtlmap_fig.frames:
    frame['data'][0]['hovertemplate'] = # hovertemplate str

How can I achieve the same thing (setting the default frame to be the last frame instead of the first and changing the hovertemplate) when using a discrete colourmap with px.choropleth_mapbox?

Sample code here:

Live version of the map here:

Thanks again!