How to Create an Animated Bar Chart in Python With Plotly and Excel
Plotly Express animates a bar chart when you pass a date column as animation_frame. Read daily sales by country from Excel with pandas, format the dates as text, build the chart with px.bar and save it as an HTML file with a play button and a date slider.

Data and setup
The data needs long format: one row per date and country, with Date, Country and Sales columns. Each date becomes one frame of the animation, so every country should have a row for every date.

Install the three packages with pip install plotly pandas openpyxl (pandas uses openpyxl to read .xlsx files). Then:
- Import the necessary libraries:
import plotly.express as px
import pandas as pd
- Load your Excel file into a DataFrame:
df = pd.read_excel("data.xlsx", usecols="A:C")
- Clean your data by dropping any null values:
df = df.dropna()
Format the dates
Convert the Date column to text before you plot. Plotly uses each distinct value as a frame label, and a plain YYYY-MM-DD string gives clean slider labels instead of full timestamps:
df['Date'] = df['Date'].dt.strftime("%Y-%m-%d")
The video uses df.dropna(inplace=True). Reassigning the result, as in the code above, does the same and is the style the pandas documentation recommends.
Build the chart
One px.bar call creates the whole animation. The important argument is animation_frame='Date'; a fixed range_y keeps the axis from rescaling on every frame:
fig = px.bar(
df,
x='Country',
y='Sales',
color='Country',
animation_frame='Date',
range_y=[0, df['Sales'].max() * 1.1],
)
- x: The column representing the countries.
- y: The column representing sales figures.
- color: Allows you to color the bars based on the country.
- animation_frame: This will create the animation based on the date.
- range_y: Fixes the y-axis from 0 to 10% above the highest sales value, so the tallest bar always fits.
Show and share
fig.show() opens the chart in the browser or notebook, and write_html saves it as a single HTML file:
fig.show()
fig.write_html("animated_bar_chart.html")
Anyone can open that file in a browser and play the animation, without Python.
Legend and limits
The legend stays interactive in the saved file: click a country to hide its bar, double-click to show only that country. The animation keeps playing with the remaining bars.
Note that Plotly keeps the bars in a fixed order, so this is an animated bar chart rather than a true race where bars overtake each other. For a race that re-sorts every frame and exports to a video file, see my bar race animation with Excel and Python.

The Plotly Express quick fixes work on animated charts too, for example a cleaner template and a bold title with a subtitle.
