How to Create Animated Visualizations with Plotly
By: Soner Yıldırım
Tags: Data Visualization, Artificial Intelligence, Machine Learning, Data Science

How to Create Animated Visualizations with Plotly

Take your data viz skills one step further.

Photo by Daniel Cheung on Unsplash

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:

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 np

import pandas as pd # fixing data tytpes

obesity_cleaned = df2.astype({'obesity_rate': 'float32', 'year': 'int32'}) # eliminating gender

obesity_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 px df_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: