2

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!!

Jin.w.Kim
  • 599
  • 1
  • 4
  • 15

1 Answers1

3

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.

Anass
  • 396
  • 2
  • 10
  • Hi!! I have an extra question. In case of the below code, x = np.random.normal(4, 0.8, 1000) plt.plot(x, stats.norm.pdf(x, np.mean(x), np.std(x)), color="Black") It has the same problem I had before. When I add the code .sort_values(), it says that 'numpy.ndarray' object has no attribute 'sort_values'. Could you let me know how I can draw a PDF curve from random numbers which I generated? Many thanks!!! – Jin.w.Kim Mar 06 '22 at 12:51
  • 1
    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()` `...` – Anass Mar 06 '22 at 13:16
  • 1
    @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. – Anass Mar 06 '22 at 13:23
  • Thank you so much for your reply. If I do like this, 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(), it works. Does it mean I don't need to generate random numbers to draw a PDF curve? I mean I can just draw a PDF curve with given mean and Stdev, isn't it? – Jin.w.Kim Mar 06 '22 at 13:37
  • 1
    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. – Anass Mar 06 '22 at 13:45
  • 1
    @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) – Anass Mar 06 '22 at 13:55
  • 1
    Thank you so much!!! I understood what the codes mean. Thanks a lot!! You're my hero!!! – Jin.w.Kim Mar 06 '22 at 13:59