本文介绍了将正态分布拟合到一维数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个1维数组,我可以计算该样本的均值"和标准差",并绘制正态分布",但是我有一个问题:

I have a 1 Dimentional array and I can compute the "mean" and "standard deviation" of this sample and plot the "Normal distribution" but I have a problems:

我想在同一图中绘制数据和正态分布,如下所示:

I want to plot the data and Normal distribution in the same figure like below :

我不知道如何同时绘制数据"和正态分布"

I dont know how to plot both the "DATA" and the "Normal Distribution"

关于"scipy.stats中的高斯概率密度函数"的任何想法吗?

any Idea about "Gaussian probability density function in scipy.stats"?

s = np.std(array)
m = np.mean(array)
plt.plot(norm.pdf(array,m,s))

推荐答案

您可以使用matplotlib绘制直方图和PDF(如@MrE答案中的链接).为了适合和计算PDF,可以使用scipy.stats.norm,如下所示.

You can use matplotlib to plot the histogram and the PDF (as in the link in @MrE's answer). For fitting and for computing the PDF, you can use scipy.stats.norm, as follows.

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


# Generate some data for this demonstration.
data = norm.rvs(10.0, 2.5, size=500)

# Fit a normal distribution to the data:
mu, std = norm.fit(data)

# Plot the histogram.
plt.hist(data, bins=25, density=True, alpha=0.6, color='g')

# Plot the PDF.
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
plt.title(title)

plt.show()

这是脚本生成的图:

这篇关于将正态分布拟合到一维数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:57