【问题标题】:Overleaped heatmap doesn't show correctly重叠的热图无法正确显示
【发布时间】:2022-01-06 00:33:14
【问题描述】:

我在图像上方绘制了一张热图(如图像链接1 所示),带有高斯滤波器和标准化数据。主要问题是 y 轴上没有低于 92 的值,因此绘图不是从 (0,0) 开始,而是从 (0,92) 开始。因此,当我将两张图片(热图和背景图像)放在一起时,图表上有一个突然的切割(如第二个链接所示,没有背景的热图在哪里)。

那么,我怎样才能延长热图上的轴,使其从 (0,0) 开始?

下面是我目前用来绘制两个图像的代码。谢谢!

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy.ndimage.filters import gaussian_filter
import matplotlib.colors as mcolors
from sklearn.preprocessing import normalize

x = df['x_data']
y = df['y_data']

heatmap, xedges, yedges = np.histogram2d(x, y, bins = [800,600])

extent = [0, xedges[-1], yedges[0], yedges[-1]]
heatmap = normalize(heatmap)
heatmap = gaussian_filter(heatmap, 16)   

colors = [(1,1-c,0,c) for c in np.linspace(0,1,100)]
cmapred = mcolors.LinearSegmentedColormap.from_list('mycmap', colors, N=5)

map_img = mpimg.imread('dir/to/background/image.png')

fig, ax = plt.subplots(figsize=(16.1, 9.1))

plt.imshow(map_img, extent=[0, 800, 0, 600], cmap = 'Greys_r')
plt.imshow(heatmap.T, extent = extent, origin = 'lower', cmap = cmapred, alpha = 0.7)

plt.ylim([0,600])
plt.xlim([0,800])
plt.show()

Image of heatmap + background:https://imgur.com/2vX6Bw6

Image of only heatmap:https://imgur.com/axMe7K7

【问题讨论】:

    标签: python matplotlib plot heatmap


    【解决方案1】:

    您可以手动将行添加到热图中。也许更容易尝试明确设置直方图箱?

    bins=[np.arange(0, 800, 1), np.arange(0, 600, 1)]
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2020-08-04
      • 2016-09-11
      • 2018-03-24
      相关资源
      最近更新 更多