Creating bowler’s pitch map in Python(Cricket)

Nishant Singh
4 min readApr 9, 2021

--

Using Matplotlib and Seaborn Libraries

As analytics in sports is thriving, with different tools being used to analyze teams and player’s performances. One such sport is Cricket and studying an opposition team and its players are paramount to prepare for a game. Sports broadcasters use several infographics to show the statistics of a player, one such infographic is Bowler’s pitch map which represents the areas of the pitch where the bowler has pitched their deliveries. In this project, I have recreated the bowler’s pitch map with the help of libraries like Matplotlib and Seaborn in python.

Plotting Cricket Pitch

The dimensions of a cricket pitch are 22.56 meters in length and 3. 66 meters in width with the turf strip(where the majority of the bowls are pitched) 2.66 in width. I created the below function to create a pitch map by taking the constant length and width.

spadl_config = {
“length”: 3.66,
“width”: 22.56,
“origin_x”: 0,
“origin_y”: 0,
}
plt.rcParams[‘figure.figsize’] = [2, 6]
def _plot_rectangle(x1, y1, x2, y2, ax, color):
ax.plot([x1, x1], [y1, y2], color=color)
ax.plot([x2, x2], [y1, y2], color=color)
ax.plot([x1, x2], [y1, y1], color=color)
ax.plot([x1, x2], [y2, y2], color=color)
ax.set_facecolor(“green”)
def pitch(
ax=None,
linecolor=”green”,
fieldcolor=None,
alpha=1,
figsize=None,
pitch_config=spadl_config,
show=True,
):
cfg = pitch_config

# Create figure
if ax is None:
fig = plt.figure()
ax = fig.gca()
# Pitch Outline
x1, y1, x2, y2 = (
cfg[“origin_x”],
cfg[“origin_y”],
cfg[“origin_x”] + cfg[“length”],
cfg[“origin_y”] + cfg[“width”],
)
_plot_rectangle(x1, y1, x2, y2, ax=ax, color=linecolor)#lower crease rectangle
x1 = 0.51
x2 = 3.15
y1 = 1.22
y2 = 2.44
_plot_rectangle(x1, y1, x2, y2, ax=ax, color=’white’)
# upper crease rectangle
x1 = 0.51
x2 = 3.15
y1 = 20.12
y2 = 21.34
_plot_rectangle(x1, y1, x2, y2, ax=ax, color=’white’)
plt.axvline(x = 0.51, color = ‘white’)
plt.axvline(x = 3.15, color = ‘white’)
plt.axhline(y = 20.12, color = ‘white’, linestyle = ‘-’)
plt.axhline(y = 2.44, color = ‘white’, linestyle = ‘-’)
# plt.axis(“off”)if show:
plt.show()
return ax

Data for plotting

This is a tricky part since unlike Football, where the coordinates data of the ball during a match is made available by few companies like Opta, Statsbomb, etc. for Cricket no such data is available. So I created dummy data of a bowler’s deliveries with features 4 features: ball x coordinate on the pitch, bally coordinate on the pitch, is the ball hitting the wicket(Boolean value), and speed at which the delivery was bowled with the help of Python’s library Numpy which allows you to created normally distributed random values at a particular mean and Standard deviation. The sample data below:

Scatter and Kde Plots using Seaborn

The last step was to plot the x and y coordinates of the ball on the pitch created above which can be plotted as follows

pitch(show=False)
sns.scatterplot(balls_xy.iloc[:,0],balls_xy.iloc[:,1])

Seaborn also allows us to give one more variable for grouping variables that will produce points with different colors which is hue. After giving hue as Wickets_hitting or Not_hitting column, we get the following result:

Lastly, added the color bar on speed variation with the following line of code:

ax = sns.scatterplot(balls_xy.iloc[:,0],balls_xy.iloc[:,1], hue = balls_xy.iloc[:,3],palette=’hot’)norm = plt.Normalize(balls_xy.iloc[:,3].min(), balls_xy.iloc[:,3].max())
sm = plt.cm.ScalarMappable(cmap=”hot”, norm=norm)
sm.set_array([])
# Remove the legend and add a colorbar
ax.get_legend().remove()
ax.figure.colorbar(sm)

This code will be a good starting point for those interested in Cricket analytics, given that such data is available in the future, and also there is scope for the addition of more things in the plot such as: marking good, short, slot areas on the map and calculating deliveries percentages for the same.

Github link: https://github.com/iambolt/cricket_pitch

--

--