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.
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()
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.
<matplotlib.collections.LineCollection at 0x19771e0b048>
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()
#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!
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!