How to Create Animated Visualizations with Plotly | by Soner Yıldırım | Towards Data Science

We live in the era of big data. We can collect lots of data which allows to infer meaningful results and make informed business decisions. However, as the amount of data increases, it gets trickier to analyze and explore the data. There comes in the power of visualizations which are great tools in exploratory data analysis when used efficiently and appropriately. Visualizations also help to deliver a message to your audience or inform them about your findings. There is no one-fits-all kind of visualization method so certain tasks require different kinds of visualizations. In recents years, animated visualizations have gained more popularity as they make it possible to “tell” much more than a statis visualization. Animated visualizations are great at showing how things change over time.
We will cover how to create animated visuzalizations with Plotly Python (plotly.py) which is an open-source plotting library built on plotly javascript (plotly.js). One of the things I like about plotly.py is that it offers a high-level API (plotly express) and a low level API (graph objects) to create visualizations. With plotly express, we can create a nice plot with very few lines of code. On the other hand, we need to write more code with graph objects but have more control on what we create.
In this post, we will use plotly express API. I will write up another post to cover animated visualizations with graph objects.
We will merge two different datasets. One is obesity rate dataset which available here on kaggle. The raw dataset needs to be cleaned and reformatted. If you’d like to go through the data cleaning steps, feel free to visit my post on that:
towardsdatascience.com
Here is the first five rows of the cleaned dataframe:
The other dataset that we will use is not separating gender. Thus, we need to eliminate gender on this dataset by grouping on “country” and “year” and taking the mean of “obesity_rate”:
import numpy as npimport pandas as pd# fixing data tytpesobesity_cleaned = df2.astype({'obesity_rate': 'float32', 'year': 'int32'})# eliminating genderobesity_cleaned = obesity_cleaned.groupby(['country','year']).mean().reset_index()obesity_cleaned.head()
The other dataset is available as built-in dataset of plotly express. It is called gapminder and includes life expectancy, gdp per capita, population of 142 countries from 1952 to 2007 (with 5-year-increments). We first import plotly express and the dataset:
import plotly.express as pxdf_gdp = px.data.gapminder()df_gdp.head()
We can combine these two dataframes using merge function of pandas. The shared columns are country and year so we pass them to on parameter:
df_merge = pd.merge(df_gdp, obesity_cleaned,                     on=['country','year'])df_merge.head()
We can now create our first animated visualizations. Here is the code to produce the animated scatter plot. I will explain each parameter and then show the plot.
px.scatter(df_merge, x="obesity_rate", y="gdpPercap",animation_frame="year",size="pop", color="continent", hover_name="country",size_max = 50,range_x=[0.1, 50], range_y=[100,60000],log_x=True, log_y=True)fig.show()
We first right the name of the dataframe and specify x-axis and y-axis. So our animated scatter plot will show how gdp per capita and obesity rate changes over time. To make the plot animated, we use animation_frame parameter. We use “year” as the animation frame so values will change based on the year. We assign population to size parameter so size of the points in scatter plot becomes proportional to the population of the country. For color parameter, we use continent column so that we have an idea about each continent in general. Size_max parameter allows to adjust the size of points in scatter plot. If not specified, points may look too small to distringuish for human-eye. Range_x and range_y parameter are used to specify ranges so that all points remain visible during the animation. Finally, log_x and log_y parameters adjusting axis ranges on log scale. If there is big difference in values, it is better to use log scale to make the plot look nicer.
Visualizations are great tools to deliver a message. Animated plots are even more powerful as they account for time as well. With the plot we just created, we gain information about:
Population of countries
Gdp per capita of countries
Obesity rate of countries
How these values change over time
How continents differ according to these measure
If there is a correlation among these measures
Let’s see the animated scatter plot we created:
Countries in Europe have both high gdp per capita and obesity rates in general.
In general, African and Asian countries are way behind in terms of both gpd per capita and obesity rate.
Throug the years, Asian countries have increased theire gdp per capita than African countries.
There seems to be a positive correlation between obesity rate and gdp per capita (we, ofcourse, need more measures to confirm)
Although it is hard notice due to very small population size, Kuwait has always been the country with highest obesity rate and in top 3 in terms of gdp per capita.
We can also create an animated bar plot. For instance, how obesity rate changes in time can be animated with bar plots. Let’s choose 5 countries (one from each continent) to show on the plot.
countries = ['China', 'Germany', 'Senegal', 'Brazil', 'New Zealand']df5 = obesity_cleaned[obesity_cleaned.country.isin(countries)].reset_index(drop=True)
We also add a title this time. Here is the code to create an animated bar plot with plotly express:
fig = px.bar(df5, x="country", y="obesity_rate",color="country",animation_frame="year", animation_group="country",range_y=[0.1, 50],title='Obesity Rate from 1975 to 2016')fig.show()
Unfortunately, obesity rate has been increasing all over the world.
We have covered how to create some basic animated plots with plotly express. Ofcourse, this is just a little of what can be done with this amazing library. There are many other plot types that we can dynamically create with plotly. I will also write about how to create animated plots using graph objects of plotly.py. The syntax is a little more complicated but it offers more flexibility. It is better to get familiar with both APIs and choose the one that fits your needs best.
Just like any other topic, the best way to get familiar with plotly is to practice. Thus, I suggest creating lots of plots to sharpen your skills.
Thank you for reading. Please let me know if you have any feedback.