我已经实现了一种在Python中创建2D热图的方法:
def myplot(x, y, z, s, bins=[1000,1777], stat='mean'):
heatmap, xedges, yedges, binnumber = stats.binned_statistic_2d(x, y, values=z, statistic=stat, bins=bins, range=[[0, 1080], [0, 1920]])
heatmap[np.isnan(heatmap)] = 0
heatmap = gaussian_filter(heatmap, sigma=s)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
return heatmap.T, extent
def transparent_cmap(cmap, N=255):
"Copy colormap and set alpha values"
mycmap = cmap
mycmap._init()
mycmap._lut[:,-1] = np.linspace(0, 0.8, N+4)
return mycmap
然后我将创建两个热图,如下所示:
fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(111)
img, extent = myplot(df1["x"].values, df1["y"].values, df1["pressure"].values, 8, stat='mean', bins=bins)
ax.imshow(img, extent=extent, origin='lower', cmap=transparent_cmap(cm.jet))
ax.set_xlim(0,1080)
ax.set_ylim(0,1920)
plt.show()
fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(111)
img, extent = myplot(df2["x"].values, df2["y"].values, df2["pressure"].values, 8, stat='mean', bins=bins)
ax.imshow(img, extent=extent, origin='lower', cmap=transparent_cmap(cm.jet))
ax.set_xlim(0,1080)
ax.set_ylim(0,1920)
plt.show()
因此,我有两个Pandas DataFrames df1
和df2
,它们分别具有“ x”,“ y”和“ pressure”列。现在,我的问题是两个热图的颜色不一致。这意味着热图中的相同颜色表示的值不相同。理想情况下,颜色图中最左边的颜色应对应于df1和df2中的最小值,最右边的颜色应对应于最大值。这可能吗?
此外,我想在绘图旁边显示一个色条,该色条显示了不同颜色的值。还有可能吗?