Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: histogram is not displaying

I am trying to draw histogram but nothing appears in the Figure Window. My code is below:

import numpy as np
import matplotlib.pyplot as plt

values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]

plt.hist(values, 10, histtype = 'bar', facecolor = 'blue')
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([0,11,0,220000])
plt.show()

This is the output: enter image description here

I am trying to achieve this plot enter image description here

Any help would be much appreciated...

like image 407
MAY Avatar asked Dec 25 '22 06:12

MAY


1 Answers

You are confusing what a histogram is. The histogram that can be produced with the given data is as given below.

A histogram basically counts how many given values fall within a given range.

You have given incorrect arguments to the axis() function. The ending value is 2200000 You missed a single zero. Also you have swapped the arguments. Limits of the x axis comes first and then the limits of the Y axis. This is the modified code:

import numpy as np
import matplotlib.pyplot as plt

values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]

plt.hist(values, 10, histtype = 'bar', facecolor = 'blue')
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([0,2200000,0,11])
plt.show()

This is the histogram generated:

enter image description here

like image 107
Aswin P J Avatar answered Dec 26 '22 19:12

Aswin P J