【发布时间】:2021-10-18 17:26:01
【问题描述】:
我正在尝试使用 Jupyter Lab 中的 matplotlib 为高斯调制波脉冲设置动画,但我无法让它工作。我希望脉冲及时向前移动,即从中间向右移动(换句话说,显示脉冲传播)。
我使用 scipy 的“signal.gausspulse”函数创建脉冲本身,该函数创建脉冲的静态图像。然后我创建一个网格并尝试将脉冲“映射”到它上面,同时通过将帧号“i”作为输入的动画函数循环它并循环遍历我们想要动画的值。
在我让动画中的脉搏移动之前,它只是静止的,没有任何移动。我认为这是因为具有波脉冲 y 值的整个数组没有随时间变化,所以我尝试创建一个循环来更新它。这有帮助,但它非常缓慢,会使脉搏向上移动两次,然后完全停止运动。
我无法弄清楚这一点,并且真的不知道该怎么做,因此将不胜感激任何帮助! :) 我可能误用了一些术语,所以我提前为此道歉 - 我尝试评论代码并解释我所经历的步骤,希望能有所帮助。
%matplotlib widget
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
#setting up the canvas
fig, ax = plt.subplots(figsize=(5, 3))
ax.set(xlim=(-3, 3), ylim=(-1, 1))
#creating meshgrid for future animation
x = np.linspace(-10, 5, 200)
t = np.linspace(-10, 5, 200)
X2, T2 = np.meshgrid(x, t) #X2 and T2 are numpy arrays now
#creating the gaussian modulated pulse: fc is frequency and bw is another parameter than can be ignored
F = signal.gausspulse(X2, fc=5, bw = 0.3, retquad=False, retenv=True)[0]
#updating the values in F array to make the wave pulse move in time
j = 0
i = 0
for i in range(len(t)):
for j in range(len(t)):
F[i,j] += i - 5e-40 #some arbitrary value added/subtracted, chose e-40 bec of values in array F
#F[i,j] += j + 5e-40
#creting a Line.2D to be plotted later; F vs time
line = ax.plot(t, F[0, :], color='k', lw=2)[0]
#animating function
def animate(i):
return line.set_ydata(F[i,:])
anim = FuncAnimation(fig, animate, interval=1000, frames=1000, repeat = False)
plt.draw()
plt.grid(True)
plt.show()
【问题讨论】:
标签: python matplotlib animation plot scipy