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

Question:

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?

Asked By: JaySabir

||

Answers:

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()}')
Answered By: JohanC

This is a case where it’s better to use the computational tools that pandas provides instead of trying to back quantitative values out from a visual representation.

If you want the values corresponding to the .25 quantile for each species, you should do:

penguins.groupby("species")["bill_length_mm"].quantile(.25)

which returns

species
Adelie       36.75
Chinstrap    46.35
Gentoo       45.30
Name: bill_length_mm, dtype: float64
Answered By: mwaskom