python: plotting a histogram with a function line on top

后端 未结 3 1529
情书的邮戳
情书的邮戳 2021-02-04 04:35

I\'m trying to do a little bit of distribution plotting and fitting in Python using SciPy for stats and matplotlib for the plotting. I\'m having good luck with some things like

3条回答
  •  天涯浪人
    2021-02-04 05:32

    just put both pieces together.

    import scipy.stats as ss
    import numpy as np
    import matplotlib.pyplot as plt
    alpha, loc, beta=5, 100, 22
    data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
    myHist = plt.hist(data, 100, normed=True)
    rv = ss.gamma(alpha,loc,beta)
    x = np.linspace(0,600) 
    h = plt.plot(x, rv.pdf(x), lw=2)
    plt.show()
    

    enter image description here

    to make sure you get what you want in any specific plot instance, try to create a figure object first

    import scipy.stats as ss
    import numpy as np
    import matplotlib.pyplot as plt
    # setting up the axes
    fig = plt.figure(figsize=(8,8))
    ax  = fig.add_subplot(111)
    # now plot
    alpha, loc, beta=5, 100, 22
    data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=5000)
    myHist = ax.hist(data, 100, normed=True)
    rv = ss.gamma(alpha,loc,beta)
    x = np.linspace(0,600)
    h = ax.plot(x, rv.pdf(x), lw=2)
    # show
    plt.show()
    

提交回复
热议问题