来自numpy直方图输出的Matplotlib直方图
我在较大数据集的一堆子集上运行了numpy.histogram()
.我想将计算结果与图形输出分开,所以我不希望在数据本身上不调用matplotlib.pyplot.hist()
.
I have run numpy.histogram()
on a bunch of subsets of a larger datasets. I want to separate the calculations from the graphical output, so I would prefer not to call matplotlib.pyplot.hist()
on the data itself.
原则上,这两个函数都采用相同的输入:装箱前的原始数据本身. numpy
版本仅返回nbin+1
bin边沿和nbin
频率,而matplotlib
版本继续绘制图本身.
In principle, both of these functions take the same inputs: the raw data itself, before binning. The numpy
version just returns the nbin+1
bin edges and nbin
frequencies, whereas the matplotlib
version goes on to make the plot itself.
那么有没有一种简便的方法可以从numpy.histogram()
输出本身生成直方图,而无需重做计算(也不必保存输入)?
So is there an easy way to generate the histograms from the numpy.histogram()
output itself, without redoing the calculations (and having to save the inputs)?
要清楚,numpy.histogram()
输出是nbin
个bin的nbin+1
个bin边缘的列表;没有matplotlib
例程将这些作为输入.
To be clear, the numpy.histogram()
output is a list of nbin+1
bin edges of nbin
bins; there is no matplotlib
routine which takes those as input.
您可以使用plt.bar
绘制numpy.histogram
的输出.
You can plot the output of numpy.histogram
using plt.bar
.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)
a = np.random.rayleigh(scale=3,size=100)
bins = np.arange(10)
frq, edges = np.histogram(a, bins)
fig, ax = plt.subplots()
ax.bar(edges[:-1], frq, width=np.diff(edges), edgecolor="black", align="edge")
plt.show()