origin绘制折线图降噪
时间: 2025-06-20 22:30:21 浏览: 35
### 数据降噪在折线图中的应用
为了在绘制折线图时进行数据降噪处理,可以采用多种技术来平滑原始数据。以下是几种常见的方法及其实现方式:
#### 方法一:移动平均法
通过计算固定窗口内的均值来进行数据平滑化。这种方法简单易懂,适用于去除高频噪声。
```python
import numpy as np
def moving_average(data, window_size=3):
weights = np.ones(window_size) / window_size
smoothed_data = np.convolve(data, weights, mode='valid')
return smoothed_data
```
上述代码实现了简单的移动平均算法[^1]。`window_size` 参数决定了用于平滑化的样本数量。
#### 方法二:指数加权移动平均 (EWMA)
相比于普通的移动平均,指数加权移动平均赋予最近的数据更高的权重,从而更好地反映趋势变化。
```python
import pandas as pd
def ewma_smooth(data, span=7):
series = pd.Series(data)
smoothed_series = series.ewm(span=span, adjust=False).mean()
return smoothed_series.tolist()
```
此函数利用 Pandas 的 `ewm` 函数完成指数加权移动平均操作[^2]。
#### 方法三:Savitzky-Golay 滤波器
这是一种基于多项式的局部回归滤波器,能够有效保留信号特征的同时减少噪声影响。
```python
from scipy.signal import savgol_filter
def savitzky_golay_smoothing(data, window_length=5, polyorder=2):
smoothed_data = savgol_filter(data, window_length, polyorder)
return smoothed_data
```
该方法适合于需要保持曲线形状的应用场景[^3]。
#### 绘制平滑后的折线图
无论选择了哪种降噪方法,在完成数据平滑之后都可以借助 Matplotlib 来展示效果。
```python
import matplotlib.pyplot as plt
# 原始数据和平滑后数据对比绘图
plt.figure(figsize=(10, 6))
plt.plot(original_data, label="Original Data", color="gray", alpha=0.5)
plt.plot(smoothed_data, label="Smoothed Data", color="blue")
plt.legend()
plt.show()
```
以上展示了如何将经过不同降噪手段得到的结果可视化出来[^4]。
阅读全文
相关推荐
















