2

Also, the histogram bar widths are different on certain values of bin. How to keep the bar widths uniform? I have tried using the rwidth but that dos not solve my problem.

Data:

test    age
17 - Alpha OH PROGESTERONE - HORMONE ASSAYS 23
17 - Alpha OH PROGESTERONE - HORMONE ASSAYS 26
17 ALPHA HYDROXY PROGESTERONE   18
17 ALPHA HYDROXY PROGESTERONE   21
17 ALPHA HYDROXY PROGESTERONE   25
17 ALPHA HYDROXY PROGESTERONE   27

Code

axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
plt.hist(df.loc[0:1,'age':'age'].to_numpy(),100, rwidth=0.9)

axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
plt.hist(df.loc[2:5,'age':'age'].to_numpy(),10, rwidth=0.9)

axes = plt.gca()
axes.set_xlim(0, 100)
axes.set_ylim(0, 100)
plt.hist(df.loc[6:14,'age':'age'].to_numpy(),100, rwidth=0.9)

As you can see, in the first image, the bars are vanished on keeping the bins = 100

I can see the bars now on keeping the bins = 5

If you look carefully, in the third mage, which corresponds to the third histogram, the bars are of different widths (2 and 3 pixels wide in my screen)

Brian Spiering
  • 20,142
  • 2
  • 25
  • 102

1 Answers1

1

The height of a bar shows the number of measurements within that range.

The bars get shorter when you increase the bin count, because the more bins you have in total, the less measurements fall into an individual bin.

pcko1
  • 3,910
  • 1
  • 14
  • 29
  • That is true. But then why do the bars also altogether vanish? Is it because the bins are so high that the height of bars is now in decimals? – Varun Khanna Apr 11 '19 at 08:00
  • if you dont use normalized histograms (with frequency on the y-axis), then the y-axis represents the bin count, which is `int`. Therefore those low values are probably 0. – pcko1 Apr 11 '19 at 08:26
  • by normalized I mean setting `density=True` in `plt.hist()` – pcko1 Apr 11 '19 at 08:30