更新时间:2022-03-08 01:12:26
Numpy具有专用函数,用于以您需要的方式创建直方图:
Numpy has a dedicated function for creating histograms the way you need to:
histogram(a, bins=10, range=None, normed=False, weights=None, new=None)
您可以像这样使用
(hist_data, bin_edges) = histogram(my_array[:,0], weights=my_array[:,1])
此处的关键点是使用weights
参数:每个值a[i]
都会对直方图贡献weights[i]
.示例:
The key point here is to use the weights
argument: each value a[i]
will contribute weights[i]
to the histogram. Example:
a = [0, 1]
weights = [10, 2]
在x = 0时描述10点,在x = 1时描述2点.
describes 10 points at x = 0 and 2 points at x = 1.
You can set the number of bins, or the bin limits, with the bins
argument (see the official documentation for more details).
然后可以使用以下类似的方式绘制直方图:
The histogram can then be plotted with something like:
bar(bin_edges[:-1], hist_data)
如果您只需要进行直方图 plot ,则类似的 hist()函数可以直接绘制直方图:
If you only need to do a histogram plot, the similar hist() function can directly plot the histogram:
hist(my_array[:,0], weights=my_array[:,1])