提问人:Trevor Thrash 提问时间:11/1/2023 最后编辑:Trenton McKinneyTrevor Thrash 更新时间:11/1/2023 访问量:38
生成空间热图
Generate a Spatial Heatmap
问:
我正在尝试重新创建下面提供的以下图表:
当通过十六进制箱的镜头观察它时,我的尝试很接近,但是我想插入数据并获得与上述示例类似的图表。我插值数据的尝试并不顺利。
到目前为止,我拥有的十六进制代码是:
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()
这将创建以下十六进制图格图,该图具有与原始热图相似的“热区”形状:
但是,当我尝试使用以下代码插值数据时:
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()
我得到这张图:
如何重写代码以实现与原始图像相似的内容?我知道我正在接近,但无法从这里找出正确的行动方案。
答: 暂无答案
上一个:处理两位数和图例时出现问题
评论
df_filtered