Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Specifying bins for histogram plot in python

I am trying to plot a histogram

 import matplotlib.pyplot as plt
 import numpy as np
 v = ['1302.42', '7.85422', '21.0631', '6956.37', '10000', '10000', '7312.88', '7708.98', '8421.12', '7419.41',
     '1952.47', '6762.42', '10000', '4057.73', '2051.06', '10000']

    plt.hist(v, bins=np.linspace(0, 10000, 11))
    plt.xticks(rotation=90)
    plt. set(ylabel='Frequency')
    plt.show()
    

enter image description here

The plot doesn’t appear right. I am not sure if the bin specification is correct. I am trying to set an interval of 1000. The actual values range from 0 to 10000.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Suggestions on how to fix this will be really helpful.

>Solution :

Looks fine, you just need to convert your strings into floats:

v = [float(v) for v in [
      '1302.42', '7.85422', '21.0631', '6956.37', '10000', '10000', '7312.88', 
      '7708.98', '8421.12', '7419.41', '1952.47', '6762.42', '10000', '4057.73', '2051.06', 
         '10000']]

And then the rest of the code is identical.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading