Skip to content Skip to sidebar Skip to footer

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 sol

Solution 1:

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()

Solution 2:

It sounds like you want to flatten e.

You can either create a list with the already flat variables and extend it with e:

return tuple([im1, im2] + e)

Or unpacke everywhere you want to use it.

Solution 3:

Assuming that you are using matplotlib.animation, animate should be returning an iterable and you are returning one that contains three objects. return imm1, imm2, e is returning a tuple of three instances. The final one is a list. You should be able to return a list instead of a tuple by changing the animate function to:

def animate(j):
    im1, im2, objects =object_finder_function()
    imm1, imm2, e = plot_images(im1, im2, objects, axs)

    return [imm1, imm2] + e

However, I would change plot_images to return a list instead. Maybe something like the following:

def create_ellipse(objects, object_idx, artists):
    ellipse = Ellipse(
        xy=(objects['x'][object_idx], objects['y'][object_idx]),
        width=(6 * objects['a'][object_idx]),
        height=(6 * objects['b'][object_idx]),
        angle=(objects['theta'][object_idx] * 180.0 / np.pi))
    ellipse.set_facecolor('none')
    ellipse.set_edgecolor('red')
    artists.add_artists(ellipse)
    return ellipse

def plot_images(img1, img2, objects, ax):
    renderables = [ax.imshow(img1),
                   ax.imshow(img2 with transparency as an overlay)]
    renderables.extend(create_ellipse(objects, idx, ax)
                       for idx in range(len(objects)))
    return renderables

Post a Comment for "How To Return An Unknown Number Of Objects In Python Animation Function"