Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

Animations in Plotly

Animate Plotly charts over time (or a category) using animation frames.

Why animations

Animations are useful when you want to show change over time as motion, not just as a static trend line:

  • GDP vs life expectancy by year
  • Covid cases by month

Under the hood, Plotly builds one frame per value of your animation column, then adds Play/Pause controls and a slider that swap between frames. It's not "real" motion — it's the same trick as a flipbook, just automated.

Animated scatter (classic example)

Python
import plotly.express as px
 
df = px.data.gapminder()
 
fig = px.scatter(
    df,
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    color="continent",
    hover_name="country",
    log_x=True,
    size_max=60,
    animation_frame="year",
    animation_group="country",
    title="Gapminder: GDP vs Life Expectancy (animated)",
)
 
fig.show()

animation_frame="year" tells Plotly to build one frame per year; animation_group="country" tells it which points in different frames are "the same point" so a bubble smoothly moves/resizes instead of popping to a new spot each frame.

sketch Animation = swapping between precomputed frames p5.js
Each 'year' is a frame; points move smoothly between their positions in consecutive frames, like a flipbook.

Tips

  • Animations work best with fewer points (or aggregation) — dozens of moving bubbles read clearly, thousands become a blur.
  • Add animation_group so Plotly tracks each entity across frames instead of treating every frame as unrelated data.
  • Consider facets if animation is too visually busy — sometimes seeing all years at once, side by side, communicates the trend faster than watching it play out.
diagram How Plotly animation frames work mermaid
One data slice per animation value becomes one frame; animation_group links the same entity across frames.

Check your understanding

quizCheck yourself
  1. What does animation_frame="year" do in the gapminder example?

    Show answer

    B — Builds one frame per year with play controls and a slider — Plotly builds one frame per value of the animation column and adds Play/Pause controls plus a slider.

  2. What is animation_group="country" for?

    Show answer

    A — Telling Plotly which points across frames are the same entity — It lets a bubble move smoothly between frames instead of being treated as unrelated data.

  3. In the gapminder animated scatter, what does log_x=True apply to?

    Show answer

    D — GDP per capita on the x axis — x is gdpPercap, which spans a huge range, so a log scale spreads the points out.

  4. When might facets be better than an animation?

    Show answer

    A — When the animation is too busy and you want all years visible at once — Seeing all years side by side can communicate the trend faster and allows precise comparison.

Next

This wraps the interactive visualization phase — continue to Phase 8: Statistics for Data Analytics to build the analytical foundations behind these charts.

🧪 Try It Yourself

Exercise 1 – Animate Over a Column

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Track Entities Across Frames

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Use a Log Axis for Skewed Data

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did