0

I visualize density function (PDF) using two plotting approaches: displot() and plot(). I don't understand why displot() doesn't produce normally distributed plot wheras plot() do this perfectly. Density plots should look alike but they don't. What's wrong with displot() here?

from scipy.stats import norm
import seaborn as sns 
import numpy as np

data_x= np.arange(-4, 4, 0.001)
norm_pdf = norm.pdf(data_x)
sns.displot(data = norm_pdf, x = data_x, kind='kde')

enter image description here

from scipy.stats import norm
import matplotlib.pyplot as plt
import numpy as np

data_x= np.arange(-4, 4, 0.001)
plt.plot(data_x, norm.pdf(data_x))
plt.show()

enter image description here

1 Answer 1

2

displot (or the underlying kdeplot) creates an approximation of a probability density function (pdf) to resemble the function that might have generated the given random data. As input, you'll need random data. The function will mimic these data as a sum of Gaussian bell shapes (a "kernel density estimation" with a Gaussian kernel).

Here is an example using 8000 random points as input. You'll notice the curve resembles the normal pdf, but is also a bit "bumpier" (that's how randomness looks like).

data_x = norm.rvs(size=8000)
sns.kdeplot(x=data_x)

kdeplot of random normal data

When you call kdeplot (or displot(..., kind='kde')) with both data= and x=, while x= isn't a columnname in a dataframe, data= gets ignored. So, you are using 8000 evenly distributed values between -4 and 4. The kde of such data looks like a flat line between -4 and 4. But as the kde supposes the underlying function locally resembles a Gaussian, the start and end are smoothed out.

data_x = np.arange(-4, 4, 0.001)
sns.kdeplot(x=data_x)

kdeplot of np.arange

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, that expains a lot. So summing up: displot works fine only with random data generated from distribution (e.g. norm.rvs) or with dataset and dataframe column. This is how I think it works
Rephrasing "works fine for" to "is meant for" would be closer. displot doesn't draw pdfs, it draws kdes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.