Animate Interactive plotly Graph in Python (2 Examples)
Hi! This tutorial will show you how to animate an interactive plotly graph in Python.
Here is an overview:
Let’s jump into the Python code!
Install & Import plotly
# install plotly pip install plotly # import plotly import plotly.express as px
Now that we have installed and imported plotly into our Python environment, we can make use of its plot-building functions to build an interactive graph.
However, though, we need data to visualize.
Create Sample Data
We will make use of the gapminder dataset, which is shipped along with the plotly package in Python.
Therefore, to load and preview the first 10 rows of the DataFrame, run the lines of code below:
df = px.data.gapminder() df.head(10) # country continent year lifeExp pop gdpPercap iso_alpha iso_num #0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4 #1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4 #2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4 #3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4 #4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4 #5 Afghanistan Asia 1977 38.438 14880372 786.113360 AFG 4 #6 Afghanistan Asia 1982 39.854 12881816 978.011439 AFG 4 #7 Afghanistan Asia 1987 40.822 13867957 852.395945 AFG 4 #8 Afghanistan Asia 1992 41.674 16317921 649.341395 AFG 4 #9 Afghanistan Asia 1997 41.763 22227415 635.341351 AFG 4
With the dataset loaded, we can now build animated plotly graphs.
Example 1: Animated Scatter Plot
In this first example, we will build an animated scatterplot:
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country", size="pop", color="continent", hover_name="country", log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90]) fig.show()
Here, we pass the DataFrame
df and plot
gdpPercap on the x-axis and
lifeExp on the y-axis.
The animation is based on the
year column and grouped by
country, resulting in a dynamic representation of life expectancy and GDP per capita over time.
The size of the data points is determined by the
pop column, and different colors represent different continents.
The country names are displayed when you hover over the data points. Additionally, the x-axis is displayed in a logarithmic scale, and the x and y-axis ranges are constrained.
Finally, fig.show() is used to display the animated scatter plot.
Example 2: Animated Bar Plot
In this second example, we will build an animated bar plot using the dataset:
fig = px.bar(df, x="continent", y="pop", color="continent", animation_frame="year", animation_group="country", range_y=[0,4000000000]) fig.show()
In the above example, we create a bar chart where each bar represents the population (
pop) of a country, grouped by continent.
The animation is driven by the
year column, with each frame showing how populations change over time. The
range_y = parameter is used to set the y-axis range from 0 to 4billion.
After that, we use
fig.show() to display the animated bar chart.
You can build and animate other graphs as well. The key thing is that your data should contain a time element over which the animation frames will run.
Video, Further Resources & Summary
Do you need more explanations on how to animate interactive plotly graphs in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to animate interactive plotly graphs in Python.
The YouTube video will be added soon.
So with that, we have demonstrated how to animate interactive plotly graphs in Python. Furthermore, you could have a look at some of the other interesting plotly in Python tutorials on Statistics Globe:
- Add & Remove Trace in plotly Graph in Python (3 Examples)
- How to Draw a plotly Line Plot in Python (Examples)
- Customize Layout of plotly Graph in Python (3 Examples)
- plotly.express Module in Python
- plotly Candlestick Chart in Python (3 Examples)
- Introduction to Python Programming
This post has shown how to animate interactive plotly graphs in Python. I hope you found it helpful! In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.