Skip to content

Commit 079037c

Browse files
Update utility_function_viz.py
1 parent 8757819 commit 079037c

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

07. Passing Networks/01. Basic Passing Networks/utility_function_viz.py

+179
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,184 @@
33
Created on Tue May 12 20:27:24 2020
44
55
@author: slothfulwave612
6+
7+
Python module for visualization.
8+
9+
Modules Used(1):-
10+
1. matplotlib -- plotting library.
611
"""
712

13+
import matplotlib.pyplot as plt
14+
from matplotlib.patches import Arc
15+
import matplotlib.patches as patches
16+
17+
def createPitch(length,width, unity,linecolor, fig, ax): # in meters
18+
# Code by @JPJ_dejong
19+
20+
"""
21+
creates a plot in which the 'length' is the length of the pitch (goal to goal).
22+
And 'width' is the width of the pitch (sideline to sideline).
23+
Fill in the unity in meters or in yards.
24+
"""
25+
#check boundaries again
26+
if length <= 95:
27+
return(str("Didn't you mean meters as unity?"))
28+
29+
elif length >= 131 or width >= 101:
30+
return(str("Field dimensions are too big. Maximum length is 130, maximum width is 100"))
31+
32+
#Run program if unity and boundaries are accepted
33+
else:
34+
#Pitch Outline & Centre Line
35+
ax.plot([0,0],[0,width], color=linecolor)
36+
ax.plot([0,length],[width,width], color=linecolor)
37+
ax.plot([length,length],[width,0], color=linecolor)
38+
ax.plot([length,0],[0,0], color=linecolor)
39+
ax.plot([length/2,length/2],[0,width], color=linecolor)
40+
41+
## the following lines of code will create
42+
## the goal-post at both side of the pitch
43+
ax.plot([-3,0], [(width/2)-5,(width/2)-5], color=linecolor)
44+
ax.plot([-3,0], [(width/2)+5,(width/2)+5], color=linecolor)
45+
ax.plot([-3,-3], [(width/2)-5,(width/2)+5], color=linecolor)
46+
ax.plot([length+3,length+3], [(width/2)-5,(width/2)+5], color=linecolor)
47+
ax.plot([length,length+3], [(width/2)-5,(width/2)-5], color=linecolor)
48+
ax.plot([length,length+3], [(width/2)+5,(width/2)+5], color=linecolor)
49+
50+
#Left Penalty Area
51+
ax.plot([18 ,18],[(width/2 +18),(width/2-18)],color=linecolor)
52+
ax.plot([0,18],[(width/2 +18),(width/2 +18)],color=linecolor)
53+
ax.plot([18,0],[(width/2 -18),(width/2 -18)],color=linecolor)
54+
55+
#Right Penalty Area
56+
ax.plot([(length-18),length],[(width/2 +18),(width/2 +18)],color=linecolor)
57+
ax.plot([(length-18), (length-18)],[(width/2 +18),(width/2-18)],color=linecolor)
58+
ax.plot([(length-18),length],[(width/2 -18),(width/2 -18)],color=linecolor)
59+
60+
#Left 6-yard Box
61+
ax.plot([0,6],[(width/2+7.32/2+6),(width/2+7.32/2+6)],color=linecolor)
62+
ax.plot([6,6],[(width/2+7.32/2+6),(width/2-7.32/2-6)],color=linecolor)
63+
ax.plot([6,0],[(width/2-7.32/2-6),(width/2-7.32/2-6)],color=linecolor)
64+
65+
#Right 6-yard Box
66+
ax.plot([length,length-6],[(width/2+7.32/2+6),(width/2+7.32/2+6)],color=linecolor)
67+
ax.plot([length-6,length-6],[(width/2+7.32/2+6),width/2-7.32/2-6],color=linecolor)
68+
ax.plot([length-6,length],[(width/2-7.32/2-6),width/2-7.32/2-6],color=linecolor)
69+
70+
#Prepare Circles; 10 yards distance. penalty on 12 yards
71+
centreCircle = plt.Circle((length/2,width/2),10,color=linecolor,fill=False)
72+
centreSpot = plt.Circle((length/2,width/2),0.8,color=linecolor)
73+
leftPenSpot = plt.Circle((12,width/2),0.8,color=linecolor)
74+
rightPenSpot = plt.Circle((length-12,width/2),0.8,color=linecolor)
75+
76+
#Draw Circles
77+
ax.add_patch(centreCircle)
78+
ax.add_patch(centreSpot)
79+
ax.add_patch(leftPenSpot)
80+
ax.add_patch(rightPenSpot)
81+
82+
#Prepare Arcs
83+
leftArc = Arc((11,width/2),height=20,width=20,angle=0,theta1=312,theta2=48,color=linecolor)
84+
rightArc = Arc((length-11,width/2),height=20,width=20,angle=0,theta1=130,theta2=230,color=linecolor)
85+
86+
#Draw Arcs
87+
ax.add_patch(leftArc)
88+
ax.add_patch(rightArc)
89+
90+
#Tidy Axes
91+
plt.axis('off')
92+
93+
return fig,ax
94+
95+
def draw_lines(ax, lines, cosmetics):
96+
'''
97+
Function for drawing lines for passes between players.
98+
'''
99+
for x, y, end_x, end_y in lines:
100+
dx = end_x - x
101+
dy = end_y - y
102+
103+
attributes = {
104+
'x': x,
105+
'y': y,
106+
'dx': dx,
107+
'dy': dy
108+
}
109+
110+
ax.add_patch(patches.FancyArrow(**attributes, **cosmetics))
111+
112+
return ax
113+
114+
def draw_points(ax, shots):
115+
'''
116+
Function for drawing points for each player's position.
117+
'''
118+
119+
cosmetics = {
120+
'linewidth': 2,
121+
'facecolor': (0, 0, 1, 1),
122+
'edgecolor': (0, 0, 0, 1),
123+
'radius': 1.5
124+
}
125+
126+
for x, y in shots:
127+
attributes = {
128+
'xy': (x, y)
129+
}
130+
ax.add_patch(patches.Circle(**attributes, **cosmetics))
131+
132+
return ax
133+
134+
def show_lines(ax, lines, weights, weight_adj, fill_adj):
135+
'''
136+
Function to draw the pass lines.
137+
'''
138+
for i, e in enumerate(lines):
139+
cosmetics = {
140+
'width': weight_adj(weights[i]),
141+
'head_width': 0,
142+
'head_length': 0,
143+
'facecolor': (0, 0, 1, fill_adj(weights[i])),
144+
'edgecolor': (0, 0, 0, 0)
145+
}
146+
if weights[i] > 5:
147+
ax = draw_lines(ax, [e], cosmetics=cosmetics)
148+
149+
return ax
150+
151+
def draw_numbers(ax, avg_location, players):
152+
'''
153+
Function for drawing number for each player's position.
154+
'''
155+
for k, v in avg_location.items():
156+
jersey = players[k]['jersey']
157+
x,y = v
158+
159+
ax.text(x, y,
160+
jersey, fontsize=12,
161+
ha='center', va='center',
162+
color='white')
163+
164+
return ax
165+
166+
def label_players(ax):
167+
'''
168+
Function for labeling the players.
169+
'''
170+
## adding title
171+
ax.text(60, 82, 'Barcelona\'s Passinig Network || Barca vs Real Madrid (Nov 2010)', fontsize=15)
172+
173+
## adding labels
174+
ax.text(127, 80, '1 - Victor Valdes', fontsize=15)
175+
ax.text(127, 75, '2 - Dani Alves', fontsize=15)
176+
ax.text(127, 70, '3 - Gerard Pique', fontsize=15)
177+
ax.text(127, 65, '5 - Carles Puyol', fontsize=15)
178+
ax.text(127, 60, '6 - Xavi Hernandez', fontsize=15)
179+
ax.text(127, 55, '7 - David Villa', fontsize=15)
180+
ax.text(127, 50, '8 - Andres Iniesta', fontsize=15)
181+
ax.text(127, 45, '10 - Lionel Messi', fontsize=15)
182+
ax.text(127, 40, '16 - Sergio Busquets', fontsize=15)
183+
ax.text(127, 35, '17 - Pedro Rodriguez', fontsize=15)
184+
ax.text(127, 30, '22 - Eric Abidal', fontsize=15)
185+
186+
return ax

0 commit comments

Comments
 (0)