import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
N = 100
t_end = 10
t = np.linspace(0, t_end, N)
def ft(t):
return 5*np.exp(-t)
v = ft(t)
scat = ax.scatter(t[0], v[0], c="b", s=5, label='v(t)')
ax.set(xlim=[0, t_end], ylim=[-1, 6])
ax.set_xlabel('Time [seconds]',fontsize=16)
ax.set_ylabel('V [volts]', fontsize=16)
plt.grid()
ax.legend()
def update(frame):
# for each frame, update the data stored on each artist.
x = t[:frame]
y = v[:frame]
# update the scatter plot:
data = np.stack([x, y]).T
scat.set_offsets(data)
ax.set_title(f'time = {t[frame]:.2f} s, v = {v[frame]:.2e}',fontsize=16)
return scat
ani = animation.FuncAnimation(fig=fig, func=update, frames=N, interval=30)
writer1 = animation.PillowWriter(fps=15) # Set frames per second (fps)
ani.save("first-order.gif",writer = writer1)