Pat X
Pat X

Reputation: 43

Matplotlib - Why is my saved animation video blank?

This should be pretty simple but I just don't know.

A newbie to Python and FFmpeg. Just trying to save a test video from ArtistAnimation but got blank video.

Before I tried to produce the video, I can see the animation by plt.show() (without "matplotlib.use("Agg")" ). I have already installed FFmpeg in Anaconda as well.

To ensure my FFmpeg is functioning, I used the code from matplotlib example and produced a video that looks perfect. (I guess this means my FFmpeg will work fine from now on?)

Then, I only changed the figure to my version. Having compared the figure part, I didn't see anything wrong obviously. But in the saved video of my version, it's blank.

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import numpy as np
import pandas as pd


fig = plt.figure()
ims = []
for i in range(10):
    ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2, rowspan=2)
    data = np.random.normal(0, 1, i+1)
    pd.DataFrame(data).plot(kind='bar', ax=ax1)
    ims.append([ax1])



# Set up formatting for the movie files
Writer = ani.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

anim = ani.ArtistAnimation(fig, ims, interval=500, repeat_delay=3000, blit=True)
anim.save('textmovie.mp4', writer=writer)
plt.show()

Upvotes: 1

Views: 1393

Answers (2)

guttikar
guttikar

Reputation: 71

For me, removing the blit=True did the trick. I think it has something to do with the fact that if blit=True then you need to return all the artists that have gotten modified but I am not sure. In my case I was changing the viewing angle (ax.azim) which probably changed the rendering of everything I had plotted. I was returning ax, from the animate function. Hopefully someone more knowledgeable than me would comment on this.

Upvotes: 0

Rotem
Rotem

Reputation: 32084

I found a solution using celluloid.

There could be a way to create the animation using ims.append, but I couldn't find one.

For the solution to work, you need to place ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2, rowspan=2) before the loop.

Here is the code:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import numpy as np
import pandas as pd
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig) #  https://pypi.org/project/celluloid/
ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2, rowspan=2)
for i in range(10):
    data = np.random.normal(0, 1, i+1)
    pd.DataFrame(data).plot(kind='bar', ax=ax1)
    camera.snap()

# Set up formatting for the movie files
Writer = ani.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

# anim = ani.ArtistAnimation(fig, ims, interval=500, repeat_delay=3000, blit=True)
anim = camera.animate(interval=500, repeat_delay=3000, blit=True)
anim.save('textmovie.mp4', writer=writer)

Result:
camera animate

Upvotes: 1

Related Questions