Bar Chart Race in Python with Matplotlib

~In roughly less than 50 lines of code

Bar Chart Race animation showing the 10 biggest cities in the world

Bar chart races have been around for a while. This year, they took social media by storm. It began with Matt Navarra’s tweet, which was viewed 10 million times. Then, John Burn-Murdoch created reproducible notebook using d3.js, others started creating their races. Then, Flourish studio released race chart for non-programmers. Since then hundreds of races have been shared on the Internet.

Race with Matplotlib

I wondered — How easy would it be to re-produce JBM’s version in Python using Jupyter and Matplotlib? Turns out, in less than 50 lines of code, you can reasonably re-create reusable bar chart race in Python with Matplotlib.

Here’s what we want to create.

Let’s Code

Now, that you’ve seen the output, we’ll incrementally build it up.

Matplotlib ’s style defaults are designed for many common situations, but are in no way optimal for our aesthetics. So, bulk of our code would go into styling (axes, text, colors, ticks etc)

In your Jupyter notebook, import the dependent libraries.

Read the city populations dataset with pandas . We only need 4 columns to work with 'name', 'group', 'year', 'value' . Typically, a name is mapped to a group (city to continent/country) and each year has one value .

Data transformations

We are interested to see top 10 value s are a given year . Using pandas transformations, we will get top 10 values.

Basic chart

Now, let’s plot a basic bar chart. We start by creating a figure and an axes. Then, we use ax.barh(x, y) to draw horizontal barchart.

Notice, highest bar is at the bottom. We need to flip that.

Color, Labels

Next, let’s add values, group labels and colors based on groups. We’ll user colors and group_lk to add color to the bars.

group_lk is mapping between name and group values.

Now, we’re left with styling the chart.

Polish Style

For convenience let’s move our code to draw_barchart function.

We need to style following items:

Text: Update font sizes, color, orientation

Axis: Move X-axis to top, add color & subtitle

Grid: Add lines behind bars

Format: comma separated values and axes tickers

Add title, credits, gutter space

Remove: box frame, y-axis labels

We’ll add another dozen lines of code for this.

We now have an identical chart.

Animate Race

To animate the race, we will use FuncAnimation from matplotlib.animation . FuncAnimatio creates an animation by repeatedly calling a function (that draws on canvas). In our case, that function will be draw_barchart . We also use frames , this argument accepts on what values you want to run draw_barchart – we’ll run from year 1968 to 2018.

And, there, we have it, bar chart race inside a notebook with matplotlib.

You could save the animator object to a video/gif or play within the notebook.

Bonus: xkcd-style!

Turning your matplotlib plots into xkcd styled ones is pretty easy. You can simply turn on xkcd sketch-style drawing mode with plt.xkcd .

Here’s the animated xkcd-styled bar chart race.

DIY

Full code for the race animation is here and you can play with it on Google Colab also. Try changing the dataset, colors and share your races.

Matplotlib is a massive library — being able to adjust every aspect of a plot is powerful but it can be complex / time-consuming for highly customized charts. At least, for these bar chart races, it was fairly quick!