如何在Python中从数据框制作正态分布图?

发布于 2025-01-10 07:14:41 字数 963 浏览 6 评论 0原文

我的问题是如何从Python中的数据框制作正态分布图。我可以找到很多信息来从随机数制作这样的图表,但我不知道如何从数据框制作它。

首先,我生成了随机数并制作了一个数据框。

import numpy as np
import pandas 
from pandas import DataFrame

cv1 = np.random.normal(50, 3, 1000)

source = {"Genotype": ["CV1"]*1000, "AGW": cv1}
Cultivar_1=DataFrame(source)

输入图片description here

然后,我尝试制作正态分布图。

sns.kdeplot(data = Cultivar_1['AGW'])
plt.xlim([30,70])  
plt.xlabel("Grain weight (mg)", size=12)    
plt.ylabel("Frequency", size=12)                
plt.grid(True, alpha=0.3, linestyle="--")     
plt.show()

输入图片此处描述

但是,这是密度图,而不是使用均值标准差计算的正态分布图。

你能告诉我需要使用哪些代码来制作正态分布图吗?

谢谢!!

my question is how to make a normal distribution graph from data frame in Python. I can find many information to make such a graph from random numbers, but I don't know how to make it from data frame.

First, I generated random numbers and made a data frame.

import numpy as np
import pandas 
from pandas import DataFrame

cv1 = np.random.normal(50, 3, 1000)

source = {"Genotype": ["CV1"]*1000, "AGW": cv1}
Cultivar_1=DataFrame(source)

enter image description here

Then, I tried to make a normal distribution graph.

sns.kdeplot(data = Cultivar_1['AGW'])
plt.xlim([30,70])  
plt.xlabel("Grain weight (mg)", size=12)    
plt.ylabel("Frequency", size=12)                
plt.grid(True, alpha=0.3, linestyle="--")     
plt.show()

enter image description here

However, this is a density graph, not a normal distribution graph which is calculated using mean and standard deviation.

Could you let me know which codes I need to use to make a normal distribution graph?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴梦一场 2025-01-17 07:14:42

这是在 python 中从数据帧创建正态分布图的可能方法之一。

#Loading dependencies
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats

# Generating the dataframe
cv1 = np.random.normal(50, 3, 1000)
source = {"Genotype": ["CV1"]*1000, "AGW": cv1}
dataframe = pd.DataFrame(source)

# Calculating the mean and standard deviation of the parameter "AGW":
mean = dataframe["AGW"].mean()
std = dataframe["AGW"].std()
s = np.random.normal(mean, std, 100) 

# This mean and standard deviation will be useful to create the normal distribution graph

# Creating the normal distribution graph for the column "AGW"
count, bins, ignored = plt.hist(s, 100, density=True)

# Mathematical representation/formula of the normal distribution
plt.plot(bins, 1/(std * np.sqrt(2 * np.pi)) *
                       np.exp( - (bins - mean)**2 / (2 * std**2) ),
                 linewidth=2, color='r')

# This is the direct function used in stats
pdf = stats.norm.pdf(dataframe["AGW"].sort_values(), mean, std)
plt.plot(dataframe["AGW"].sort_values(), pdf)
plt.xlabel("Grain weight (mg)", size=12)
plt.ylabel("Frequency", size=12)
plt.xlim([30,70]) 
plt.grid(True, alpha=0.3, linestyle="--")
plt.show()

This is one of the possible way to create normal distribution graph from data frame in python.

#Loading dependencies
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats

# Generating the dataframe
cv1 = np.random.normal(50, 3, 1000)
source = {"Genotype": ["CV1"]*1000, "AGW": cv1}
dataframe = pd.DataFrame(source)

# Calculating the mean and standard deviation of the parameter "AGW":
mean = dataframe["AGW"].mean()
std = dataframe["AGW"].std()
s = np.random.normal(mean, std, 100) 

# This mean and standard deviation will be useful to create the normal distribution graph

# Creating the normal distribution graph for the column "AGW"
count, bins, ignored = plt.hist(s, 100, density=True)

# Mathematical representation/formula of the normal distribution
plt.plot(bins, 1/(std * np.sqrt(2 * np.pi)) *
                       np.exp( - (bins - mean)**2 / (2 * std**2) ),
                 linewidth=2, color='r')

# This is the direct function used in stats
pdf = stats.norm.pdf(dataframe["AGW"].sort_values(), mean, std)
plt.plot(dataframe["AGW"].sort_values(), pdf)
plt.xlabel("Grain weight (mg)", size=12)
plt.ylabel("Frequency", size=12)
plt.xlim([30,70]) 
plt.grid(True, alpha=0.3, linestyle="--")
plt.show()
初与友歌 2025-01-17 07:14:41

我找到了一种从数据帧制作正态分布图的解决方案。

#Library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats

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

# Calculating mean and Stdev of AGW
df_mean = np.mean(df["AGW"])
df_std = np.std(df["AGW"])
 
# Calculating probability density function (PDF)
pdf = stats.norm.pdf(df["AGW"].sort_values(), df_mean, df_std)

# Drawing a graph
plt.plot(df["AGW"].sort_values(), pdf)
plt.xlim([30,70])  
plt.xlabel("Grain weight (mg)", size=12)    
plt.ylabel("Frequency", size=12)                
plt.grid(True, alpha=0.3, linestyle="--")
plt.show()

输入图像描述这里

I found one solution to make a normal distribution graph from data frame.

#Library
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats

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

# Calculating mean and Stdev of AGW
df_mean = np.mean(df["AGW"])
df_std = np.std(df["AGW"])
 
# Calculating probability density function (PDF)
pdf = stats.norm.pdf(df["AGW"].sort_values(), df_mean, df_std)

# Drawing a graph
plt.plot(df["AGW"].sort_values(), pdf)
plt.xlim([30,70])  
plt.xlabel("Grain weight (mg)", size=12)    
plt.ylabel("Frequency", size=12)                
plt.grid(True, alpha=0.3, linestyle="--")
plt.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文