来自numpy直方图输出的Matplotlib直方图 [英] Matplotlib histogram from numpy histogram output

查看:348
本文介绍了来自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()

这篇关于来自numpy直方图输出的Matplotlib直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆