COSC 1210 · CS I (Python) · Matplotlib

GapMinder in Motion: 55 Years of World Development

This is the plot you build in the matplotlib lecture — life expectancy against GDP per capita, one bubble per country, size for population, color for continent — but for every fifth year from 1952 to 2007 instead of 2007 only. Press play. Click a bubble to follow a country. Below the chart is the Python code that draws exactly the frame you see.
Figure · drawn the way plt.savefig would draw it
1952
19521967198219972007
click a continent to hide it
Stories in the data · predict first, then play
Pick a story, or just press Play and watch the bubbles move up and to the right.
Python · the code for this frame

Lines marked in teal change with the chart: the year, hidden continents, followed countries. It is the code from the lecture plus one new line, df = gm[gm['year'] == year], which keeps only the rows of one year. The data file has the same columns as the lecture's 2007 file. Between two data years the page blends the bubbles smoothly; Python only knows the 12 data years.

Make it move in Python

Put the drawing code into a function of year (it still uses gm and continent_colors from the code above). Then FuncAnimation calls that function once for every year in frames and glues the pictures together into an animation. It needs the same fixed axis limits for every frame, or the axes would jump around.

In Google Colab, show the animation in the notebook with from IPython.display import HTML and HTML(anim.to_jshtml()), or save it as a GIF with anim.save("gapminder.gif").

Keyboard: space play/pause, ← → previous/next year. Data: Gapminder, 142 countries, every fifth year 1952–2007 (the classic dataset; the lecture uses its 2007 rows).

Built to accompany COSC 1210 (CS I, Python), lecture “Basic Plots with Matplotlib”. Every frame is drawn live in your browser from the data, with the lecture's settings: s = 2 * pop / 1e6, alpha=0.6, the continent colors, a log x-axis with ticks 1k, 10k, 100k. The idea of the animated bubble chart comes from Hans Rosling's Gapminder.