高斯拟合曲线python
时间: 2023-11-29 14:47:38 浏览: 153
高斯拟合曲线是一种常用的数据拟合方法,可以用于对实验数据进行拟合,从而得到数据的分布规律。以下是使用Python进行高斯拟合曲线的方法:
1. 导入必要的库
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
```
2. 定义高斯函数
```python
def gaussian(x, a, x0, sigma):
return a * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))
```
3. 生成数据
```python
x = np.linspace(-10, 10, 100)
y = gaussian(x, 1, 0, 1) + 0.1 * np.random.normal(size=len(x))
```
4. 拟合数据
```python
popt, pcov = curve_fit(gaussian, x, y)
```
5. 绘制拟合曲线
```python
plt.plot(x, y, 'b+:', label='data')
plt.plot(x, gaussian(x, *popt), 'r-', label='fit')
plt.legend()
plt.title('Gaussian fit')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
```
以上代码将生成一个高斯拟合曲线的图像,其中蓝色的线条表示原始数据,红色的线条表示拟合曲线。
阅读全文
相关推荐


















