如何使用 Matplotlib 或 Seaborn 手动选择数据在 Python 热图上的显示位置?
How to manually choose where the data is displayed on a heatmap in Python with Matplotlib or Seaborn?
我在 pandas DataFrame 中有以下 table,其中包含四分卫的统计数据:
|| pass_type | WPA ||
||----------------|---------- ||
|| deep - left | -0.202351 ||
|| short - left | 0.456855 ||
|| deep - middle | -1.698436 ||
|| short - middle | 0.879427 ||
|| deep - right | 0.378192 ||
|| short - right | 1.448731 ||
我想在热图中显示这些统计数据,但我希望能够选择每个值的显示位置。我的目标是“重建”场地,第一行是短传,第二行是长传,左列是左传,等等。
准确的说,我想得到的是:
deep || -0.20 | -1.69 | 0.37 ||
short || 0.45 | 0.87 | 1.44 ||
---------------------------------
|| left | middle | right ||
我已经能够使用 plotly 来做到这一点,方法是创建两个列表,一个具有深度传球,另一个具有短传。但我希望能够使用 Seaborn 或 Matplotib 来完成,因为我喜欢它们的格式选项 + 在 github.
上的 Jupyter Notebook 中发布更容易
你可以用 str.split
:
将两部分分开
plot_data = (df.join(df['pass_type'].str.split(' - ', expand=True))
.pivot(index=0, columns=1, values='WPA')
)
那么你会得到 plot_data
作为:
1 left middle right
0
deep -0.202351 -1.698436 0.378192
short 0.456855 0.879427 1.448731
你可以传递给 sns.heatmap
:
sns.heatmap(plot_data)
输出:
我在 pandas DataFrame 中有以下 table,其中包含四分卫的统计数据:
|| pass_type | WPA ||
||----------------|---------- ||
|| deep - left | -0.202351 ||
|| short - left | 0.456855 ||
|| deep - middle | -1.698436 ||
|| short - middle | 0.879427 ||
|| deep - right | 0.378192 ||
|| short - right | 1.448731 ||
我想在热图中显示这些统计数据,但我希望能够选择每个值的显示位置。我的目标是“重建”场地,第一行是短传,第二行是长传,左列是左传,等等。 准确的说,我想得到的是:
deep || -0.20 | -1.69 | 0.37 ||
short || 0.45 | 0.87 | 1.44 ||
---------------------------------
|| left | middle | right ||
我已经能够使用 plotly 来做到这一点,方法是创建两个列表,一个具有深度传球,另一个具有短传。但我希望能够使用 Seaborn 或 Matplotib 来完成,因为我喜欢它们的格式选项 + 在 github.
上的 Jupyter Notebook 中发布更容易你可以用 str.split
:
plot_data = (df.join(df['pass_type'].str.split(' - ', expand=True))
.pivot(index=0, columns=1, values='WPA')
)
那么你会得到 plot_data
作为:
1 left middle right
0
deep -0.202351 -1.698436 0.378192
short 0.456855 0.879427 1.448731
你可以传递给 sns.heatmap
:
sns.heatmap(plot_data)
输出: