这个python案例是否分析射门位置分布图?

wen python案例 3

我需要先说明,您提到的"这个python案例"我目前看不到具体内容,不过我可以为您提供如何用Python分析射门位置分布图的完整方案,您可以对照自己的案例来检查:

这个python案例是否分析射门位置分布图?


📊 射门位置分布图分析要点

数据需求

典型射门数据应包含:

# 示例数据结构
shots_data = [
    {"x": 45, "y": 30, "goal": 0, "type": "普通射门"},
    {"x": 52, "y": 18, "goal": 1, "type": "头球"},
    # ... 更多数据
]
  • x坐标:球场长度方向(0-105米)
  • y坐标:球场宽度方向(0-68米)
  • goal:是否进球(0/1)
  • type:射门方式(可选)

核心分析代码模板

import matplotlib.pyplot as plt
import numpy as np
def plot_shot_map(shots, team_name="主队"):
    fig, ax = plt.subplots(figsize=(12, 8))
    # 绘制球场背景(半场)
    draw_pitch(ax, half=True)  # 自定义函数
    # 分离进球和非进球
    goals = [s for s in shots if s["goal"] == 1]
    nongoals = [s for s in shots if s["goal"] == 0]
    # 绘制非进球(圆圈)
    ax.scatter([s["x"] for s in nongoals], 
              [s["y"] for s in nongoals],
              s=80, c='#e74c3c', alpha=0.6, label='未进球')
    # 绘制进球(星形)
    ax.scatter([s["x"] for s in goals],
              [s["y"] for s in goals],
              s=150, marker='*', c='#2ecc71', 
              edgecolors='black', label='进球')
    ax.set_title(f'{team_name} 射门位置分布图')
    ax.legend()
    plt.show()

进阶分析功能

🔍 密度热力图(射门密集区域)

from scipy.stats import gaussian_kde
def heatmap_shots(shots):
    x = [s["x"] for s in shots]
    y = [s["y"] for s in shots]
    # 核密度估计
    density = gaussian_kde([x, y], bw_method=0.1)
    xs, ys = np.meshgrid(np.linspace(0, 105, 200),
                        np.linspace(0, 68, 200))
    z = density(np.vstack([xs.ravel(), ys.ravel()]))
    z = z.reshape(xs.shape)
    plt.imshow(z, extent=[0, 105, 0, 68], cmap='Reds', alpha=0.5)
    plt.colorbar(label='射门密度')

📈 离散角度分析

def shot_angle_analysis(shots):
    angles = []
    for s in shots:
        # 计算射门角度(相对于球门中心)
        goal_x, goal_y = 105, 34  # 球门中心坐标
        angle = np.degrees(np.arctan2(goal_y - s["y"], goal_x - s["x"]))
        angles.append(angle)
    plt.hist(angles, bins=36, alpha=0.7, edgecolor='black')
    plt.xlabel('射门角度(度)')
    plt.ylabel('频次')

判断您的案例是否包含此分析

特征 如何检查
有x,y坐标数据 查看代码中是否出现 "x":"y": 或类似坐标变量
使用matplotlib/seaborn绘图 显式导入或调用 plt.plotsns.scatterplot
绘制球场背景 有无绘制绿底白线的矩形区域代码
区分进球/非进球 有无颜色/形状差异展示
热力图/密度图 有无 scipy.stats.gaussian_kdesns.kdeplot

完整示例代码(可直接运行)

import matplotlib.pyplot as plt
import numpy as np
# 模拟射门数据(20次射门)
np.random.seed(42)
shots = []
for i in range(20):
    # 模拟禁区附近射门(x靠近105,y在0-68)
    x = np.random.normal(85, 15)
    y = np.random.normal(34, 20)
    goal = np.random.random() < 0.15  # 15%进球率
    shots.append({"x": x, "y": y, "goal": goal})
def draw_pitch():
    """绘制半场球场"""
    plt.gca().set_facecolor('#44a952')
    plt.plot([0, 0, 105, 105, 0], [0, 68, 68, 0, 0], 'w', lw=2)
    # 禁区
    plt.plot([0, 16.5, 16.5, 0], [13.84, 13.84, 54.16, 54.16], 'w')
    # 球门
    plt.plot([0, 2], [30.34, 30.34], 'w', lw=4)
    plt.plot([0, 2], [37.66, 37.66], 'w', lw=4)
    plt.plot([2, 2], [30.34, 37.66], 'w', lw=4)
fig, ax = plt.subplots(figsize=(12, 8))
draw_pitch()
# 绘制射门点
for s in shots:
    if s["goal"]:
        ax.scatter(s["x"], s["y"], s=150, marker='*', c='yellow', edgecolor='black')
    else:
        ax.scatter(s["x"], s["y"], s=80, c='red', alpha=0.6)
ax.set_xlim(0, 105)
ax.set_ylim(0, 68)
ax.set_title('射门位置分布图(★进球 | ●未进球)')
plt.show()

您将案例代码发给我,我可以直接判断它是否包含射门位置分析,并指出缺少的部分。 如果是其他运动(如篮球/冰球),只需调整球场尺寸即可。

抱歉,评论功能暂时关闭!