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 .bar() argument plots our data. 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.
plt.bar(x=np.arange(1,21),height=table['Pts'])
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:
#Create our bar chart as before
plt.bar(x=np.arange(1,21),height=table['Pts'])
#Give it a title
plt.title("Premier League 16/17")
#Give the x axis some labels across the tick marks.
#Argument one is the position for each label
#Argument two is the label values and the final one is to rotate our labels
plt.xticks(np.arange(1,21), table['Team'], rotation=90)
#Give the x and y axes a title
plt.xlabel("Team")
plt.ylabel("Points")
#Finally, show me our new plot
plt.show()
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:
#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']
#Add a new argument, color, to our 'plt.bar()' method
#This argument passes our teamColours array
plt.bar(x=np.arange(1,21),height=table['Pts'],color = teamColours)
#Label bars, axes and the chart as before
plt.title("Premier League 16/17")
plt.xticks(np.arange(1,21), table['Team'], rotation=90)
plt.xlabel("Team")
plt.ylabel("Points")
plt.show()
And now we have a beautiful, in colour, chart. Exceptional work!
Summary
While the table is the customary way to display team performance over a season, it hides a lot of information that we struggle to visualise as numbers. When we plot points onto a chart we can see differences between teams much more easily.
We used matplotlib’s ‘.bar()’ tool to create a simple barchart, then to add titles, axes labels and even colour to make something that we can present easily.
Next up, take a look at another way to present this data with a lollipop chart.