import pandas as pd
import matplotlib.pyplot as plt
from highlight_text import fig_text
import matplotlib as mpl
from mplsoccer.pitch import Pitch
#Set general use colors
text_color = 'w'
data = pd.read_csv('shotmaps.csv')
data
minute | second | team | x | y | outcome | |
---|---|---|---|---|---|---|
0 | 21 | 42 | Barcelona | 50.88 | 85.20 | Saved |
1 | 29 | 33 | Barcelona | 34.96 | 91.68 | Missed |
2 | 35 | 3 | Barcelona | 44.40 | 101.64 | Saved |
3 | 41 | 3 | Barcelona | 27.92 | 96.36 | Saved |
4 | 42 | 9 | Barcelona | 50.40 | 104.88 | Saved |
5 | 42 | 13 | Barcelona | 21.60 | 108.00 | Missed |
6 | 45 | 32 | Barcelona | 47.84 | 107.64 | Saved |
7 | 46 | 51 | Barcelona | 47.04 | 89.76 | Saved |
8 | 57 | 31 | Barcelona | 48.64 | 107.04 | Post |
9 | 60 | 12 | Barcelona | 33.76 | 101.64 | Missed |
10 | 64 | 29 | Barcelona | 37.36 | 98.28 | Saved |
11 | 66 | 58 | Barcelona | 50.72 | 107.52 | Missed |
12 | 69 | 12 | Barcelona | 37.36 | 98.28 | Missed |
13 | 69 | 57 | Barcelona | 54.56 | 104.04 | Saved |
14 | 77 | 5 | Barcelona | 52.32 | 89.76 | Saved |
15 | 80 | 9 | Barcelona | 46.24 | 91.44 | Missed |
16 | 82 | 51 | Barcelona | 41.12 | 97.80 | Saved |
17 | 84 | 15 | Barcelona | 27.44 | 87.84 | Missed |
18 | 87 | 51 | Barcelona | 28.88 | 96.36 | Missed |
19 | 89 | 6 | Barcelona | 42.08 | 99.36 | Saved |
fig, ax = plt.subplots(figsize=(13,8.5))
fig.set_facecolor('#22312b')
ax.patch.set_facecolor('#22312b')
#The statsbomb pitch from mplsoccer
pitch = Pitch(pitch_type='statsbomb', orientation='vertical',
pitch_color='#22312b', line_color='#c7d5cc', figsize=(13, 8),
constrained_layout=False, tight_layout=True, view='half')
pitch.draw(ax=ax)
#I invert the axis to make it so I am viewing it how I want
plt.gca().invert_yaxis()
#plot the points, you can use a for loop to plot the different outcomes if you want
plt.scatter(data['x'],data['y'], s=100,c='#ea6969',alpha=.7)
s='Barcelona Shot Chart vs Juventus'
fig_text(s=s,
x=.27,y=.9,
fontfamily='Andale Mono',
highlight_weights=['bold'],
fontsize=24,
color=text_color
)
total_shots = len(df)
fig_text(s=f'Total Shots: {total_shots}',
x=.27, y =.67, fontsize=14,fontfamily='Andale Mono',color=text_color)
fig_text(s=f'xG: .85',
x=.49, y =.67, fontsize=14,fontfamily='Andale Mono',color=text_color)
fig_text(s=f'Goals: 0',
x=.68, y =.67, fontsize=14,fontfamily='Andale Mono',color=text_color)
fig.text(.22,.14,f'@mckayjohns / twitter',fontstyle='italic',fontsize=12,fontfamily='Andale Mono',color=text_color)
#plt.savefig('bcnjuveshots.png',dpi=300,bbox_inches = 'tight',facecolor='#22312b')
Text(0.22, 0.14, '@mckayjohns / twitter')