📜  如何在Python制作钟形曲线?

📅  最后修改于: 2022-05-13 01:55:17.691000             🧑  作者: Mango

如何在Python制作钟形曲线?

统计学中的钟形曲线对应于正态分布或以德国数学家卡尔·弗里德里希·高斯命名的高斯分布。在正态分布中,点集中在平均值上,大多数点位于平均值附近。钟形曲线的方向取决于一组给定输入点的平均值和标准偏差值。通过改变平均值,我们可以改变曲线在轴上的位置,曲线的形状可以通过改变标准偏差值来操纵。在本文中,我们将学习在Python绘制钟形曲线。

在正态分布中,均值、中值和众数都相等,钟形曲线关于均值(即 y 轴)对称。正态分布的概率密度函数使用以下公式计算:

f(x,\mu,\sigma ) = \frac{1}{\sigma\sqrt{2\pi}} \times e^{\frac{-1}{2}\times(\frac{x-u}{\sigma})^2}

在哪里:

x = 输入点,



\mu = 平均值

\sigma = 输入值集的标准偏差

示例 1:创建简单的钟形曲线。

方法:我们将在 x 轴上创建一个点列表,并将这些点传递到我们的自定义 pdf函数以生成概率分布函数以生成与 x 中每个点对应的 y 值。现在我们使用 matplotlib 库中提供的plot()scatter()方法绘制曲线。 plot() 方法用于绘制线图,而 scatter() 方法用于在图形内创建虚线。

代码:

Python
# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
  
# A custom function to calculate
# probability distribution function
def pdf(x):
    mean = np.mean(x)
    std = np.std(x)
    y_out = 1/(std * np.sqrt(2 * np.pi)) * np.exp( - (x - mean)**2 / (2 * std**2))
    return y_out
    
# To generate an array of x-values
x = np.arange(-2, 2, 0.1)
  
# To generate an array of
# y-values using corresponding x-values
y = pdf(x)
  
# Plotting the bell-shaped curve
plt.style.use('seaborn')
plt.figure(figsize = (6, 6))
plt.plot(x, y, color = 'black',
         linestyle = 'dashed')
  
plt.scatter( x, y, marker = 'o', s = 25, color = 'red')
plt.show()


Python
# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
  
# A custom function to calculate
# probability distribution function
def pdf(x):
    mean = np.mean(x)
    std = np.std(x)
    y_out = 1/(std * np.sqrt(2 * np.pi)) * np.exp( - (x - mean)**2 / (2 * std**2))
    return y_out
  
# To generate an array of x-values
x = np.arange(-2, 2, 0.1)
  
# To generate an array of 
# y-values using corresponding x-values
y = pdf(x)
  
# To fill in values under the bell-curve
x_fill = np.arange(-2, 2, 0.1)
y_fill = pdf(x_fill)
  
# Plotting the bell-shaped curve
plt.style.use('seaborn')
plt.figure(figsize = (6, 6))
plt.plot(x, y, color = 'black',
         linestyle = 'dashed')
  
plt.scatter(x, y, marker = 'o',
            s = 25, color = 'red')
  
plt.fill_between(x_fill, y_fill, 0,
                 alpha = 0.2, color = 'blue')
plt.show()


输出:

钟形曲线

示例 2:填充钟形曲线下方的区域。



我们还可以填充钟形曲线下方的区域,为此我们将使用 matplotlib 库中的fill_between()函数为两条曲线之间的区域着色。

fill_between()函数接受多个参数,例如 x 值、y 值,它们是图形上点和线的坐标。此外,它还支持一些特定于图形的参数,例如决定颜色不透明度的 'alpha' 和 'color' 属性接受要在曲线下填充的颜色的名称。

方法:我们在 x 轴上获取一个点列表,并将这些点传递到我们的自定义 pdf函数以生成概率分布函数,以生成与 x 中每个点对应的 y 值。同样,为了填充曲线下方的区域,我们选择一系列 x_fill 值并生成概率分布。现在我们首先使用 plot() 和 scatter() 方法绘制曲线,然后使用 fill_between() 方法填充曲线下方的区域。

代码:

Python

# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
  
# A custom function to calculate
# probability distribution function
def pdf(x):
    mean = np.mean(x)
    std = np.std(x)
    y_out = 1/(std * np.sqrt(2 * np.pi)) * np.exp( - (x - mean)**2 / (2 * std**2))
    return y_out
  
# To generate an array of x-values
x = np.arange(-2, 2, 0.1)
  
# To generate an array of 
# y-values using corresponding x-values
y = pdf(x)
  
# To fill in values under the bell-curve
x_fill = np.arange(-2, 2, 0.1)
y_fill = pdf(x_fill)
  
# Plotting the bell-shaped curve
plt.style.use('seaborn')
plt.figure(figsize = (6, 6))
plt.plot(x, y, color = 'black',
         linestyle = 'dashed')
  
plt.scatter(x, y, marker = 'o',
            s = 25, color = 'red')
  
plt.fill_between(x_fill, y_fill, 0,
                 alpha = 0.2, color = 'blue')
plt.show()

输出:

显示钟形曲线下填充区域的图