Matplotlib histogram from numpy histogram output [duplicate]

浪尽此生 提交于 2019-12-20 12:08:23

问题


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.

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.

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)?

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.


回答1:


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), ec="k", align="edge")

plt.show()



来源:https://stackoverflow.com/questions/44003552/matplotlib-histogram-from-numpy-histogram-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!