How to show a Plotly animated slider in Python?

Plotly supports creating interactive animated visualizations with sliders that allow users to control the animation timeline. In this tutorial, we will show how to create an animated scatter plot with a slider using plotly.express.

Key Components

  • plotly.express Used to generate interactive figures with built-in animation support

  • animation_frame Defines which column to use for animation frames

  • animation_group Groups data points that should animate together

Creating Sample Data

Since external CSV files aren't available online, let's create sample data to demonstrate the animated slider ?

import plotly.express as px
import pandas as pd

# Create sample student performance data
data = {
    'student_id': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'expected': [85, 78, 92, 87, 80, 94, 89, 82, 96],
    'final': [83, 76, 90, 85, 78, 92, 87, 80, 94],
    'semester': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    'state': ['CA', 'NY', 'TX', 'CA', 'NY', 'TX', 'CA', 'NY', 'TX']
}

df = pd.DataFrame(data)
print(df.head())
   student_id  expected  final  semester state
0           1        85     83         1    CA
1           2        78     76         1    NY
2           3        92     90         1    TX
3           1        87     85         2    CA
4           2        80     78         2    NY

Creating the Animated Scatter Plot

Now we'll create an animated scatter plot where each frame represents a different semester ?

import plotly.express as px
import pandas as pd

# Sample data
data = {
    'student_id': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'expected': [85, 78, 92, 87, 80, 94, 89, 82, 96],
    'final': [83, 76, 90, 85, 78, 92, 87, 80, 94],
    'semester': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    'state': ['CA', 'NY', 'TX', 'CA', 'NY', 'TX', 'CA', 'NY', 'TX']
}

df = pd.DataFrame(data)

# Create animated scatter plot
fig = px.scatter(
    df, 
    x="expected", 
    y="final",
    animation_frame="semester",
    animation_group="student_id",
    color="state", 
    hover_name="state",
    title="Student Performance: Expected vs Final Scores by Semester"
)

# Optional: Remove play/pause buttons to show only slider
fig["layout"].pop("updatemenus", None)

fig.show()

Parameters Explained

Parameter Purpose Example Value
animation_frame Column used for animation timeline "semester"
animation_group Groups data points that animate together "student_id"
color Column used for color coding "state"
hover_name Column shown on hover "state"

Customizing the Animation

You can further customize the animation speed and transitions ?

import plotly.express as px
import pandas as pd

# Sample data
data = {
    'student_id': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'expected': [85, 78, 92, 87, 80, 94, 89, 82, 96],
    'final': [83, 76, 90, 85, 78, 92, 87, 80, 94],
    'semester': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    'state': ['CA', 'NY', 'TX', 'CA', 'NY', 'TX', 'CA', 'NY', 'TX']
}

df = pd.DataFrame(data)

fig = px.scatter(
    df, 
    x="expected", 
    y="final",
    animation_frame="semester",
    animation_group="student_id",
    color="state",
    size_max=15,
    range_x=[70, 100],
    range_y=[70, 100]
)

# Customize animation settings
fig.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] = 1000
fig.layout.updatemenus[0].buttons[0].args[1]["transition"]["duration"] = 500

fig.show()

Key Features

  • Interactive Slider Users can manually control the animation timeline

  • Play/Pause Controls Automatic animation with customizable speed

  • Smooth Transitions Data points smoothly move between frames

  • Hover Information Additional details appear on mouse hover

Conclusion

Plotly's animated sliders provide an effective way to visualize data changes over time. Use animation_frame to define the timeline and animation_group to track individual data points across frames. This creates engaging, interactive visualizations perfect for presentations and data exploration.

Updated on: 2026-03-26T22:25:11+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements