Compute the bi-dimensional histogram of two data samples.
Parameters : | x : array_like, shape(N,)
y : array_like, shape(M,)
bins : int or [int, int] or array_like or [array, array], optional
range : array_like, shape(2,2), optional
normed : bool, optional
weights : array_like, shape(N,), optional
|
---|---|
Returns : | H : ndarray, shape(nx, ny)
xedges : ndarray, shape(nx,)
yedges : ndarray, shape(ny,)
|
See also
Notes
When normed is True, then the returned histogram is the sample density, defined such that:
where H is the histogram array and the area of bin {i,j}.
Please note that the histogram does not follow the Cartesian convention where x values are on the abcissa and y values on the ordinate axis. Rather, x is histogrammed along the first dimension of the array (vertical), and y along the second dimension of the array (horizontal). This ensures compatibility with histogramdd.
Examples
>>> x, y = np.random.randn(2, 100)
>>> H, xedges, yedges = np.histogram2d(x, y, bins=(5, 8))
>>> H.shape, xedges.shape, yedges.shape
((5, 8), (6,), (9,))
We can now use the Matplotlib to visualize this 2-dimensional histogram:
>>> extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]
>>> import matplotlib.pyplot as plt
>>> plt.imshow(H, extent=extent, interpolation='nearest')
<matplotlib.image.AxesImage object at ...>
>>> plt.colorbar()
<matplotlib.colorbar.Colorbar instance at ...>
>>> plt.show()
(Source code, png, pdf)