How to locate the median in a (seaborn) KDE plot?

前端 未结 1 445
青春惊慌失措
青春惊慌失措 2020-12-29 07:09

I am trying to do a Kernel Density Estimation (KDE) plot with seaborn and locate the median. The code looks something like this:

import seaborn as sns
impor         


        
相关标签:
1条回答
  • 2020-12-29 07:32

    You need to:

    1. Extract the data of the kde line
    2. Integrate it to calculate the cumulative distribution function (CDF)
    3. Find the value that makes CDF equal 1/2, that is the median
    import numpy as np
    import scipy
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    sns.set_palette("hls", 1)
    data = np.random.randn(30)
    p=sns.kdeplot(data, shade=True)
    
    x,y = p.get_lines()[0].get_data()
    
    #care with the order, it is first y
    #initial fills a 0 so the result has same length than x
    cdf = scipy.integrate.cumtrapz(y, x, initial=0)
    
    nearest_05 = np.abs(cdf-0.5).argmin()
    
    x_median = x[nearest_05]
    y_median = y[nearest_05]
    
    plt.vlines(x_median, 0, y_median)
    plt.show()
    

    Result

    0 讨论(0)
提交回复
热议问题