生成空间热图

Generate a Spatial Heatmap

提问人:Trevor Thrash 提问时间:11/1/2023 最后编辑:Trenton McKinneyTrevor Thrash 更新时间:11/1/2023 访问量:38

问:

我正在尝试重新创建下面提供的以下图表:

TruMedia xWOBA Chart

当通过十六进制箱的镜头观察它时,我的尝试很接近,但是我想插入数据并获得与上述示例类似的图表。我插值数据的尝试并不顺利。

到目前为止,我拥有的十六进制代码是:

from matplotlib.colors import LinearSegmentedColormap

df_filtered = df_filtered[df_filtered['estimated_woba_using_speedangle'] > 0]

plt.figure(figsize=(10, 10))

# Create hexbin plot with custom color map
hb = plt.hexbin(df_filtered['plate_x'], df_filtered['plate_z'], C=df_filtered['estimated_woba_using_speedangle'], 
                gridsize=15, cmap='jet', vmin=0.250, vmax=0.500, mincnt=1)

# Add a color bar with specified limits
cb = plt.colorbar(hb)
cb.set_label('Mean estimated_woba_using_speedangle')
cb.set_ticks([0.250, 0.500])

# Add labels and title
plt.xlabel('Plate Position X')
plt.ylabel('Plate Position Z')
plt.title('Hexbin Heatmap: Adolis Garcia xWOBA')

plt.xlim(-2, 2)
plt.ylim(0, 5)

# Flip the x-axis ticks
plt.gca().invert_xaxis()

# Show the plot
plt.show()

这将创建以下十六进制图格图,该图具有与原始热图相似的“热区”形状:

My Hexbin Plot

但是,当我尝试使用以下代码插值数据时:

import scipy.ndimage

# Create the hexbin plot and capture the output
hb = plt.hexbin(df_filtered['plate_x'], df_filtered['plate_z'], C=df_filtered['estimated_woba_using_speedangle'], gridsize=15,
cmap='jet', vmin=0.250, vmax=0.500, mincnt=2, visible=False)

# Get the coordinates and values from hexbin
x, y = hb.get_offsets().T
c = hb.get_array()

# Create a grid to interpolate on
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]

# Perform the KDE
from scipy.interpolate import griddata
grid_z = griddata((x, y), c, (grid_x, grid_y), method='linear')

# Smooth the KDE output for better rendering
grid_z = scipy.ndimage.gaussian_filter(grid_z, sigma=1)

# Plot the smoothed data
plt.imshow(grid_z.T, extent=[min(x), max(x), min(y), max(y)], origin='lower', cmap='jet', vmin=0.250, vmax=0.500)

# Add color bar, labels, and title
cb = plt.colorbar()
cb.set_label('Mean estimated_woba_using_speedangle')
plt.xlabel('Plate Position X')
plt.ylabel('Plate Position Z')
plt.title('Interpolated Hexbin Heatmap')

# Show the plot
plt.show()

我得到这张图:

My Interpolated Heatmap

如何重写代码以实现与原始图像相似的内容?我知道我正在接近,但无法从这里找出正确的行动方案。

Python Matplotlib 插值 热图

评论

1赞 D.L 11/1/2023
什么是第一个代码块?df_filtered
0赞 OM222O 11/1/2023
openCV 提供了一个内置的插值包,你可以先尝试双线性和三线性。另外,为什么需要十六进制图?对于热图来说,这似乎是一个非常奇怪的选择。

答: 暂无答案