Drawing a Football Pitch in Python

Ajay Prakash Nair
3 min readOct 11, 2024

--

Football pitch visualizations are central to football analytics, whether it’s tracking player movements, event data, or building advanced heatmaps. In this post, we’ll explore two methods for drawing a football pitch in Python: manually using matplotlib, and efficiently using the mplsoccer package.

Method 1: Drawing the Pitch Manually Using matplotlib

For those who like more control or just want to understand the process behind visualizing a football pitch, this first method draws the pitch using Python’s matplotlib library.

Steps to Create a Football Pitch:

Step 1: Install the Required Libraries

We will need matplotlib and numpy:

pip install matplotlib numpy

Step 2 : Import the Required Libraries (including Arc)

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Arc # Import Arc from patches

Step 3: Define the Pitch Dimensions

Football pitches typically have dimensions of about 105 meters in length and 68 meters in width, though this can vary slightly by league and stadium. We will define our pitch with these dimensions and add various elements like the goals, penalty areas, and center circle.

Step 4: Function to draw the pitch

We’ll define a function create_pitch to draw the football pitch. This function will use basic matplotlib shapes like lines and circles to create a graphical representation of the pitch.

def create_pitch(length=105, width=68):
"""Create a football pitch using matplotlib"""

# Create figure
fig, ax = plt.subplots(figsize=(7, 5))

# Pitch Outline & Centre Line
plt.plot([0, 0, length, length, 0], [0, width, width, 0, 0], color="black")
plt.plot([length/2, length/2], [0, width], color="black")

# Left Penalty Area
plt.plot([16.5, 16.5, 0, 0, 16.5], [width/2 - 16.5, width/2 + 16.5, width/2 + 16.5, width/2 - 16.5, width/2 - 16.5], color="black")

# Right Penalty Area
plt.plot([length, length - 16.5, length - 16.5, length, length], [width/2 - 16.5, width/2 - 16.5, width/2 + 16.5, width/2 + 16.5, width/2 - 16.5], color="black")

# Left 6-yard Box
plt.plot([5.5, 5.5, 0, 0, 5.5], [width/2 - 5.5, width/2 + 5.5, width/2 + 5.5, width/2 - 5.5, width/2 - 5.5], color="black")

# Right 6-yard Box
plt.plot([length, length - 5.5, length - 5.5, length, length], [width/2 - 5.5, width/2 - 5.5, width/2 + 5.5, width/2 + 5.5, width/2 - 5.5], color="black")

# Prepare Circles
center_circle = plt.Circle((length/2, width/2), 9.15, color="black", fill=False)
center_spot = plt.Circle((length/2, width/2), 0.8, color="black")
left_penalty_spot = plt.Circle((11, width/2), 0.8, color="black")
right_penalty_spot = plt.Circle((length-11, width/2), 0.8, color="black")

# Draw Circles
ax.add_patch(center_circle)
ax.add_patch(center_spot)
ax.add_patch(left_penalty_spot)
ax.add_patch(right_penalty_spot)

# Draw Arcs (penalty areas)
left_arc = Arc((11, width/2), height=18.3, width=18.3, angle=0, theta1=308, theta2=52, color="black")
right_arc = Arc((length-11, width/2), height=18.3, width=18.3, angle=0, theta1=128, theta2=232, color="black")
ax.add_patch(left_arc)
ax.add_patch(right_arc)

# Tidy Axes
plt.xlim(-5, length + 5)
plt.ylim(-5, width + 5)
plt.axis('off')

return fig, ax

# Create the pitch
fig, ax = create_pitch()

# Display the pitch
plt.show()

This method provides full control over the pitch design but can become tedious for more complex visualizations like shot maps or pass maps. For more advanced features and customizations, we can use the mplsoccer library, which simplifies the process.

Method 2: Drawing a Football Pitch Using mplsoccer

If manually creating a football pitch seems daunting or if you prefer an optimized approach, you can use the mplsoccer library. This package simplifies the process and offers additional functionalities for advanced visualizations, like pass maps, shot maps, and more.

Step 1: Install mplsoccer

You can install mplsoccer using pip:

pip install mplsoccer

Step 2: Drawing a Basic Pitch

After installing mplsoccer, you can quickly draw a football pitch using the following code:

from mplsoccer.pitch import Pitch
pitch = Pitch(pitch_color='grass', line_color='white', stripe=True)
fig, ax = pitch.draw()

This creates a full football pitch with a green “grass” background and white lines. You can customize the pitch by changing parameters like pitch_color and line_color.

Conclusion

In this blog, we explored two methods for drawing a football pitch using Python:

  1. Manually using matplotlib, which gives you complete control but is time-consuming for more complex visualizations.
  2. Using mplsoccer, which simplifies the process and offers advanced customization options.

For quick and sophisticated visualizations, mplsoccer is highly recommended. It’s powerful enough to create more advanced visualizations like pass maps and shot maps, which we will explore in future posts. Keep an eye on the mplsoccer documentation to discover more features!

--

--

Ajay Prakash Nair
Ajay Prakash Nair

No responses yet