'Trying to animate 2 image lists and 1 graph on the same figure

First of all, this is an uncommon task and I desperately need help from python animation experts!

I have done segmentation on a video to get surface area of a region of interest over time. I want to animate the raw images, next to the segmented images and the graph of surface area over time. I have used a for loop and plt.pause to get a crude animation (click to see the crude sketch of what I am looking for).

The data for the two images are lists of frames, and the graph is a simple scatter plot. I have tried the ArtistAnimation to animate the two image sets together:

fig_original = plt.figure('Results', figsize=(10, 10))
graphs = fig_original.add_gridspec(2, 2)
original_fig = fig_original.add_subplot(graphs[:, 0])
original_fig.axis('off')
segmented_fig = fig_original.add_subplot(graphs[0, 1])
segmented_fig.axis('off')
area_fig = fig_original.add_subplot(graphs[1, 1])
ims = []
for j, imageSet in enumerate([[originalImages, original_fig], [segmentedImages, segmented_fig]]):
    ax = imageSet[1]
    ims.append([])
    for i, frame in enumerate(imageSet[0]):
        im = ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), animated=True)
        if i ==0:
            ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        ims[j].append([im])

animation = ani.ArtistAnimation(fig_original, ims, blit=True, interval=10)

However I get the following error, and the plots only display the very last frame with no animations rendered:

Traceback (most recent call last):   File "/Applications/Spyder

.app/Contents/Resources/lib/python3.9/matplotlib/cbook/__init__.py", line 287, in process
    func(*args, **kwargs)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/matplotlib/animation.py", line 909, in _start
    self._init_draw()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/matplotlib/animation.py", line 1465, in _init_draw
    artist.set_visible(False)
AttributeError: 'list' object has no attribute 'set_visible'

I have not yet tried to include the surface area-time graph as an animation. So I also need help with that!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source