Issue
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()
Solution
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)
Answered By - Rotem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.