Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behaviour of hexbin and histogram2d

What is the difference between hexbin and histogram2d?

f, (ax1,ax2) = plt.subplots(2)
ax1.hexbin(vradsel[0], distsel[0],gridsize=20,extent=-200,200,4,20],cmap=plt.cm.binary)
H, xedges, yedges =np.histogram2d(vradsel[0], distsel[0],bins=20,range=[[-200,200],[4,20]])
ax2.imshow(H, interpolation='nearest', cmap=plt.cm.binary, aspect='auto',extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]])
plt.show()

You can see that the histogram2d gives a -90 degree rotation. I know the data should be like the hexbin plot.

Difference hexbin/histogram2d

like image 938
Mathias711 Avatar asked Apr 10 '14 13:04

Mathias711


1 Answers

The difference is not between the way the histograms are being calculated, but rather in the way you've plotted the histogram. Your array H from np.histogram starts with the 4, -200 bin at the upper left of the array, but will be plotted depending on your default value of origin. You can control this using the origin=lower or origin=upper keywords in plt.imshow.

But origin just mirrors the image, so additionally you must remember that in images, the horizontal axis x comes first, and the vertical axis y comes second, the opposite of arrays, so you must also transpose H before plotting.

My recommendation is just to use plt.hist2d() instead, which will adjust the extent and orientation properly, as with plt.hexbin. You can still access the result as with the numpy version: H, x, y, im = ax.hist2d(...) but it makes the plot automatically


a = np.random.rand(100)*400 - 200
b = np.random.rand(100)*16 + 4
a[:10] = -200
b[:10] = 4

f, ax = plt.subplots(3)

ax[0].hexbin(a, b, gridsize=20, extent=[-200,200,4,20], cmap=plt.cm.binary)

H, xedges, yedges = np.histogram2d(a, b, bins=20, range=[[-200,200],[4,20]])
ax[1].imshow(H.T, interpolation='nearest', cmap=plt.cm.binary, aspect='auto',
                extent=[xedges[0],xedges[-1],yedges[0],yedges[-1]], origin='lower')

# simplest and most reliable:
ax[2].hist2d(a, b, bins=20, range=[[-200,200],[4,20]], cmap=plt.cm.binary)

hists

like image 122
askewchan Avatar answered Oct 04 '22 03:10

askewchan