Bar Chart Race Tutorial

bar chart race

In this article, you’ll learn how to create a bar chart race animation such as the one above using the matplotlib data visualization library in python. Bar chart races have been around for quite some time. This year, they’ve taken social media by storm. Now that you’ve seen the result, let’s build it up gradually.

What is a bar chart race?

A chart race is an animated sequence of bars that show data values at different moments in time. The bars re-position themselves at each time period so that they remain in order.

The idea behind a chart race is to create a transition of bars, that moves slowly to their new respective positions and allows the user to easily track their movement.

Lets start by importing the libraries

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.animation as animation
from IPython.display import HTML

Download the Data set

df = pd.read_csv('city_populations.csv', usecols=['name', 'group', 'year', 'value'])

Color and Labels

Here, I will use colors and group_lk methods to add color to the bars.

colors = dict(zip(
    ["India", "Europe", "Asia", "Latin America", "Middle East", "North America", "Africa"],
    ["#adb0ff", "#ffb3ff", "#90d595", "#e48381", "#aafbff", "#f7bb5f", "#eafb50"]
group_lk = df.set_index('name')['group'].to_dict()
fig, ax = plt.subplots(figsize=(15, 8))

def draw_barchart(current_year):
    dff = df[df['year'].eq(current_year)].sort_values(by='value', ascending=True).tail(10)
    ax.barh(dff['name'], dff['value'], color=[colors[group_lk[x]] for x in dff['name']])
    dx = dff['value'].max() / 200
    for i, (value, name) in enumerate(zip(dff['value'], dff['name'])):
        ax.text(value-dx, i,     name,           size=14, weight=600, ha='right', va='bottom')
        ax.text(value-dx, i-.25, group_lk[name], size=10, color='#444444', ha='right', va='baseline')
        ax.text(value+dx, i,     f'{value:,.0f}',  size=14, ha='left',  va='center')
    ax.text(1, 0.4, current_year, transform=ax.transAxes, color='#777777', size=46, ha='right', weight=800)
    ax.text(0, 1.06, 'Population (thousands)', transform=ax.transAxes, size=12, color='#777777')
    ax.tick_params(axis='x', colors='#777777', labelsize=12)
    ax.margins(0, 0.01)
    ax.grid(which='major', axis='x', linestyle='-')
    ax.text(0, 1.15, 'The most populous cities in the world from 1500 to 2018',
            transform=ax.transAxes, size=24, weight=600, ha='left', va='top')
    ax.text(1, 0, 'by @thecleverprogrammer; credit @Amankharwal', transform=ax.transAxes, color='#777777', ha='right',
            bbox=dict(facecolor='white', alpha=0.8, edgecolor='white'))


Now I will use the FuncAnimation from matplotlib.animation to animate the bar chart.

fig, ax = plt.subplots(figsize=(15, 8))
animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1900, 2019))
bar chart race

I hope you liked this article on Bar Chart Race tutorial. Feel free to ask your questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1501

Leave a Reply