I am trying to do an animation using the FuncAnimation
module but my code only produces one frame. It looks like that it update the right thing (k
) and it go on with the animation for the right amount of frames, but every frame shows the first image with k=0.
def plotheatmap(u_k, k):
# Clear the current plot figure
plt.clf()
plt.title(f"Temperature at t = {k*dt:.3f} unit time")
plt.xlabel("x")
plt.ylabel("y")
# This is to plot u_k (u at time-step k)
plt.pcolormesh(u_k, cmap=plt.cm.jet, vmin=0, vmax=4)
plt.colorbar()
def animate(k):
plotheatmap(convert(U)[k], k)
anim = animation.FuncAnimation(plt.figure(), animate, interval=200, frames=M 1)
CodePudding user response:
Since you didn't provide a reproducible example, I'm going to generate random data, and you will adapt it to your case.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x = np.arange(-0.5, 10, 1)
y = np.arange(4.5, 11, 1)
fig, ax = plt.subplots()
ax.set_xlabel("x")
ax.set_ylabel("y")
def plotheatmap(k):
z = np.random.rand(6, 10)
# clear the previously added heatmaps
ax.collections.clear()
# add a new heatmap
ax.pcolormesh(x, y, z)
ax.set_title(f"Temperature at t = {k:.3f} unit time")
def animate(k):
plotheatmap(k)
M = 10
anim = FuncAnimation(fig, animate, interval=200, frames=M 1)