我試圖通過從一個網站上搜刮資料來創建一個射門圖,以尋找足球資料。 我希望主隊的射門在球場的一側,而客隊的射門則在另一側。有什么幫助嗎?我試圖通過使用if h/a = h.... and else....的if陳述句來解決這個問題
。import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from mplsoccer.pitch import Pitch
from pandas.core.indexes.base import Index
text_color = 'w'/span>
data = pd.read_csv('shotmaps.csv')
fig, ax = plt.subplots(figsize=(13,8.5)
fig.set_facecolor('#22312b')
ax.patch.set_facecolor('#22312b')
pitch = Pitch(half=False, positional=True, shade_middle=True, positional_color='black', line_color='black', axis=True, label=True, tick=True)
pitch.draw(ax=ax)
plt.title('曼聯vs紐卡斯爾', fontsize=24, c=text_color)
if data['H/A'].eq('h').any()
plt. scatter(data['x-coordinate']*120, data['y-coordinate']*80, c = '#ea6969', s=70)
if data['H/A'].eq('a').any() 。
plt.gca().invert_xaxis()
plt. scatter(data['x-coordinate']*120, data['y-coordinate']*80, c ='blue', s=70)
這是我從csv中獲取的資料框架:
這是我從csv中獲取的資料框架。
未命名。0 Minute Player Situation Team
0 0 8 Cristiano Ronaldo OpenPlay 曼聯
1 1 10 Cristiano Ronaldo OpenPlay 曼聯
2 2 18 Raphael Varane FromCorner 曼聯
3 3 22 Cristiano Ronaldo OpenPlay 曼聯
4 4 25 Bruno Fernandes OpenPlay 曼聯
5 5 28 Bruno Fernandes OpenPlay 曼聯
6 6 29 Bruno Fernandes OpenPlay Manchester United
7 7 29 Jadon Sancho OpenPlay 曼聯
8 8 31 Jadon Sancho OpenPlay 曼聯
9 9 31 Harry Maguire FromCorner 曼聯
10 10 38 Cristiano Ronaldo OpenPlay 曼聯
11 11 42 Nemanja Matic OpenPlay 曼聯
12 12 46 Mason Greenwood OpenPlay 曼聯
13 13 46 Cristiano Ronaldo OpenPlay 曼聯
14 14 53 Nemanja Matic OpenPlay 曼聯
15 15 58 Bruno Fernandes OpenPlay 曼聯
16 16 61 Cristiano Ronaldo OpenPlay 曼聯
17 17 65 Raphael Varane FromCorner 曼聯
18 18 75 Mason Greenwood FromCorner Manchester United
19 19 79 Bruno Fernandes OpenPlay Manchester United
20 20 91 Jesse Lingard OpenPlay 曼聯
21 21 6 Joelinton OpenPlay Newcastle United
22 22 14 Matt Ritchie OpenPlay Newcastle United
23 23 22 Miguel Almirón OpenPlay NewcastleUnited
24 24 22 Matt Ritchie OpenPlay Newcastle United
25 25 27 Joe Willock OpenPlay NewcastleUnited
26 26 35 Allan Saint-Maximin SetPiece Newcastle United
27 27 55 Javier Manquillo OpenPlay Newcastle United
28 28 66 Joelinton OpenPlay Newcastle United
29 29 68 Allan Saint-Maximin OpenPlay NewcastleUnited
30 30 78 Allan Saint-Maximin OpenPlay Newcastle United
31 31 79 Joe Willock FromCorner Newcastle United
32 32 86 Joelinton OpenPlay Newcastle United
xG結果 x-坐標 y-坐標 H/A
0 0.057108 MissedShots 0.978 0.338 h
1 0.039677 MissedShots 0.972 0.720 h
2 0.219654 MissedShots 0.930 0.616h
3 0.026905 BlockedShot 0.766 0.521 h
4 0.006393 MissedShots 0.494 0.800 h
5 0.009298 MissedShots 0.718 0.804 h
6 0.021526 MissedShots 0.744 0.452 h
7 0.369672 MissedShots 0.892 0.514 h
8 0.128696 BlockedShot 0.851 0.527 h
9 0.042829 SaveedShot 0.905 0.424 h
10 0.108275 BlockedShot 0.940 0.342 h
11 0.014675 BlockedShot 0.732 0.382 h
12 0.015693 SaveedShot 0.802 0.176 h
13 0.887859 Goal 0.976 0.516 h
14 0.015525 MissedShots 0.697 0.488h
15 0.030891 BlockedShot 0.787 0.418 h
16 0.301787 Goal 0.923 0.632 h
17 0.070338 MissedShots 0.905 0.519 h
18 0.027712 BlockedShot 0.862 0.285 h
19 0.031612 Goal 0.756 0.499 h
20 0.369022 目標 0.865 0.522 h
21 0.070895 MissedShots 0.860 0.675 a
22 0.016308 BlockedShot 0.767 0.720 a
23 0.011347 BlockedShot 0.744 0.310 a
24 0.014497 MissedShots 0.784 0.699 a
25 0.068061 MissedShots 0.927 0.330 a
26 0.015108 MissedShots 0.765 0.707 a
27 0.264944 Goal 0.933 0.328 a
28 0.060307 SaveedShot 0.872 0.660 a
29 0.006815 MissedShots 0.698 0.726 a
30 0.020886 BlockedShot 0.739 0.479 a
31 0.013166 SaveedShot 0.706 0.464 a
32 0.059875 MissedShots 0.912 0.593 a
uj5u.com熱心網友回復:
首先,將你的資料框架分成主隊和客隊:
home_team = df.loc[df["H/A"] == "h"]
away_team = df.loc[df["H/A"] == "a"]
現在,讓我們假設主隊正在向左向右進攻。一個X坐標,比如說,0.9表示主隊從距離他們的球門90%的位置射門,這將是在一個0.9 * pitch_length的X坐標。
另一方面,客隊在0.9的位置射門,將表示距離他們球門90%的位置,或距離主隊球門10%的位置。換句話說,x = (1 - 0.9) * pitch_length
那就這么做吧。
plt. scatter(home_team['x-coordinate']*120, home_team['y-coordinate']*80, c ='#ea6969', s=70)
plt. scatter((1-away_team['x-coordinate'])*120, away_team['y-coordinate']*80, c ='blue', s=70)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309190.html
標籤:
