【发布时间】:2026-01-13 02:40:01
【问题描述】:
我使用 matplotlib 创建了一个射门图,它目前不是交互式的,只是球员所有射门的静态图像(进球、阻挡、扑救、错过)
我用来创建音高图的代码可以在这里找到:https://fcpython.com/visualisation/drawing-pitchmap-adding-lines-circles-matplotlib
以下是用于绘制镜头图的python代码:
# 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()
我想要做的是通过添加过滤器(小部件)来实现交互,这样我就可以过滤所拍摄的镜头类型,这样地图只会在地图上显示特定类型的镜头,例如如果我过滤“目标”,那么地图应该只显示导致目标的射门,等等。
我还想从 plotly 添加“悬停”功能,如果您将鼠标悬停在镜头上,它会告诉您“xG”值、镜头时间等。
当使用 plotly 时,我无法弄清楚如何保留音高图。我尝试了以下结果:
trace1 = px.scatter(df_salah, x='XM', y='YM', color='result')
fig = go.FigureWidget(trace1)
我还想保持圆圈的缩放比例
有人可以帮我从代码开始吗?因为我对如何执行此操作有点迷茫,或者我什至使用的是正确的库/平台吗?
【问题讨论】:
-
Yes, it is possible。但是您的代码特定问题是什么?
-
@Mr.T 我如何编写这样的代码?
-
通过开始适应其他人(如链接中的)所做的事情来满足您的需求。如果遇到问题,请阅读event handling 上的文档并查看examples in the gallery 如何解决类似问题。如果你然后遇到一个特定的问题,你会来问一个与代码相关的特定问题。没有人会为你编写完整的代码。
-
@Mr.T 谢谢,我明白了,但我不知道如何在使用 plotly 时保留我的音高图,这是我想首先弄清楚的。
-
好吧,首先,您可以决定要使用的平台。看起来,您想使用 plotly - 那么为什么将其标记为 matplotlib?而在 dadduckgo 上的第一个热门是:tutorialspoint.com/plotly/…
标签: python pandas matplotlib plotly ipywidgets