问题遇到的现象和发生背景
想要画一个矢量图的动图,看到有教程:将一组图片read进来,做成一个列表,然后直接用animation做成动图保存,我这里尝试用quiver直接绘制出来组成列表,但是在保存时就会报错。
问题相关代码,请勿粘贴截图
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def Vx(u0,U,W,H,cx,t):
len_x=U.shape[0]
len_y=U.shape[1]
dx=W/(len_x-1)
dy=H/(len_y-1)
for i in range(0,len_x):
for j in range(0,len_y):
U[i,j]=-u0math.sin(2math.pi*(idx-cxt)/W)math.cos(math.pij*dy/H)
return U
def Vy(v0,V,W,H,cx,t):
len_x=V.shape[0]
len_y=V.shape[1]
dx = W / (len_x - 1)
dy = H / (len_y - 1)
for i in range(0,len_x):
for j in range(0,len_y):
V[i,j]=-v0math.cos(2math.pi*(idx-cxt)/W)math.sin(math.pij*dy/H)
return V
def New_UV(nx,ny):
U=np.zeros((nx,ny))
V=np.zeros((nx,ny))
return U,V
u0=1
v0=3
dt=1
cx=10
W=1000
H=1500
nx=21
ny=31
N=10000
U,V=New_UV(nx,ny)
U=Vx(u0,U,W,H,cx,0)
V=Vy(v0,V,W,H,cx,0)
fig = plt.figure()
plt.grid(ls='--')
imag=[]
for index in range(200):
U = Vx(u0, U, W, H, cx, index)
V = Vy(v0, V, W, H, cx, index)
imag.append(plt.quiver(U,V))
ani=animation.ArtistAnimation(fig, imag, interval=200, repeat_delay=40000)
ani.save("test.gif",writer='ffmpeg')
plt.show()
运行结果及报错内容
我的解答思路和尝试过的方法
感觉这个报错可能是因为quiver绘制出来不是图片而是一个内存地址,我不想把这堆图片全部挨个保存下来再挨个读取最后删掉,有没有什么办法能直接把绘制出的图片做成GIF或者MP4格式的动图?
我尝试过用.set_data()这样的方式,但是报错提示我‘Quiver’ object has no attribute of .set_data。
也尝试过save的时候writer=‘pillow’
都不对。
我想要达到的结果
我想用quiver绘制出一组矢量图,然后把这组矢量图组成一个动图,期间不要把这组图片逐帧保存下来。