Heatmap using python matplotlib scatter dataset

I am writing a script to make a heatmap for scattering data on two dimensions. The following is an example of what I'm trying to do:

import numpy as np
from matplotlib.pyplot import*
x = [1,2,3,4,5]
y = [1,2,3,4,5]
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
imshow(heatmap, extent = extent)

I should expect the โ€œwarmestโ€ areas to be located along y = x, but instead they will appear along y = -x + 5 ie if the heat map reads one list in the opposite direction. I do not know why this is happening. Any suggestions?

thank

+5
source share
2 answers

Try the option imshow origin=lower. By default, it sets the element (0,0) of the array in the upper left corner.

For instance:

import numpy as np
import matplotlib.pyplot as plt
x = [1,2,3,4,5,5]
y = [1,2,3,4,5,5]
heatmap, xedges, yedges = np.histogram2d(x, y, bins=10)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.imshow(heatmap, extent = extent)
ax1.set_title("imshow Default");
ax2 = fig.add_subplot(212)
ax2.imshow(heatmap, extent = extent,origin='lower')
ax2.set_title("imshow origin='lower'");

fig.savefig('heatmap.png')

It produces:

enter image description here

+3
source

, , :

ax2.imshow(heatmap.T, extent = extent,origin='lower')
0

All Articles