I have created a shot map using matplotlib, it is currently not interactive and just a static image of all the shots a player has taken (goal, blocked, saved, missed)
The code I used as reference to create the pitch map can be found here: https://fcpython.com/visualisation/drawing-pitchmap-adding-lines-circles-matplotlib
Below is the python code used to plot the shot map:
# Plotting Shots Horizontal Full Pitch
draw_pitch("#3E3E40","#faf0e6","horizontal","full")
x = df_salah['XM'].tolist()
y = df_salah['YM'].tolist()
y1 = [68 - i for i in y]
## Add Z variable for xG
z = df_salah['xG'].tolist()
z1 = [500 * i for i in z] # This is to scale the "xG" values for plotting
## Add small legend in the bottom corner
mSize = [0.05,0.10,0.2,0.4,0.6,1]
mSizeS = [500 * i for i in mSize]
mx = [5.5,7,9,11,13.5,16.5]
my = [60,60,60,60,60,60]
colors = {'Goal':'Green', 'MissedShots':'Purple', 'BlockedShot':'Red', 'SavedShot':'Blue', 'ShotOnPost':'Yellow'}
## markers = {'Goal':'Star', 'MissedShots':'X', 'BlockedShot':'O', 'SavedShot':'V', 'ShotOnPost':'S'}
plt.text(11,55,"xG", color="white", ha="center",va="center", zorder=zo, fontsize=16)
plt.scatter(x,y,s=z1, marker='o',color=df_salah['result'].map(colors),edgecolors="black", zorder=zo)
plt.scatter(mx,my,s=mSizeS,facecolors="white", edgecolor="white",zorder=zo)
plt.plot([5.5,17], [57,57],color="white",lw=2,zorder=zo)
i = 0
for i in range(len(mx)):
plt.text(mx[i], my[i], mSize[i], fontsize=mSize[i]*18, color="#195905", zorder=zo,ha="center", va="center")
## Title
plt.title("Mohamed Salah Shots 19/20 Season")
plt.show()
What I want to do is make this interactive by adding filters (widgets) so I can filter for type of shot taken so the map only reveals that particular type of shot on the map e.g. If I filter for "goals" then the map should show only shots that lead to a goal, etc.
I also want to add the "hover" feature from plotly where if you hover over the shot it will tell you the "xG" value, time of shot etc.
When using plotly, how do I keep the pitch map as I can't figure that out. I tried the following which resulted in:
trace1 = px.scatter(df_salah, x='XM', y='YM', color='result')
fig = go.FigureWidget(trace1)
I also want to keep the scaling of of the circles
Could someone help start me off with the code as I am a bit lost on how to do this or if I'm even using the correct libraries/platform?