-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw_football_pitch.py
162 lines (124 loc) · 4.38 KB
/
draw_football_pitch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Function written by Peter McKeever
# Source: http://petermckeever.com/2020/10/how-to-draw-a-football-pitch/
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc
def draw_pitch(x_min=0,
x_max=105,
y_min=0,
y_max=68,
pitch_color='w',
line_color='grey',
line_thickness=1.5,
point_size=20,
orientation='horizontal',
aspect='full',
ax=None):
if not ax:
raise TypeError('This function is intended to be used with an existing fig and ax in order to allow flexibility in plotting of various sizes and in subplots.')
if orientation.lower().startswith('h'):
first = 0
second = 1
arc_angle = 0
if aspect == 'half':
ax.set_xlim(x_max / 2, x_max + 5)
elif orientation.lower().startswith('v'):
first = 1
second = 0
arc_angle = 90
if aspect == 'half':
ax.set_ylim(x_max / 2, x_max + 5)
else:
raise NameError('You must choose one of horizontal or vertical')
ax.axis('off')
rect = plt.Rectangle((x_min, y_min),
x_max,
y_max,
facecolor=pitch_color,
edgecolor='none',
zorder=-2)
ax.add_artist(rect)
x_conversion = x_max / 100
y_conversion = y_max / 100
pitch_x = [0, 5.8, 11.5, 17, 50, 83, 88.5, 94.2, 100] # pitch x markings
pitch_x = [x * x_conversion for x in pitch_x]
pitch_y = [0, 21.1, 36.6, 50, 63.2, 78.9, 100] # pitch y markings
pitch_y = [x * y_conversion for x in pitch_y]
goal_y = [45.2, 54.8] # goal posts
goal_y = [x * y_conversion for x in goal_y]
# side and goal lines
lx1 = [x_min, x_max, x_max, x_min, x_min]
ly1 = [y_min, y_min, y_max, y_max, y_min]
# outer boxed
lx2 = [x_max, pitch_x[5], pitch_x[5], x_max]
ly2 = [pitch_y[1], pitch_y[1], pitch_y[5], pitch_y[5]]
lx3 = [0, pitch_x[3], pitch_x[3], 0]
ly3 = [pitch_y[1], pitch_y[1], pitch_y[5], pitch_y[5]]
# goals
lx4 = [x_max, x_max + 2, x_max + 2, x_max]
ly4 = [goal_y[0], goal_y[0], goal_y[1], goal_y[1]]
lx5 = [0, -2, -2, 0]
ly5 = [goal_y[0], goal_y[0], goal_y[1], goal_y[1]]
# 6 yard boxes
lx6 = [x_max, pitch_x[7], pitch_x[7], x_max]
ly6 = [pitch_y[2], pitch_y[2], pitch_y[4], pitch_y[4]]
lx7 = [0, pitch_x[1], pitch_x[1], 0]
ly7 = [pitch_y[2], pitch_y[2], pitch_y[4], pitch_y[4]]
# Halfway line, penalty spots, and kickoff spot
lx8 = [pitch_x[4], pitch_x[4]]
ly8 = [0, y_max]
lines = [
[lx1, ly1],
[lx2, ly2],
[lx3, ly3],
[lx4, ly4],
[lx5, ly5],
[lx6, ly6],
[lx7, ly7],
[lx8, ly8],
]
points = [[pitch_x[4], pitch_y[3]]]
circle_points = [pitch_x[4], pitch_y[3]]
arc_points1 = [pitch_x[6], pitch_y[3]]
arc_points2 = [pitch_x[2], pitch_y[3]]
for line in lines:
ax.plot(line[first],
line[second],
color=line_color,
lw=line_thickness,
zorder=-1)
for point in points:
ax.scatter(point[first],
point[second],
color=line_color,
s=point_size,
zorder=-1)
circle = plt.Circle((circle_points[first], circle_points[second]),
x_max * 0.088,
lw=line_thickness,
color=line_color,
fill=False,
zorder=-1)
ax.add_artist(circle)
arc1 = Arc((arc_points1[first], arc_points1[second]),
height=x_max * 0.088 * 2,
width=x_max * 0.088 * 2,
angle=arc_angle,
theta1=128.75,
theta2=231.25,
color=line_color,
lw=line_thickness,
zorder=-1)
ax.add_artist(arc1)
arc2 = Arc((arc_points2[first], arc_points2[second]),
height=x_max * 0.088 * 2,
width=x_max * 0.088 * 2,
angle=arc_angle,
theta1=308.75,
theta2=51.25,
color=line_color,
lw=line_thickness,
zorder=-1)
ax.add_artist(arc2)
ax.set_aspect('equal')
return ax