A fun sports one this week. We will be looking to recreate the player metrics chart that you see a lot of the sports viz community make, especially for football (soccer). Typically these are made using python, but we will create the output in Tableau.
I can’t and won’t take any credit for this as it builds on the foundation of two fantastic data community members. The first is Anmol Durgapal, who can be found on Twitter here. I’m hugely grateful for Anmol’s help with the code I needed to create the blank chart in python. The original tutorial from Anmol can be found under Basic Pizza Plot (Percentiles).
Side note, Mplsoccer have a whole range of python example tutorials that you can work your way through if this is of interest!
The second, where the majority of the calculations come from is Ruth A, who provides a chart template for a coxcomb chart. I’ll explain a few amendments to the calculations made later on.
THE PYTHON CODE (OPTIONAL)
The code used for the tutorial can be found on Github I used PyCharm Community to run the code but feel free to use any preference of your own. We will be using the python code simply to create the background for our visualisation. If you would like to gloss over the python aspect.
A couple of housekeeping notes for the code:
You may need to install matplotlib and the mplsoccer package.
If you want your chart to have no labels you can use params = [“”]*12
If you want to amend the number of segments you can amend the params and values calculation, that currently has *12.
You can also amend the colour Hex Codes, based on what colour you want your viz background, circle background and circle lines.
I recommend keeping the figure size the same, as it will help with our build.
Once you’ve finalised these steps – run the code.
A graph should pop up. We will want to save this down in its current state (Ctrl + S), (Do not resize!), to use later as a background.
A copy of the template can be found on github or on my Tableau Public page following the links at the top of the page. We create a custom join between 1 and 1. Within the data you will want to amend the ‘Data’ Sheet. Make the measure out of 100, and the metric names anything of your choosing. If you’d like to use football players stats, values can be found on fbref website (supplied by StatsBomb). For example here is England legend, Raheem Sterling’s stats.
Leave To_Pad as it is, in short this helps us create each of the polygons using a bin, giving the curvature to the edges of our visualisation.
I’m going to skip over most the calculations as we will be simply replacing the dataset. If you want to learn more about the building phase itself, check out this well detailed tableau tutorial by AIEngineering. Alternatively if you want to build the visualisation from scratch, it follows the same methodology as Ruth Amarteifio in this youtube video.
Some calculations I have amended from Ruth’s template and reasons why:
Category Angle – I’ve amended the number to -4.5 in the calculation. Reason being the value here will depend on the number of segment slices. You will need to adjust this value if you create a background with less/more segments. As it will rotate the categories forwards/backwards to fit.
(For example this will be -4 if you have 10 slices)
…Imagine the equivalent is : The scenario of trying to align your plastic wedges to the playing piece in the game trivial pursuit.
Radius – I’ve completely amended this calculation to fit based on the sizing of the python chart export. What this calculation now does is takes the value from the dataset out of 1 and rescales it by 5.4 (The radius of the circle). I make a small adjustment of 0.27 to account for the middle section of the circle that isn’t usually covered if creating the same graph in Python.
X & Y – These both follow the same logic as in Ruth’s tutorial. I’ve only made two minor amendments. The first is reversing the Y axis to allow my categories to go in the order I need, by putting a minus sign in front of the calculation. The second is by making small adjustments to where the visualisation sits in respect to a centre point.
THE BUILD (STEPS TO FOLLOW)
If you didn’t want to try the tutorial, I have saved down a few backgrounds you can try out in the repository. Here is an example of me changing the current template to Raheem Sterling’s metrics. Do note, If you’d like to understand more on what the metrics mean, please visit the website. They are solely used here for illustrative purposes.
- Download and open the template from the top of the page.
- Open Pycharm, Make amendments and Run the python code with the chosen field names and colours.
- Save the output picture down to a local area.
- Replace the dataset from github with your new dataset. I’ve taken the stats from here.
- Refresh the data if not automatic.
- Open the chart sheet and click on the sheet.
- Go to Maps – Background Images – datasetname.
- Edit the chart to have the following components for X and Y. In Options, Leave lock aspect and always show viz both turned on.
6. Finally, Right click on the colours tab and go to sort. Make sure they are in the same order in the list clockwise as they are in your visualisation.
And there we have it. The final visualisation with the chart background snapped on.
HINTS, TRICKS AND HOUSEKEEPING
Make sure your max value is 100. If you have raw values create a new column in the original data and then use the value column as a percent of 100, and bring in the raw value as a tooltip.
If you want to use a different number of segments, you will want amend the above calculations of Category Angle.
The only other last thing to make sure is you save your background image down correctly. When the Python script runs, and the graph pops up. Hit (Ctrl + S) to save the file in its current figure dimensions. This will mean when we import it as a background into Tableau, everything is sized accordingly.
Try playing around with different segment numbers.
Try replacing the circle in the middle with a shape of a players face.
Try adding in extra columns of detail into dataset for hover functionality.
As always, Let me know how you get on with this one. I can be reached on Twitter. A simple one in terms of data prep, but maybe a little tougher in terms of getting the background to snap on correctly.