0

I am making a histogram using matplotlib. I am using integer data and I want them to represent 1 bin. The following is the sample code which I made.

import matplotlib.pyplot as plt

a=[1,2,2,3,3,3,4,4,4,4]
plt.figure()
plt.hist(a,bins=4)
plt.show()

Histogram which I got

The histogram which I got is above. This have left end 1 ad rightend 4. I want the width of the histogram to be 1, however, this will just show 0.75 size. Moreover, I want the x value to locate at the center of the bar of histogram. Is there any way I can adjust?

Kyle
  • 80
  • 4
  • The easiest is to use seaborn indicating a discrete distribution: `sns.histplot(a, discrete=True)`. With only matplotlib and numpy, you need explicit bins fitting around the integers: `plt.hist(a, bins=np.arange(0.5, 5))` – JohanC Oct 13 '22 at 21:30

0 Answers0