Defining default/starting frame with Plotly Express Animation

I have two plotly express plots (one animated choropleth map and one animated barplot, see https://covid19mtl.ca/en) 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: https://colab.research.google.com/drive/1Qih2IjZLhwaxkAauyBnHQu96sCWner87

Thanks! :slight_smile:

1 Like

Hi @jeremym,

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

mtlmap_fig.update_traces(z=mtlmap_fig.frames[-1].data[0].z)
mtlmap_fig.show()
1 Like

Thanks!
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:

mtlmap_fig.update_traces({
    '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: https://covid19mtl.ca/en

Thanks again!