1

I am trying to plot mean line using displot. I am trying to replicate this example but getting error.

    from matplotlib import pyplot

g = sns.displot(data=df, x='age', kind="kde", hue="attrition", palette="crest", fill=True,alpha=.7, linewidth=0,)

g.fig.set_figwidth(15)
g.fig.set_figheight(5)
kdeline = g.lines[0]

mean = x.mean()
height = np.interp(mean, kdeline.get_xdata(), kdeline.get_ydata())
g.vlines(mean, 0, height, color='crimson', ls=':')
g.set_ylim(ymin=0)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-26-86084b5eb03a> in <module>
      5 g.fig.set_figwidth(15)
      6 g.fig.set_figheight(5)
----> 7 kdeline = g.lines[0]
      8 mean = x.mean()
      9 height = np.interp(mean, kdeline.get_xdata(), kdeline.get_ydata())

AttributeError: 'FacetGrid' object has no attribute 'lines'  
Asra Khalid
  • 177
  • 1
  • 18

1 Answers1

1

You could use plt.axvline(mean). Please see the below example with the planets dataset:

from matplotlib import pyplot
import seaborn as sns
df = sns.load_dataset('planets')
g = sns.displot(data=df, x='year', kind="kde", hue="method", palette="crest", fill=True,alpha=.7, linewidth=0,)
g.fig.set_figwidth(15)
g.fig.set_figheight(5)
mean = df['year'].mean()
plt.axvline(mean)

enter image description here

David Erickson
  • 16,433
  • 2
  • 19
  • 35