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'
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)
and
return imm1, imm2, e0, e1, e2
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?