Matplotlib histogram from numpy histogram output

I ran numpy.histogram()a set of subsets of larger datasets. I want to separate the calculations from the graphical output, so I would prefer not to name matplotlib.pyplot.hist()for the data itself.

In principle, both of these functions use the same input data: the original data itself, before they are binded. The version numpysimply returns the edges of the buffer nbin+1and nbin, while the version matplotlibcontinues to make the plot itself.

So, is there an easy way to generate histograms from the output itself numpy.histogram()without redoing the calculation (and keeping the inputs)?

To be clear, the output numpy.histogram()is a list of edges container nbin+1silos nbin; There is no procedure matplotlibthat takes them as input.

+4
source share
1 answer

You can build output numpy.histogramwith 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()

enter image description here

+5
source

Source: https://habr.com/ru/post/1677261/


All Articles