Different behavior 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 2d histogram gives a rotation of -90 degrees. I know that the data should look like a hexbin chart.

Difference hexbin / histogram2d

+4
source share
1 answer

The difference is not how the histograms are calculated, but how you built the histogram. Your array Hof np.histogramstarts with a bit 4, -200in the upper left corner of the array, but will be displayed depending on your default value origin. You can manage this using keywords origin=loweror origin=upperin plt.imshow.

origin , , x, y , , H .

, plt.hist2d(), , plt.hexbin. , numpy: H, x, y, im = ax.hist2d(...),


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

+3

Source: https://habr.com/ru/post/1535948/


All Articles