numpy.histogram2d#
- numpy.histogram2d(x, y, bins=10, range=None, density=None, weights=None)[源代码]#
计算两个数据样本的二维直方图.
- 参数:
- xarray_like,形状 (N,)
包含要进行直方图化的点的 x 坐标的数组.
- yarray_like,形状 (N,)
包含要进行直方图化的点的 y 坐标的数组.
- binsint 或 array_like 或 [int, int] 或 [array, array],可选
bin 规格:
如果为 int,则为两个维度的 bin 数 (nx=ny=bins).
如果为 array_like,则为两个维度的 bin 边缘 (x_edges=y_edges=bins).
如果为 [int, int],则表示每个维度的箱子数量 (nx, ny = bins).
如果为 [array, array],则表示每个维度的箱子边缘 (x_edges, y_edges = bins).
[int, array] 或 [array, int] 的组合,其中 int 是箱子的数量,而 array 是箱子的边缘.
- 范围array_like,形状为(2,2),可选.
沿着每个维度的箱子的最左边和最右边的边缘(如果未在“bins”参数中明确指定):
[[xmin, xmax], [ymin, ymax]].此范围之外的所有值都将被视为异常值,并且不会在直方图中进行统计.- 密度bool, 可选
如果为 False(默认),则返回每个箱子中的样本数. 如果为 True,则返回箱子的概率密度函数,
bin_count / sample_count / bin_area.- weightsarray_like,形状为(N,),可选.
一个值数组
w_i,用于衡量每个样本(x_i, y_i). 如果 density 为 True,则权重归一化为 1. 如果 density 为 False,则返回的直方图的值等于属于落入每个箱子的样本的权重之和.
- 返回:
- Hndarray,形状为(nx, ny)
样本 x 和 y 的二维直方图. x 中的值沿着第一维度进行直方图化, y 中的值沿着第二维度进行直方图化.
- xedgesndarray,形状为(nx+1,)
沿第一维的箱子边缘.
- yedgesndarray,形状为(ny+1,)
沿第二维的箱子边缘.
参见
histogram一维直方图
histogramdd多维直方图
注释
当 density 为 True 时,返回的直方图是样本密度,定义为箱子的乘积总和
bin_value * bin_area为 1.请注意,直方图不遵循笛卡尔约定,其中 x 值在横坐标上, y 值在纵坐标上. 而是 x 沿着数组的第一维度(垂直)进行直方图化,而 y 沿着数组的第二维度(水平)进行直方图化. 这确保了与
histogramdd的兼容性.示例
>>> import numpy as np >>> from matplotlib.image import NonUniformImage >>> import matplotlib.pyplot as plt
构造具有可变箱宽的二维直方图. 首先定义箱子的边缘:
>>> xedges = [0, 1, 3, 5] >>> yedges = [0, 2, 3, 4, 6]
接下来,我们创建一个具有随机箱内容的直方图 H:
>>> x = np.random.normal(2, 1, 100) >>> y = np.random.normal(1, 1, 100) >>> H, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges)) >>> # Histogram does not follow Cartesian convention (see Notes), >>> # therefore transpose H for visualization purposes. >>> H = H.T
imshow只能显示正方形箱子:>>> fig = plt.figure(figsize=(7, 3)) >>> ax = fig.add_subplot(131, title='imshow: square bins') >>> plt.imshow(H, interpolation='nearest', origin='lower', ... extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]]) <matplotlib.image.AxesImage object at 0x...>
pcolormesh可以显示实际边缘:>>> ax = fig.add_subplot(132, title='pcolormesh: actual edges', ... aspect='equal') >>> X, Y = np.meshgrid(xedges, yedges) >>> ax.pcolormesh(X, Y, H) <matplotlib.collections.QuadMesh object at 0x...>
NonUniformImage可以用于显示具有插值的实际箱边缘:>>> ax = fig.add_subplot(133, title='NonUniformImage: interpolated', ... aspect='equal', xlim=xedges[[0, -1]], ylim=yedges[[0, -1]]) >>> im = NonUniformImage(ax, interpolation='bilinear') >>> xcenters = (xedges[:-1] + xedges[1:]) / 2 >>> ycenters = (yedges[:-1] + yedges[1:]) / 2 >>> im.set_data(xcenters, ycenters, H) >>> ax.add_image(im) >>> plt.show()
还可以构造一个二维直方图,而无需指定箱子边缘:
>>> # Generate non-symmetric test data >>> n = 10000 >>> x = np.linspace(1, 100, n) >>> y = 2*np.log(x) + np.random.rand(n) - 0.5 >>> # Compute 2d histogram. Note the order of x/y and xedges/yedges >>> H, yedges, xedges = np.histogram2d(y, x, bins=20)
现在,我们可以使用
pcolormesh绘制直方图,并使用hexbin进行比较.>>> # Plot histogram using pcolormesh >>> fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True) >>> ax1.pcolormesh(xedges, yedges, H, cmap='rainbow') >>> ax1.plot(x, 2*np.log(x), 'k-') >>> ax1.set_xlim(x.min(), x.max()) >>> ax1.set_ylim(y.min(), y.max()) >>> ax1.set_xlabel('x') >>> ax1.set_ylabel('y') >>> ax1.set_title('histogram2d') >>> ax1.grid()
>>> # Create hexbin plot for comparison >>> ax2.hexbin(x, y, gridsize=20, cmap='rainbow') >>> ax2.plot(x, 2*np.log(x), 'k-') >>> ax2.set_title('hexbin') >>> ax2.set_xlim(x.min(), x.max()) >>> ax2.set_xlabel('x') >>> ax2.grid()
>>> plt.show()