Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heatmap using scatter dataset python matplotlib

I am writing a script to make a heatmap for scatter data on two dimensionS. The following is a toy example of what I am 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 a the 'warmest' areas to be along y=x but instead they show up along y=-x+5 i.e the heatmap reads one list in the reverse direction. I am not sure why this is happening. Any suggestions?

Thanks

like image 952
msohail Avatar asked Oct 23 '22 13:10

msohail


1 Answers

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

For example:

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')

Produces:

enter image description here

like image 160
Yann Avatar answered Nov 14 '22 01:11

Yann