pythonmatplotlibseabornecdf

how to find x value on a intersection point of axhline on seaborn ecdf plot?


I have a ecdf plot like this:

penguins = sns.load_dataset("penguins")
fig, ax = plt.subplots(figsize = (10,8))
sns.ecdfplot(data=penguins, x="bill_length_mm", hue="species")
ax.axhline(.25, linestyle = '--', color ='#cfcfcf', lw = 2, alpha = 0.75)

enter image description here

how to find the x values on this intersecting axhline?


Solution

  • You could loop through the generated curves (ax.get_lines()), extract their coordinates and search for the index of the first y-value larger than the desired y-value.

    Here is some illustrating code (note that sns.ecdfplot() should get ax as parameter):

    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as sns
    
    penguins = sns.load_dataset("penguins")
    fig, ax = plt.subplots(figsize=(10, 8))
    sns.ecdfplot(data=penguins, x="bill_length_mm", hue="species", ax=ax)
    y_special = 0.25
    for line in ax.get_lines():
        x, y = line.get_data()
        ind = np.argwhere(y >= y_special)[0, 0]  # first index where y is larger than y_special
        # x[ind] is the desired x-value
        ax.text(x[ind], y_special, f' {x[ind]:.1f}', ha='left', va='top') # maybe color=line.get_color()
    ax.axhline(y_special, linestyle='--', color='#cfcfcf', lw=2, alpha=0.75)
    plt.show()
    

    example plot

    PS: Optionally you could add these x-values to the legend:

    for line, legend_text in zip(ax.get_lines(), ax.legend_.get_texts()):
        x, y = line.get_data()
        ind = np.argwhere(y >= y_special)[0, 0]
        legend_text.set_text(f'{x[ind]:5.2f} {legend_text.get_text()}')