import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
# 假设你有两组不同的 (x, y) 数据
x1 = np.array([1, 2, 3, 4, 5])
y1 = np.array([1, 4, 9, 16, 25]) # y1 对应 x1
x2 = np.array([1.5, 2.5, 3.5, 4.5, 5.5])
y2 = np.array([2, 3, 5, 7, 11]) # y2 对应 x2
# 计算每个数据组的均值和标准差
mean1, std1 = np.mean(x1), np.std(x1)
mean2, std2 = np.mean(x2), np.std(x2)
# 创建用于绘制正态分布的 x 值范围
x_min = min(x1.min(), x2.min())
x_max = max(x1.max(), x2.max())
x_values = np.linspace(x_min, x_max, 1000)
# 计算每个数据组的正态分布 PDF
norm_pdf1 = norm.pdf(x_values, mean1, std1)
norm_pdf2 = norm.pdf(x_values, mean2, std2)
# 绘制第一个正态分布曲线
plt.plot(x_values, norm_pdf1, label=f'N(μ={mean1:.2f}, σ={std1:.2f})')
# 绘制第二个正态分布曲线
plt.plot(x_values, norm_pdf2, label=f'N(μ={mean2:.2f}, σ={std2:.2f})')
# 添加图例
plt.legend()
# 添加标题和轴标签
plt.title('Two Normal Distribution Curves')
plt.xlabel('X Axis')
plt.ylabel('Probability Density')
# 显示网格
plt.grid(True)
# 显示图形
plt.show()
将x和y都不同的两组数据画两个折线正态分布图在一个画上
最新推荐文章于 2024-06-21 12:52:40 发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)