Python 不同帧率子地块的艺术化
考虑以下代码,该代码实现了Python 不同帧率子地块的艺术化,python,matplotlib,Python,Matplotlib,考虑以下代码,该代码实现了ArtistAnimation,以在同一地物对象中设置两个不同子地块的动画 import numpy as np import itertools import matplotlib.pyplot as plt import matplotlib.mlab as ml import matplotlib.animation as animation def f(x,y,a): return ((x/a)**2+y**2) avals = np.linspac
ArtistAnimation
,以在同一地物对象中设置两个不同子地块的动画
import numpy as np
import itertools
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
import matplotlib.animation as animation
def f(x,y,a):
return ((x/a)**2+y**2)
avals = np.linspace(0.1,1,10)
xaxis = np.linspace(-2,2,9)
yaxis = np.linspace(-2,2,9)
xy = itertools.product(xaxis,yaxis)
xy = list(map(list,xy))
xy = np.array(xy)
x = xy[:,0]
y = xy[:,1]
fig, [ax1,ax2] = plt.subplots(2)
ims = []
for a in avals:
xi = np.linspace(min(x), max(x), len(x))
yi = np.linspace(min(y), max(y), len(y))
zi = ml.griddata(x, y, f(x, y, a), xi, yi, interp='linear') # turn it into grid data, this is what imshow takes
title = plt.text(35,-4,str(a), horizontalalignment = 'center')
im1 = ax1.imshow(zi, animated = True, vmin = 0, vmax = 400)
im2 = ax2.imshow(zi, animated=True, vmin=0, vmax=400)
ims.append([im1,im2, title])
ani = animation.ArtistAnimation(fig, ims, interval = 1000, blit = False)
plt.show()
在这种情况下,im1
和im2
中的项目数相同,并且每个子批次的帧速率相同
现在,假设我有两个列表,其中包含不同数量的项目,我希望ArtistAnimate
在相同的总时间内浏览这些帧。最初我想在ArtistAnimation
调用中操作interval
关键字,但这意味着可以为不同的艺术家设置不同的时间间隔,我认为这是不可能的
无论如何,我认为基本思想非常清楚len(im1)
不等于len(im2)
,但是动画需要在相同的时间内完成所有这些
请问有什么办法吗?谢谢
编辑
当我尝试下面提供的答案时,我应该补充说,由于我的数据结构,我宁愿使用
ArtistAnimation
。如果没有替代方案,我将回到下面的解决方案。是的,这是可能的,使用Funcanimation
并将数据封装在func
中
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
arr1 = np.random.rand(300,3,4)
arr2 = np.random.rand(200,5,6)
fig, (ax1, ax2) = plt.subplots(1,2)
img1 = ax1.imshow(arr1[0])
img2 = ax2.imshow(arr2[0])
# set relative display rates
r1 = 2
r2 = 3
def animate(ii):
if ii % r1:
img1.set_data(arr1[ii/r1])
if ii % r2:
img2.set_data(arr2[ii/r2])
return img1, img2
ani = animation.FuncAnimation(fig, func=animate, frames=np.arange(0, 600))
plt.show()
使用ArtistAnimation有什么方法可以做到这一点吗?事实上,我的数据更复杂,我更愿意遍历字典并填充列表以获得它……
arr1
和arr2
同样可以是帧列表。所有传递给动画的内容都是索引。好的。我可以试试。另外,仅供参考,您的代码在我的python版本中不起作用。我在animate img1.set_data(arr1[ii/r2])中的第18行得到一个错误文件“D:/11200999-POVProject/Validation/Dike1/DisplayRate.py”。索引器:仅整数、切片(
:)、省略号(
…)、numpy.newaxis(
无)整数或布尔数组是有效的索引
,我不太清楚数据结构会在多大程度上使ArtistAnimation在FuncAnimation上可引用。理论上,您当然可以定义一个comon帧速率并多次向列表中提供同一个艺术家,也就是说,您有ims=[[imA_1,imB_1],[imA_1,imB_2],[imA_2,imA_3],…]
(帧速率加倍),但使用FuncAnimation实际上要简单得多。