Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to extract xvalues and yvalues from seaborn kdeplot

Given that I have a seaborn.kdeplot I want to extract the x and y points. Based on similar questions I have tried the following:

points = sns.kdeplot(targets, shade=True, label='train').get_lines()[0].get_data()
x      = points[0]
y      = points[1]

But I’m getting the error

Traceback (most recent call last):
  File "<string>", line 10, in <module>
IndexError: list index out of range

I’m using seaborn==0.12.2 and matplotlib==3.7.1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because you request shade=True (use fill=True in the latest seaborn versions), the output axes object does not contain a Line2D object (it instead contains a PolyCollection). If you remove the shade=True, you be able to extract the data in the way that you give:

ax = sns.kdeplot(targets, label='train')
points = ax.get_lines()[0].get_data()
x = points[0]
y = points[1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading