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

后端 未结 3 493
庸人自扰
庸人自扰 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:46

    Using scipy, you could use stats.gaussian_kde to estimate the probability density function:

    import matplotlib.pyplot as plt
    import numpy as np
    import scipy.stats as stats
    
    noise = np.random.normal(0, 1, (1000, ))
    density = stats.gaussian_kde(noise)
    n, x, _ = plt.hist(noise, bins=np.linspace(-3, 3, 50), 
                       histtype=u'step', density=True)  
    plt.plot(x, density(x))
    plt.show()
    

    enter image description here

提交回复
热议问题