代码之家  ›  专栏  ›  技术社区  ›  abu

在seaborn直方图上添加标准普通pdf

  •  0
  • abu  · 技术社区  · 6 年前

    我想添加一个标准的正常pdf曲线上的直方图建立与 seaborn .

    import numpy as np
    import seaborn as sns 
    x = np.random.standard_normal(1000)
    sns.distplot(x, kde = False)
    

    任何帮助都将不胜感激!

    1 回复  |  直到 6 年前
        1
  •  7
  •   Bonlenfum    6 年前

    scipy.stats.norm 可以方便地访问正态分布的pdf

    为了使其与采样数据正确对应,直方图应该
    显示密度而不是计数,所以使用 norm_hist=True seaborn.distplot 打电话。

    import numpy as np                                                              
    import seaborn as sns                                                           
    from scipy import stats                                                         
    import matplotlib.pyplot as plt                                                 
    
    x = np.random.standard_normal(1000)                                             
    ax = sns.distplot(x, kde = False, norm_hist=True)                                    
    
    # calculate the pdf over a range of values
    xx = np.arange(-4, +4, 0.001)                                                   
    yy = stats.norm.pdf(xx)                                                         
    # and plot on the same axes that seaborn put the histogram
    ax.plot(xx, yy, 'r', lw=2)                                                     
    

    sample and theoretical distribution