如何在Python动画函数中返回未知数量的对象 [英] How to return an unknown number of objects in Python animation function
问题描述
我目前正在尝试制作一系列图像的动画,其中为每个图像绘制一个最初未知的椭圆形。到目前为止,我已经尝试了很多方法,但是还没有找到解决方案,尽管我想我已经接近了。这是我的代码:
I am currently trying to animate a series of images where for each image an initially unknown number of ellipses are drawn. I have tried many things so far, but haven't found a solution yet, though I guess I came close. Here is my code:
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
def plot_images(img1, img2, objects, ax):
im1 = ax.imshow(img1)
im2 = ax.imshow(img2 with transparency as an overlay)
# plotting an ellipse for each object
e = [None]*len(objects)
for j in range(len(objects)):
e[j] = Ellipse(xy=(objects['x'][j], objects['y'][j]),
width=6 * objects['a'][j],
height=6 * objects['b'][j],
angle=objects['theta'][j] * 180. / np.pi)
e[j].set_facecolor('none')
e[j].set_edgecolor('red')
ax.add_artist(e[j])
return im1, im2, e
def animate(j):
# extracting objects
im1, im2, objects = object_finder_function()
imm1, imm2, e = plot_images(im1, im2, objects, axs)
return imm1, imm2, e
fig, axs = plt.subplots()
ani = animation.FuncAnimation(fig, animate, frames=image_number, interval=50, blit=True)
plt.show()
现在当我尝试时这段代码,我得到以下错误消息:
Now when I try this code, I get the following error message:
AttributeError: 'list' object has no attribute 'get_zorder'
所以我尝试了不同的方法,但最终,我发现,当作为测试时,我将plot_images函数放入
So I tried different things, but ultimately, I found that when, as a test, I put in the plot_images function
return im1, im2, e[0], e[1], e[2]
,并相应地更改动画功能,即
and also change the animate function accordingly, i.e.
imm1, imm2, e0, e1, e2 = plot_images(im1, im2, objects, axs)
和
return imm1, imm2, e0, e1, e2
我没有收到错误消息,实际上按照我的意图在各个帧中绘制了椭圆。现在的问题是,对于一个图像,我想绘制每幅图像有数百个椭圆,所以我必须手动将其全部写下(即e [0],e [1],e [2]- -e [k],和动画功能相同),这似乎不是正确的方法。另一件事是,正如我已经说过的,每张图像的椭圆数会发生变化,并且以前是未知的,因此我无法相应地调整功能。
I don't get an error message and the ellipses are actually plotted in the respective frames as I intended. Now the problem is, that for one, there are many hundred ellipses per image that I would like to plot, so I would have to manually write that all down (i.e. e[0], e[1], e[2] -- e[k], and the same for the animate function) and this doesn't seem to be the right way. The other thing is that as I already said the number of ellipses changes for each image and is not previously known so I cannot possibly adjust the functions accordingly.
如何返回此椭圆列表,以便动画读取它,就像我会像在工作示例中一样将它们全部写下来一样?
How can I return this list of ellipses so that the animation reads it as if I would have written them all down separately as it is done in the working example?
推荐答案
您的代码有点不合逻辑,因此为了清晰起见,我对其进行了一些清理。您的 AttributeError
与 get_zorder
函数有关,该函数在matplotlib中用于确定如何对绘图进行分层。使用您尝试过的方法,我可以告诉您只需要最后打开list_of_ellipses即可。
Your code is a bit unpythonic, so I cleaned up it just a bit for clarity. Your AttributeError
has to do with the get_zorder
function, which is used in matplotlib for figuring out how to layer plots. With the things you tried I can tell you just need to unpack your list_of_ellipses at the end.
def plot_images(img1, img2, objects, ax):
im1 = ax.imshow(img1)
im2 = ax.imshow(img2 with transparency as an overlay)
list_of_ellipses = []
for j in range(len(objects)):
my_ellipse = Ellipse(xy=(objects['x'][j], objects['y'][j]),
width=6 * objects['a'][j],
height=6 * objects['b'][j],
angle=objects['theta'][j] * 180. / np.pi)
my_ellipse.set_facecolor('none')
my_ellipse.set_edgecolor('red')
ax.add_artist(my_ellipse)
list_of_ellipses.append(my_ellipse)
return im1, im2, list_of_ellipses
def animate():
im1, im2, objects = object_finder_function()
imm1, imm2, list_of_ellipses = plot_images(im1, im2, objects, axs)
return (imm1, imm2)+tuple(list_of_ellipses)
fig, axs = plt.subplots()
ani = animation.FuncAnimation(fig, animate, frames=image_number, interval=50, blit=True)
plt.show()
这篇关于如何在Python动画函数中返回未知数量的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!