使用多条曲线使函数平滑

时间:2018-12-06 14:10:28

标签: python

我有一个要绘制的函数,但是它具有多条曲线,所以我不能使用曲线拟合,也不知道如何平滑它?任何帮助

这是我的代码

import math
import matplotlib.pyplot as plt

Int = [0,0,0,0,0,0,0.02,0.04,0.06,0,0,0.02,0.32,0.04,0,0,0.18,0.78,
0.76,0.2,0,0,0.3,0.3,0.1,0,0,0.04,0,0.06,0.0,0,0,0,0]
Angle = [i for i in range(-85,90,5)]
x = Angle
y = Int

plt.plot(x,y,"r")
plt.xlabel("Angle")
plt.ylabel("Intensity (mA)")
plt.title("Intensity vs Angle")
plt.grid(b=True, which='major', color='k', linestyle='-')
plt.grid(b=True, which='minor', color='r', linestyle='-', alpha=0.2)
plt.minorticks_on()
plt.show()

2 个答案:

答案 0 :(得分:0)

scipy.signals中,您会发现信号预处理和滤波的不同技术。

一个简单的过滤器是Savitzky-Golay-Filter:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.savgol_filter.html#scipy.signal.savgol_filter

它通过移动窗口浏览数据,即向后看n个值,在其位置前面n个值。一个点离目标点越远,对平滑的影响就越小。窗口的大小由window_length指定,距离减小的强度由polyorder指定。

在您的绘图部分之前添加以下代码:

import scipy.signal
y_filtered = scipy.signal.savgol_filter(x=y, window_length=3, polyorder=1)
plt.plot(x,y_filtered,"b")

试玩window_length和polyorder。请注意,polyorder必须小于window_length,较高的window_lengths可能会严重限制您的数据。

Resulting plot

答案 1 :(得分:0)

我设法做到了,我找到了一个youtube视频..

这里是新代码,我想这样绘制它

is_test = 1

但是谢谢您的帮助