matplotlib funcanimation仅显示一个框架

发布于 2025-01-30 07:10:15 字数 584 浏览 3 评论 0原文

我正在尝试使用funcanimation模块进行动画,但我的代码只会产生一个帧。看起来它更新了正确的东西(k),并继续使用适量的框架的动画,但是每个帧显示了第一个图像,其中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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2025-02-06 07:10:15

由于您没有提供可再现的示例,因此我将生成随机数据,并且您将其适应您的情况。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文