zetzot37
zetzot37

Reputation: 35

Is there a way to save the bins from a histogram in python?

I have recently started with a Python course, and we are required to plot a histogram, and then retrieve the generated bins. The code is as follows:

fig, axs = plt.subplots(1, 1,
                        figsize =(10, 5), 
                        tight_layout = True)
axs.hist(diferencia[14329:27006], bins=10, rwidth = 0.8)
plt.show()
print("\n")
plt.savefig("histograma.png")

The histogram is generated just fine, but I am having trouble getting the bins from it. Sorry for this basic question, I am just a starter on python. Thank you!

Upvotes: 3

Views: 1617

Answers (1)

JohanC
JohanC

Reputation: 80339

You can save the information of hist via values, bins, patches = ax.hist(...). bins will be the array of bin boundaries (one more than the number of bars).

Here is an example usage to clarify the idea a bit more. The bin boundaries are both used for text output as to show a background to each bin. Note that choosing rwidth a value different from 1 could give the false impression that some values in-between aren't part of the dataset. An alternative could be to apply an edgecolor.

import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle

fig, axs = plt.subplots(figsize=(10, 5))
N = 2000
values, bins, patches = axs.hist(np.random.randn(N), bins=10, rwidth=0.8, color='dodgerblue', edgecolor='white')
for val, b0, b1, color in zip(values, bins[:-1], bins[1:], cycle(['crimson', 'lightblue'])):
    print(f'Bin {b0:.3f},{b1:.3f}: {val:.0f} entries ({val / N * 100:.2f} %)')
    axs.axvspan(b0, b1, color=color, alpha=0.1, zorder=0)
axs.margins(x=0)
plt.show()

sample histogram

Bin -3.132,-2.483: 11 entries (0.55 %)
Bin -2.483,-1.833: 52 entries (2.60 %)
Bin -1.833,-1.184: 162 entries (8.10 %)
Bin -1.184,-0.534: 354 entries (17.70 %)
Bin -0.534,0.116: 493 entries (24.65 %)
Bin 0.116,0.765: 486 entries (24.30 %)
Bin 0.765,1.415: 284 entries (14.20 %)
Bin 1.415,2.064: 117 entries (5.85 %)
Bin 2.064,2.714: 35 entries (1.75 %)
Bin 2.714,3.364: 6 entries (0.30 %)

If you need nicer looking numbers for the bins, you could set your own array of bins in ax.hist(..., bins=...). The default uses bins=np.linspace(min_value, max_value, 11). An example could be bins=np.arange(-4, 4.1, 0.5).

PS: Note that it's better to call savefig before plt.show() because the latter happens to erase the plot when closed.

Upvotes: 1

Related Questions