import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots(ncols=2)
N = 100
t_end = 10
t = np.linspace(0, t_end, N)
tau = 3
def ft(t):
return 5*np.exp(-t)
def ft2(t):
return 5*np.exp(-t/tau)
v = ft(t)
v2 = ft2(t)
fig.set_figwidth(6)
fig.set_figheight(4)
scat1 = ax[0].scatter(t[0], v[0], c="b", s=5)
ax[0].set(xlim=[0, t_end], ylim=[-1, 6])
ax[0].set_xlabel('Time [seconds]',fontsize=10)
ax[0].set_ylabel('V [volts]', fontsize=10)
scat2 = ax[1].scatter(t[0], v[0], c="b", s=5)
ax[1].set(xlim=[0, t_end], ylim=[-1, 6])
ax[1].set_xlabel('Time [seconds]',fontsize=10)
ax[0].grid(True)
ax[1].grid(True)
ax[0].set_title(r"$\dot{x} + x = 0$",fontsize=14)
ax[1].set_title(r"$3\dot{x} + x = 0$",fontsize=14)
def update(frame):
# for each frame, update the data stored on each artist.
x = t[:frame]
y = v[:frame]
# also for the other plot
y2 = v2[:frame]
# update the scatter plot:
data = np.stack([x, y]).T
scat1.set_offsets(data)
scat2.set_offsets(np.stack([x,y2]).T)
# ax[0].set_title(f'time = {t[frame]:.2f} s, v = {v[frame]:.2e}',fontsize=9)
# ax[1].set_title(f'time = {t[frame]:.2f} s, v = {v2[frame]:.2e}',fontsize=9)
return (scat1,scat2)
ani = animation.FuncAnimation(fig=fig, func=update, frames=N, interval=30)
writer1 = animation.PillowWriter(fps=10) # Set frames per second (fps)
# plt.show()
ani.save("timeconst-comparison.gif",writer = writer1)