I am plotting a histogram in python using matplotlib by:
plt.hist(nparray, bins=10, label='hist')
Is it possible to print a dataframe that has the information for all the bins, like number of elements in every bin?
Calculate the number of bins by taking the square root of the number of data points and round up. Calculate the bin width by dividing the specification tolerance or range (USL-LSL or Max-Min value) by the # of bins.
To get the values from a histogram, plt. hist returns them, so all you have to do is save them.
The return values of plt.hist
are:
Returns: tuple : (n, bins, patches) or ([n0, n1, ...], bins, [patches0, patches1,...])
So all you need to do is capture the return values appropriately. For example:
import numpy as np import matplotlib.pyplot as plt # generate some uniformly distributed data x = np.random.rand(1000) # create the histogram (n, bins, patches) = plt.hist(x, bins=10, label='hst') plt.show() # inspect the counts in each bin In [4]: print n [102 87 102 83 106 100 104 110 102 104] # and we see that the bins are approximately uniformly filled. # create a second histogram with more bins (but same input data) (n2, bins2, patches) = plt.hist(x, bins=20, label='hst') In [34]: print n2 [54 48 39 48 51 51 37 46 49 57 50 50 52 52 59 51 58 44 58 46] # bins are uniformly filled but obviously with fewer in each bin.
The bins
that is returned defines the edges of each bin that was used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With