Search code examples
python-3.xmatplotlibhistogramvisualizationbin

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.

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.