如何将热图绘制到 mplsoccer 球场上?

How to plot heatmap onto mplsoccer pitch?

想知道如何将 seaborn 绘图绘制到不同的 matplotlib 绘图上。目前我有两个图(一个是热图,另一个是足球场),但是当我将热图绘制到球场上时,我得到以下结果。 (将音高绘制到热图上也不是很好。)有什么解决方法吗?

注意:绘图不需要颜色条,也不需要网格结构。只关心覆盖整个 space 球场的热图。谢谢!

import pandas as pd
import numpy as np
from mplsoccer import Pitch
import seaborn as sns

nmf_shot_W = pd.read_csv('https://raw.githubusercontent.com/lucas-nelson-uiuc/datasets/main/nmf_show_W.csv').iloc[:, 1:]
nmf_shot_ThierryHenry = pd.read_csv('https://raw.githubusercontent.com/lucas-nelson-uiuc/datasets/main/nmf_show_Hth.csv')['Thierry Henry']

pitch = Pitch(pitch_type='statsbomb', line_zorder=2,
              pitch_color='#22312b', line_color='#efefef')
dfdfdf = np.array(np.matmul(nmf_shot_W, nmf_shot_ThierryHenry)).reshape((24,25))
g_ax = sns.heatmap(dfdfdf)
pitch.draw(ax=g_ax)

当前输出:

期望的输出:

使用 built-in pitch.heatmap:

pitch.heatmap 需要 stats 分箱数据、分箱网格和分箱中心的字典:

stats (dict) – The keys are statistic (the calculated statistic), x_grid and y_grid (the bin's edges), and cx and cy (the bin centers).

mplsoccer heatmap demos, they construct this stats object using pitch.bin_statistic因为他们有原始数据。但是,您已经有了分箱数据(“计算统计”),因此通过构建网格和中心手动重建 stats 对象:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mplsoccer import Pitch

nmf_shot_W = pd.read_csv('71878281/nmf_show_W.csv', index_col=0)
nmf_shot_ThierryHenry = pd.read_csv('71878281/nmf_show_Hth.csv')['Thierry Henry']

statistic = np.dot(nmf_shot_W, nmf_shot_ThierryHenry.to_numpy()).reshape((24, 25))

# construct stats object from binned data, bin mesh, and bin centers
y, x = statistic.shape
x_grid = np.linspace(0, 120, x + 1)
y_grid = np.linspace(0, 80, y + 1)
cx = x_grid[:-1] + 0.5 * (x_grid[1] - x_grid[0])
cy = y_grid[:-1] + 0.5 * (y_grid[1] - y_grid[0])
stats = dict(statistic=statistic, x_grid=x_grid, y_grid=y_grid, cx=cx, cy=cy)

# use pitch.draw and pitch.heatmap as per mplsoccer demo
pitch = Pitch(pitch_type='statsbomb', line_zorder=2, pitch_color='#22312b', line_color='#efefef')
fig, ax = pitch.draw(figsize=(6.6, 4.125))

pcm = pitch.heatmap(stats, ax=ax, cmap='plasma')
cbar = fig.colorbar(pcm, ax=ax, shrink=0.6)
cbar.outline.set_edgecolor('#efefef')
cbar.ax.yaxis.set_tick_params(color='#efefef')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#efefef')