In the era of data explosion, data scientists and analysts are required to have a deeper understanding and analysis of data, but also need to effectively communicate the results to others. How to make the target audience understand more intuitively? Visualize the data, of course, and preferably dynamically.

Using linear, bar, and pie charts as examples, this article will systematically explain how to make your data chart move.

What are these motion charts made of?

Those of you who are familiar with data visualization should be familiar with the Matplotlib library in Python. It is an open source data plotting package based on Python that helps developers generate histograms, power spectra, bar charts, scatter charts, and more in just a few lines of code. The library has a very useful extension called FuncAnimation to get our static charts moving.

FuncAnimation is part of the Animation class in the Matplotlib library, and I’ll show you more examples later. For the first time, you can think of this function simply as a While loop that constantly redraws the target data graph on the “canvas.”

How to use FuncAnimation?

This process starts with the following two lines of code:

import matplotlib.animation as ani
animator = ani.FuncAnimation(fig, chartfunc, interval = 100)
Copy the code

Here we can see several inputs to FuncAnimation:

  1. FIG is the figure object used to “chart”;

  2. Chartfunc is a function that takes a number as input, meaning the time in a time series.

  3. Interval is the delay between frames in milliseconds. The default value is 200.

These are the three key inputs, but there are many more optional inputs that interested readers can refer to in the original documentation, which will not be covered here.

The next step is to parameterize the data graph to convert it into a function, and then take the points in the function’s time series as input. Once that’s done, you can start.

Make sure you understand basic data visualization before you start. In other words, we need to visually process the data first, and then dynamically process it.

Follow the code below to make the basic call. In addition, data on the spread of a pandemic (including daily deaths) will be used as case data.

import matplotlib.animation as ani
import matplotlib.pyplot as plt
import numpy as np
import pandas as pdurl = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_seri es_covid19_deaths_global.csv'
df = pd.read_csv(url, delimiter=', ', header='infer')df_interest = df.loc[
    df['Country/Region'].isin(['United Kingdom'.'US'.'Italy'.'Germany'])
    & df['Province/State'].isna()]df_interest.rename(
    index=lambda x: df_interest.at[x, 'Country/Region'], inplace=True)
df1 = df_interest.transpose()df1 = df1.drop(['Province/State'.'Country/Region'.'Lat'.'Long'])
df1 = df1.loc[(df1 != 0).any(1)]
df1.index = pd.to_datetime(df1.index)
Copy the code

Code words are not easy to nonsense two sentences: need python learning materials or technical questions to exchange “click”

Draw three common dynamic charts

Draw dynamic line diagram

As shown below, the first thing you need to do is define the terms of the diagram, which will remain the same once the base terms are set. They include: creating figure objects, x and Y labels, setting line colors and figure margins, etc.

import numpy as np
import matplotlib.pyplot as pltcolor = ['red'.'green'.'blue'.'orange']
fig = plt.figure()
plt.xticks(rotation=45, ha="right", rotation_mode="anchor") #rotate the x-axis values
plt.subplots_adjust(bottom = 0.2, top = 0.9) #ensuring the dates (on the x-axis) fit in the screen
plt.ylabel('No of Deaths')
plt.xlabel('Dates')
Copy the code

Next, set the curve function and use.funcanimation to animate it:

def buildmebarchart(i=int) :
    plt.legend(df1.columns)
    p = plt.plot(df1[:i].index, df1[:i].values) #note it only returns the dataset, up to the point i
    for i in range(0.4):
        p[i].set_color(color[i]) #set the colour of each curveimport matplotlib.animation as ani
animator = ani.FuncAnimation(fig, buildmebarchart, interval = 100)
plt.show()
Copy the code

Dynamic pie chart

As you can see, the code structure doesn’t look too different from the line diagram, but there are still subtle differences.

import numpy as np
import matplotlib.pyplot as pltfig,ax = plt.subplots()
explode=[0.01.0.01.0.01.0.01] #pop out each slice from the piedef getmepie(i):
    def absolute_value(val) : #turn % back to a number
        a  = np.round(val/100.*df1.head(i).max().sum(), 0)
        return int(a)
    ax.clear()
    plot = df1.head(i).max().plot.pie(y=df1.columns,autopct=absolute_value, label=' ',explode = explode, shadow = True)
    plot.set_title('Total Number of Deaths\n' + str(df1.index[min( i, len(df1.index)-1 )].strftime('%y-%m-%d')), fontsize=12)import matplotlib.animation as ani
animator = ani.FuncAnimation(fig, getmepie, interval = 200)
plt.show()
Copy the code

The main difference is that the code for a dynamic pie chart returns a set of values each time through the loop, whereas in a linear chart it returns the entire time series up to our point. Returning the time series is achieved by df1.head(I), while.max () ensures that we only get the latest data, because the total number of deaths caused by the epidemic can change in only two ways: to maintain the current number or to continue to increase.

df1.head(i).max(a)Copy the code

Dynamic bar graph

The difficulty of creating a dynamic bar chart is not that different from the previous two cases. In this case, the author defines both horizontal and vertical bar charts, and the reader can choose the chart type and define the variable column according to their actual needs.

fig = plt.figure()
bar = ' 'def buildmebarchart(i=int) :
    iv = min(i, len(df1.index)-1) #the loop iterates an extra one time, which causes the dataframes to go out of bounds. This was the easiest (most lazy) way to solve this :)
    objects = df1.max().index
    y_pos = np.arange(len(objects))
    performance = df1.iloc[[iv]].values.tolist()[0]
    if bar == 'vertical':
        plt.bar(y_pos, performance, align='center', color=['red'.'green'.'blue'.'orange'])
        plt.xticks(y_pos, objects)
        plt.ylabel('Deaths')
        plt.xlabel('Countries')
        plt.title('Deaths per Country \n' + str(df1.index[iv].strftime('%y-%m-%d')))
    else:
        plt.barh(y_pos, performance, align='center', color=['red'.'green'.'blue'.'orange'])
        plt.yticks(y_pos, objects)
        plt.xlabel('Deaths')
        plt.ylabel('Countries')animator = ani.FuncAnimation(fig, buildmebarchart, interval=100)plt.show()
Copy the code

Once you’re done, it’s easy to store these dynamic diagrams using the following code:

animator.save(r'C:\temp\myfirstAnimation.gif')
Copy the code

Interested readers who want to get detailed information can be reference: matplotlib.org/3.1.1/api/a…