Is there a clean way to generate a line histogram chart in Python?

后端 未结 3 487
庸人自扰
庸人自扰 2021-02-01 07:01

I need to create a histogram that plots a line and not a step or bar chart. I am using python 2.7 The plt.hist function below plots a stepped line and the bins don\'t line up i

3条回答
  •  借酒劲吻你
    2021-02-01 07:53

    Matplotlib's thumbnail gallery is usually quite helpful in situations like yours. A combination of this and this one from the gallery with some customizations is probably very close to what you have in mind:

    import numpy as np
    import matplotlib.mlab as mlab
    import matplotlib.pyplot as plt
    
    mu = 0
    sigma = 1
    noise = np.random.normal(mu, sigma, size=1000)
    num_bins = 7
    n, bins, _ = plt.hist(noise, num_bins, normed=1, histtype='step')
    y = mlab.normpdf(bins, mu, sigma)
    plt.plot(bins, y, 'r--')
    plt.show()
    

    enter image description here

    Also, increasing the number of bins helps...

    enter image description here

提交回复
热议问题