Fix contour plot in animation


I’m attempting to animate a scatter/contour. I’ve managed to animate the scatter plot, however, when attempting to include any contour, it disappears immediately after initiating the animation. If possible, I would like the contour to be visible throughout the animation.

For context, the plot is meant to visualise a parameter optimisation algorithm. However, since the actual data here isn’t relevant, I’ve attempted to recreate the problem below using the gapminder dataset.


import as px
import plotly.graph_objects as go
import numpy as np

df =
df['gdpPercap'] = df['gdpPercap'] / 1000

fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
                 size="pop", color="continent", size_max=55, range_x=[0.01,100], range_y=[25,90])

fig.add_trace(go.Contour(z=np.random.uniform(0,5,size=[75, 100]), y = np.arange(25, 90, 5)))


From the animation page, I understand this may be possible by;

Defining redraw: Setting redraw: false is an optimization for scatter plots so that animate just makes changes without redrawing the whole plot. For other plot types, such as contour plots, every frame must be a total plot redraw, i.e. redraw: true .

  1. I’m having difficulty finding where to define redraw: true .
  2. Is this possible using ?
1 Like