Getting information for bins from the histogram function
Asked Answered
D

2

31

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?

Drice answered 18/10, 2013 at 5:27 Comment(0)
D
50

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.

Dyl answered 18/10, 2013 at 9:39 Comment(0)
P
1

Binning values into discrete intervals in plt.hist is done using np.histogram, so if for some reason you want the bins and counts without plotting the data, you could use np.histogram. If you want the data together with the plot, as @Bonlenfum shows, the hist() call already returns such data.

As you can see from below, the counts and bins exactly match for pyplot and numpy histograms.

x = np.random.rand(1000)
n_bins = 100

# plot histogram
n_plt, bins_plt, patches = plt.hist(x, n_bins)
# compute histogram
n_np, bins_np = np.histogram(x, n_bins)

(n_plt == n_np).all() and (bins_plt == bins_np).all()  # True
Pyonephritis answered 5/5, 2023 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.