Sib*_*ing 8 python numpy matplotlib scipy
我有一个时间依赖的信号.
我希望随着时间的推移绘制其积分,其中时间为x轴,积分值为y轴.
有没有Python方法这样做?
更具体:
我有一个时间数组,time
和一个信号数组,signal
.它们具有相同的维度.
我需要整合signal
在time
同scipy.integrate.trapz()
.
我希望看到随着时间的推移积分变化,而不是得到最终积分.
请尝试使用scipy.integrate.cumtrapz()
:
plt.plot(time[:-1], scipy.integrate.cumtrapz(signal, x=time))
plt.show()
Run Code Online (Sandbox Code Playgroud)
它计算包含累积积分值的数组.
http://docs.scipy.org/doc/scipy-0.10.1/reference/generated/scipy.integrate.trapz.html
一个稍微好一点的答案是使用可选"initial"
参数。这是一个完整的例子:
import scipy.integrate as it
import numpy as np
import matplotlib.pyplot as plt
t=np.linspace(0,1, 100)
y=t**2
y_int = it.cumtrapz( y , t, initial=0.0) # y_int is same size as t
plt.plot(t, y_int)
plt.show()
Run Code Online (Sandbox Code Playgroud)
这避免了奇怪的索引,例如time[:-1]