Treemaps are visualisations that split the area of our chart to display the value of our datapoints. At their simplest, they display shapes in sizes appropriate to their value, so bigger rectangles represent higher values. Python allows us to create these charts quite easily, as it will calculate the size of each rectangle for us and plot it in a way that fits. In addition to this, we can combine our treemap with the matplotlib library’s ability to scale colours against variables to make good looking and easy to understand plots with Python.
Let’s fire up our libraries (make sure you install squarify!) and take a look at our data:
import matplotlib import matplotlib.pyplot as plt import pandas as pd import squarify
data = pd.read_csv("Data/ManCityATT.csv") data.head()
|4||De Bruyne, Kevin||M||24||24||2060||6||10||28||61||1||0|
Our dataframe has a record for each player in Manchester City’s squad, with their games/minutes played alongside goals, assists, shots and card data.
Facing Manchester City next week, we would like to visualise where their threat comes from and who they rely on for goals and assists. We’ll do this with our treemap!
We will create our treemap in a few key steps:
- Create a new dataframe that contains only players that have scored.
- Utilise matplotlib to create a colour map that assigns each player a colour according to how many goals they have scored.
- Set up a new, rectangular plot for our heatmap
- Plot our data & title
- Show the plot, with no axes
The commented code below will show you exactly how you can do this:
# New dataframe, containing only players with more than 0 goals. dataGoals = data[data["G"]>0] #Utilise matplotlib to scale our goal numbers between the min and max, then assign this scale to our values. norm = matplotlib.colors.Normalize(vmin=min(dataGoals.G), vmax=max(dataGoals.G)) colors = [matplotlib.cm.Blues(norm(value)) for value in dataGoals.G] #Create our plot and resize it. fig = plt.gcf() ax = fig.add_subplot() fig.set_size_inches(16, 4.5) #Use squarify to plot our data, label it and add colours. We add an alpha layer to ensure black labels show through squarify.plot(label=dataGoals.Player,sizes=dataGoals.G, color = colors, alpha=.6) plt.title("Man City Goals",fontsize=23,fontweight="bold") #Remove our axes and display the plot plt.axis('off') plt.show()
Let’s take another look, this time creating a treemap for assists. See if you can understand what we are doing without the comments above!
dataAssists = data[data["A"]>0] norm = matplotlib.colors.Normalize(vmin=min(dataAssists.A), vmax=max(dataAssists.A)) colors = [matplotlib.cm.Blues(norm(value)) for value in dataAssists.A] fig = matplotlib.pyplot.gcf() fig.set_size_inches(16, 4.5) fig = plt.gcf() fig.set_size_inches(16, 4.5) squarify.plot(label=dataAssists.Player,sizes=dataAssists.A, color = colors, alpha=.6) plt.title("Man City Assists",fontsize=23,fontweight="bold") plt.axis('off') plt.show()
Awesome, we now have a couple of simple charts that show the dangerous players in City’s lineups, and the code to reproduce these for other teams. This should make for a quick, easy and impactful addition for your pre-match reports!