Lollipop Charts in Matplotlib

Matplotlib’s chart functions are quite simple and allow us to create graphics to our exact specification.

The example below will plot the Premier League table from the 16/17 season, taking you through the basics of creating a bar chart and customising some of its features. First of all, let’s get our modules loaded and data in place.

In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#This next line makes our charts show up in the notebook
%matplotlib inline

table = pd.read_csv("../../data/1617table.csv")
table.head()
Out[1]:
Pos Team Pld W D L GF GA GD Pts
0 1 Chelsea 38 30 3 5 85 33 52 93
1 2 Tottenham Hotspur 38 26 8 4 86 26 60 86
2 3 Manchester City 38 23 9 6 80 39 41 78
3 4 Liverpool 38 22 10 6 78 42 36 76
4 5 Arsenal 38 23 6 9 77 44 33 75

The .hlines() argument plots our data in horizontal lines. At its simplest, it needs two arguments, x and height.

  • X – The x coordinate for each bar. For a bar chart, we will most often want evenly spaced bars, so we provide a sequence from 1-20 for a 20 bar chart. ‘np.arange’ provides this sequence easily.
  • Height – How high will each bar go? Or, what is the value of each bar? In this example, we will provide the points column of the table.
In [2]:
plt.hlines(y=np.arange(1,21),xmin=0,xmax=table['Pts'])
Out[2]:
<matplotlib.collections.LineCollection at 0x19771e0b048>
In [3]:
plt.hlines(y=np.arange(1,21),xmin=0,xmax=table['Pts'],color="skyblue")
plt.plot(table['Pts'], np.arange(1,21), "o")
plt.yticks(np.arange(1,21), table['Team'])
plt.show()
In [4]:
#Create an array of equal length to our bars
#Each value is the hex code for the team's colours, in order of our chart
teamColours = ['#034694','#001C58','#5CBFEB','#D00027',
              '#EF0107','#DA020E','#274488','#ED1A3B',
               '#000000','#091453','#60223B','#0053A0',
               '#E03A3E','#1B458F','#000000','#53162f',
               '#FBEE23','#EF6610','#C92520','#BA1F1A']


plt.hlines(y=np.arange(1,21),xmin=0,xmax=table['Pts'],color=teamColours)
plt.plot(table['Pts'], np.arange(1,21), "o")
plt.yticks(np.arange(1,21), table['Team'])

plt.ylabel("Team")
plt.xlabel("Points")

plt.title("Premier League 16/17")

plt.show()

Top work, you’ve created a bar chart! It shows team points evenly spaced and looks great.

To show this, however, we need to add a few more things. Notably, axis labels, a title and bar labels. You can see which commands do this for us in the code below:

That is so much better! Now we can pass this chart to anyone and they can understand it.

It is still a bit… blue, though. Let’s give the bars their team’s colour. First of all, we will need to create an array of team colours using hex codes. We will then map this array to each team. Take a look how below:

And now we have a beautiful, in colour, chart. Exceptional work!

Summary

Lollipop charts are essentially horizontal bar charts, with a circle dotted on the end. As we can see above, fully labelled and properly drawn up charts can make for a nice-looking change from a typical bar chart.

Next up, take a look through some other visualisation types – like radar charts!