
Create Animated Charts In Python
Introduction
If you are working as a data analyst or data scientist for some time, you may have already known how to use matplotlib to visualize and present data in various charts. The matplotlib library provides an animation module to generate dynamic charts to make your data more engaging, however it still takes you a few steps to format your data, and initialize and update the data into the charts. In this article, I will demonstrate you another Python library – pandas-alive which allows you to generate animated charts directly from pandas data without any format conversion.
Prerequisites
You can install this library via pip command as per below if you do not have it in your working environment yet:
pip install pandas-alive
It will also install its dependencies such as pandas, pillow and numpy etc.
For demonstration of our later examples, let’s grab some sample covid-19 data from internet, you can download it from here.
Before we start, we shall import all the necessary modules and do a preview of our sample data:
import pandas as pd import pandas-alive df_covid = pd.read_excel("covid-19 sample data.xlsx")
The data we will be working on would be something similar to the below:
Now with all above ready, let’s dive into the code examples.
Generate animated bar chart race
Bar chart is the most straightforward way to present data, it can be drawn in horizontal or vertical manner. Let’s do a minor formatting on our data so that we can use date as horizontal or vertical axis to present the data.
df_covid = df_covid.pivot(index="date", columns="location", values="total_cases").fillna(0)
To create an animated bar chart horizontally, you can simply call plot_animated as per below:
df_covid.plot_animated("covid-19-h-bar.gif", period_fmt="%Y-%m", title="Covid-19 Cases")
The plot_animated function has default parameters kind=”race” and orientation = “h”, hence the output gif would be generated as per below:
You can change the default values of these two parameters to generate a vertical bar chart race:
df_covid.plot_animated("covid-19-v-bar.gif", period_fmt="%Y-%m", title="Covid-19 Cases", orientation='v')
The output chart would be something similar to below:
Generate animated line chart
To create an animated line chart, you just need to change the parameter kind = “line” as per below:
df_covid.plot_animated("covid-19-line.gif", title="Covid-19 Cases", kind='line', period_fmt="%Y-%m", period_label={ 'x':0.25, 'y':0.9, 'family': 'sans-serif', 'color': 'darkred' })
There are some other parameters such as period_label to control the format of the label, or n_visible to constrain how many records to be shown on the chart. The output chart would be as per the below:
Generate animated pie chart
Similar to other charts, you can create a simple pie chart with below parameters:
df_covid.plot_animated(filename='covid-19-pie-chart.gif', kind="pie", rotatelabels=True, tick_label_size=5, dpi=300, period_fmt="%Y-%m", )
You can also use other Axes.Pie parameters to define the pie chart behavior. The output from above code would be:
Generate scatter chart
Generate scatter chart or bubble chart is slightly complicated than other charts, but for our sample data, it does not make much sense to visualize it in this type of charts. E.g.
df_covid.plot_animated(filename='covid-19-scatter-chart.gif', kind="scatter", period_label={'x':0.05,'y':0.9}, steps_per_period=5 )
You shall see the output is similar to the line chart:
Conclusion
Pandas-Alive provides very convenient ways to generate all sorts of animated charts from pandas data frame with the underlying support from the Matplotlib library. It accepts most of the parameters you used in matplotlib, so you don’t have to learn a lot of new things before applying it for your charts.
There are many more features beyond the basis I have covered in above, such as supplying custom figures, generating GeoSpatial charts or combining multiple animated charts in one view. You can check more examples from its project page.