使用 matplotlib 动画波脉冲
Animating wave pulse using matplotlib
我正在尝试在 Jupyter Lab 中使用 matplotlib 为高斯调制波脉冲制作动画,但我无法让它工作。我希望脉冲及时向前移动,即从中间向右移动(换句话说,显示脉冲传播)。
我通过使用 scipy 的“signal.gausspulse”函数创建脉冲本身,该函数创建脉冲的静态图像。然后我创建一个 meshgrid 并尝试将脉冲“映射”到它上面,同时通过动画函数循环它,该函数将帧号“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()
您可以定义一个固定的 x_fixed
轴来绘制数据。然后计算一个新轴 x
,通过在每次迭代中减去 i/10
,它在每次迭代中向右平移。这个值是任意的,它决定了向右移动的速度。然后你计算平移轴上的新信号,但是你绘制相对于固定轴的信号。
使用 ax.cla()
清理之前的绘图并在每次迭代时设置网格和轴限制非常重要。
不需要 meshgrid 或 for 循环。
完整代码
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x_fixed = np.linspace(-3, 3, 200)
def animate(i):
ax.cla()
x = np.linspace(-3, 3, 200) - i/10
F = signal.gausspulse(x, fc = 5, bw = 0.3, retquad = False, retenv = True)[0]
ax.plot(x_fixed, F)
ax.set(xlim = (-3, 3), ylim = (-1, 1))
ax.grid()
fig, ax = plt.subplots(figsize=(5, 3))
anim = FuncAnimation(fig, animate, interval=1000, frames=1000, repeat = False)
plt.show()
我正在尝试在 Jupyter Lab 中使用 matplotlib 为高斯调制波脉冲制作动画,但我无法让它工作。我希望脉冲及时向前移动,即从中间向右移动(换句话说,显示脉冲传播)。
我通过使用 scipy 的“signal.gausspulse”函数创建脉冲本身,该函数创建脉冲的静态图像。然后我创建一个 meshgrid 并尝试将脉冲“映射”到它上面,同时通过动画函数循环它,该函数将帧号“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()
您可以定义一个固定的 x_fixed
轴来绘制数据。然后计算一个新轴 x
,通过在每次迭代中减去 i/10
,它在每次迭代中向右平移。这个值是任意的,它决定了向右移动的速度。然后你计算平移轴上的新信号,但是你绘制相对于固定轴的信号。
使用 ax.cla()
清理之前的绘图并在每次迭代时设置网格和轴限制非常重要。
不需要 meshgrid 或 for 循环。
完整代码
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x_fixed = np.linspace(-3, 3, 200)
def animate(i):
ax.cla()
x = np.linspace(-3, 3, 200) - i/10
F = signal.gausspulse(x, fc = 5, bw = 0.3, retquad = False, retenv = True)[0]
ax.plot(x_fixed, F)
ax.set(xlim = (-3, 3), ylim = (-1, 1))
ax.grid()
fig, ax = plt.subplots(figsize=(5, 3))
anim = FuncAnimation(fig, animate, interval=1000, frames=1000, repeat = False)
plt.show()