Michael Berry
Michael Berry

Reputation: 1003

extracting data from sns.kdeplot

Is it possible to extract the data from a sns.kdeplot() before plotting? ie. without using the function y.get_lines()[0].get_data() post plotting

Upvotes: 4

Views: 10192

Answers (2)

Woldemar G
Woldemar G

Reputation: 156

Based on statsmodels's documentation:

import numpy as np
import seaborn as sns
import statsmodels.api as sm
import matplotlib.pyplot as plt

# generate bimodal disrtibution
X1 = np.random.normal(100, 10, 250)
X2 = np.random.normal(10, 20, 250)
X = np.concatenate([X1, X2])

# get density from seaborn
x, y = sns.kdeplot(X).lines[0].get_data()

# get density from statsmodel
kde = sm.nonparametric.KDEUnivariate(X).fit()
xx, yy = (kde.support, kde.density)

# compare outputs
plt.plot(x, y, label='from sns')
plt.plot(xx, yy, label='from statsmodels')
plt.legend()

enter image description here

Upvotes: 1

lewiso1
lewiso1

Reputation: 178

This can be done by extracting the line data from the matplotlib Axes object:

import numpy as np
from seaborn import kdeplot

my_data = np.random.randn(1000)
my_kde = kdeplot(my_data)
line = my_kde.lines[0]
x, y = line.get_data()

fig, ax = plt.subplots()
ax.plot(x[x>0], y[x>0])

alternatively the statsmodels way:

import statsmodels.api as sm

dens = sm.nonparametric.KDEUnivariate(np.random.randn(1000))
dens.fit()
x =np.linspace(0,1,100) #restrict range to (0,1)
y = dens.evaluate(x)
plt.plot(x,y)

Upvotes: 4

Related Questions