#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Animations in Plotly

Learn to create animated charts that change over time

Animations in Plotly

Why Animate?

Animations show change over time:

  • How data evolved over years
  • Progress through stages
  • Before/after comparisons

Basic Animation

code.py
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Country': ['USA', 'USA', 'USA', 'UK', 'UK', 'UK'],
    'Year': [2020, 2021, 2022, 2020, 2021, 2022],
    'GDP': [100, 105, 110, 80, 85, 88]
})

fig = px.bar(df, x='Country', y='GDP',
             animation_frame='Year',
             range_y=[0, 120])
fig.show()

Click play to see bars change!

Animated Scatter Plot

code.py
df = pd.DataFrame({
    'Country': ['USA', 'USA', 'USA', 'China', 'China', 'China'],
    'Year': [2020, 2021, 2022, 2020, 2021, 2022],
    'GDP': [20, 21, 23, 15, 17, 18],
    'Population': [330, 331, 332, 1400, 1410, 1420]
})

fig = px.scatter(df, x='GDP', y='Population',
                 animation_frame='Year',
                 color='Country',
                 size='GDP',
                 range_x=[10, 25],
                 range_y=[300, 1500])
fig.show()

Important: Set Range!

Without range, axes jump around:

code.py
# Bad - axes change
fig = px.bar(df, x='Country', y='GDP', animation_frame='Year')

# Good - axes stay fixed
fig = px.bar(df, x='Country', y='GDP',
             animation_frame='Year',
             range_y=[0, 120])

Animation Groups

Animate by category:

code.py
fig = px.scatter(df, x='GDP', y='Population',
                 animation_frame='Year',
                 animation_group='Country',  # Track each country
                 color='Country')
fig.show()

Animated Line Chart

code.py
df = pd.DataFrame({
    'Month': list(range(1, 13)) * 2,
    'Sales': [100, 110, 105, 120, 130, 140, 135, 150, 160, 170, 165, 180,
              90, 95, 100, 110, 115, 125, 130, 140, 145, 155, 160, 170],
    'Year': ['2023'] * 12 + ['2024'] * 12
})

fig = px.line(df, x='Month', y='Sales',
              animation_frame='Year',
              range_y=[80, 200])
fig.show()

Control Animation Speed

code.py
fig = px.scatter(df, x='GDP', y='Population',
                 animation_frame='Year')

fig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = 1000  # milliseconds
fig.layout.updatemenus[0].buttons[0].args[1]['transition']['duration'] = 500

fig.show()

Cumulative Animation

Show data building up:

code.py
df = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
    'Sales': [100, 120, 115, 140, 160],
    'Cumulative': [100, 220, 335, 475, 635]
})

# Create frame for each month
frames = []
for i in range(1, len(df) + 1):
    frames.append(df.head(i))

# Manual animation with graph_objects needed for cumulative

Complete Example

code.py
import plotly.express as px
import pandas as pd

# Sales over years
years = [2020, 2021, 2022, 2023]
products = ['A', 'B', 'C']

data = []
for year in years:
    for product in products:
        data.append({
            'Year': year,
            'Product': product,
            'Sales': 100 + (year - 2020) * 20 + ord(product) * 5
        })

df = pd.DataFrame(data)

fig = px.bar(df, x='Product', y='Sales',
             color='Product',
             animation_frame='Year',
             range_y=[0, 300],
             title='Product Sales Over Time')

fig.update_layout(
    xaxis_title='Product',
    yaxis_title='Sales (K)'
)
fig.show()

Key Points

  • animation_frame sets what changes
  • animation_group tracks items
  • Always set range_x and range_y
  • Works with scatter, bar, line, choropleth
  • Click play button to start
  • Use slider to scrub through

What's Next?

Learn to create dashboards with multiple charts.

SkillsetMaster - AI, Web Development & Data Analytics Courses