4

I'd like to ask how to draw the Probability Density Function (PDF) plot in Python.

This is my codes.

import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import scipy.stats as stats

.

x = np.random.normal(50, 3, 1000)
source = {"Genotype": ["CV1"]*1000, "AGW": x}
df=pd.DataFrame(source)
df

enter image description here

I generated a data frame. Then, I tried to draw a PDF graph.

df["AGW"].sort_values()
df_mean = np.mean(df["AGW"])
df_std = np.std(df["AGW"])
pdf = stats.norm.pdf(df["AGW"], df_mean, df_std)

plt.plot(df["AGW"], pdf)

enter image description here

I obtained above graph. What I did wrong? Could you let me how to draw the Probability Density Function (PDF) Plot which is also known as normal distribution graph.

Could you let me know which codes (or library) I need to use to draw the PDF graph?

Always many thanks!!

1 Answer 1

6

You just need to sort the values (not really check what's after edit)

pdf = stats.norm.pdf(df["AGW"].sort_values(), df_mean, df_std)

plt.plot(df["AGW"].sort_values(), pdf)

And it will work.

The line df["AGW"].sort_values() doesn't change df. Maybe you meant df.sort_values(by=['AGW'], inplace=True). In that case the full code will be :

import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import scipy.stats as stats

x = np.random.normal(50, 3, 1000)
source = {"Genotype": ["CV1"]*1000, "AGW": x}
df=pd.DataFrame(source)

df.sort_values(by=['AGW'], inplace=True)
df_mean = np.mean(df["AGW"])
df_std = np.std(df["AGW"])
pdf = stats.norm.pdf(df["AGW"], df_mean, df_std)

plt.plot(df["AGW"], pdf)

Which gives :

output

Edit :

I think here we already have the distribution (x is normally distributed) so we dont need to generate the pdf of x. As the use of the pdf is for something like this :

mu = 50
variance = 3
sigma = math.sqrt(variance)
x = np.linspace(mu - 5*sigma, mu + 5*sigma, 1000)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.show()

Here we dont need to generate the distribution from x points, we only need to plot the density of the distribution we already have . So you might use this :

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
x = np.random.normal(50, 3, 1000)  #Generating Data
source = {"Genotype": ["CV1"]*1000, "AGW": x}
df=pd.DataFrame(source) #Converting to pandas DataFrame
df.plot(kind = 'density'); # or df["AGW"].plot(kind = 'density');

Which gives :

density

You might use other packages if you want, like seaborn :

import seaborn as sns
plt.figure(figsize = (5,5))
sns.kdeplot(df["AGW"] , bw = 0.5 , fill = True)
plt.show()

density 2

Or this :

import seaborn as sns
sns.set_style("whitegrid")  # Setting style(Optional)
plt.figure(figsize = (10,5)) #Specify the size of figure
sns.distplot(x = df["AGW"]   ,  bins = 10 , kde = True , color = 'teal'
            , kde_kws=dict(linewidth = 4 , color = 'black')) #kde for normal distribution
plt.show()

density 3

Check this article for more.

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

7 Comments

Hi, since x is a numpy array .sort_values() won't work. Use x.sort() exactly after the definition of x. like this :x = np.random.normal(50, 3, 1000) x.sort() ...
@Jin.W.Kim isn't there an error though, I dont mean the code, but I mean x having a normal distribution then ploting its pdf (it's like a normal distribution of a normal distribution). Isn't it supposed to be something like this : x = np.linspace(mu - 3*sigma, mu + 3*sigma, 1000) plt.plot(x, stats.norm.pdf(x, mu, sigma)) plt.show() , so x is the input on the x axis it doesnt have a normal distribution. y=stats.norm.pdf(x, mu, sigma)) , y on the other hand is the one holding the distribution.
See the what's after Edit, in my answer. It depends on what you need/ want , if you have a distribution already and you want to plot its density you use what's after Edit (you already have the distribution you just plot its density , no need to generate it). But if you dont have the distribution and you want to plot the pdf then you can use ` x = np.linspace(np.mean(x) - 3np.std(x), np.mean(x) + 3np.std(x), 1000) plt.plot(x, stats.norm.pdf(x, np.mean(x), np.std(x)), color="Black") plt.show()` But in this case x doesnt have a normal distribution , it's just some points on the x axis.
@Jin.W.Kim It's a density graph but it's also a normal distribution graph since the data is normally distributed, this is shown better with ` sns.kdeplot(df["AGW"] , bw = 0.5 , fill = True)` (second image after Edit)
Thank you so much!!! I understood what the codes mean. Thanks a lot!! You're my hero!!!
|

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.