How to draw multiple football pitches for teams comparison

Hitesh Gautam
2 min readOct 12, 2021

--

Today we’ll walk through creating multiple football pitches on a single plot in python using Matplotlib.

For drawing a single pitch refer to the following — http://petermckeever.com/2020/10/how-to-draw-a-football-pitch/

Let’s start with first importing the required packages.

Functional code derived from Peter McKeever’s code repo

Laying out Axes in a Figure in a non uniform grid can be both tedious and verbose. For dense, even grids we have Figure.subplots but for more complex layouts, such as Axes that span multiple columns / rows of the layout or leave some areas of the Figure blank, you can use gridspec.GridSpec or manually place your axes.

Figure.subplot_mosaic aims to provide an interface to visually lay out your axes as a nested lists to streamline this process. This interface naturally supports naming your axes. Figure.subplot_mosaic returns a dictionary keyed on the labels used to lay out the Figure. By returning data structures with names, it is easier to write plotting code that is independent of the Figure layout.

To read more check the following link — https://matplotlib.org/stable/tutorials/provisional/mosaic.html#sphx-glr-tutorials-provisional-mosaic-py

Now we need to define our drawing board for subplots. Variable: main. Define layout and need to set height_ratios for each title and subplots. Adding subplot fig,axd subplot_mosaic.

Looping over ax which are not ‘title’ and ‘annot’ and calling draw_pitch function

In the end, assign text to title and annot.

I hope you found it useful. I don’t think there’s a massive point in creating your own functions for drawing pitches as some really good packages have emerged. This will help you in your knowledge of Matplotlib. If you are interested in using some form of the above code in packages, feel free, but please credit when using. This is the first tutorial I have written in a long time.

If there’s anything you would like me to cover you can reach me over on Twitter.

Stay subscribed for the second part of this tutorial where we show some classical analysis over the plot.

--

--