Search code examples
matplotlibseaborndistribution

Seaborn: how to draw a vertical line that matches a specific y value in a cumulative KDE?


I'm using Seaborn to plot a cumulative distribution and it's KDE using this code:

sns.distplot(values, bins=20, 
             hist_kws= {'cumulative': True}, 
             kde_kws= {'cumulative': True} )

This gives me the following chart:

Cumulative distribution and kde

I'd like to plot a vertical line and the corresponding x index where y is 0.8. Something like:

KDE with vertical line

How do I get the x value of a specific y?


Solution

  • Update for newer Seaborn versions, with sns.histplot

    The code below has been tested with Seaborn 0.13.2. The 0.8 quantile is calculated and shown as an annotated vertical line.

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    
    values = np.random.normal(1, 20, 1000)
    ax = sns.histplot(values, bins=20, cumulative=True, kde=True, color='salmon')
    perc = 80
    x80 = np.quantile(values, perc / 100)
    ax.axvline(x80, color='b')
    ax.text(x80, 0.98, f"{perc}th percentile: \nx={x80:.2f} ", color='b',
            ha='right', va='top', transform=ax.get_xaxis_transform())
    ax.margins(x=0)
    plt.show()
    

    cumulative sns.histplot with 80th percentile

    Old answer

    You could draw a vertical line at the 80% quantile:

    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as sns
    
    values = np.random.normal(1, 20, 1000)
    sns.distplot(values, bins=20,
                 hist_kws= {'cumulative': True},
                 kde_kws= {'cumulative': True} )
    plt.axvline(np.quantile(values, 0.8), color='r')
    plt.show()
    

    example plot