我正在嘗試使用該FuncAnimation模塊制作影片,但我的代碼只產生一幀。看起來它更新了正確的東西k(
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)
uj5u.com熱心網友回復:
由于您沒有提供可重現的示例,因此我將生成隨機資料,您將根據您的情況對其進行調整。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476504.html
標籤:Python matplotlib 动画片
下一篇:網頁抓取后如何分割線?
