Overleaped heatmap doesn't show correctly

Viewed 45

I have a heatmap plotted above an image (as shown on image link 1), with gaussian filter and normalize data. The main issue is that there is no value under 92 on the y axis, so the plot doesnt start on (0,0), instead start on (0,92). So, when I put together both pictures (heatmap and background image), there is an abrupt cut on the graph (as shown on 2nd link, where is the heatmap without background).

So, how can I extend the axis on the heatmap so it start on (0,0)?

Below is the code that I'm currently using to plot both images. Thanks!

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

1 Answers

You could add rows to your heat map manually. Maybe easier is to try setting the histogram bins explicitly?

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