1"""
2==============
3Frame grabbing
4==============
5
6Use a MovieWriter directly to grab individual frames and write them to a
7file.  This avoids any event loop integration, and thus works even with the Agg
8backend.  This is not recommended for use in an interactive setting.
9"""
10
11import numpy as np
12import matplotlib
13matplotlib.use("Agg")
14import matplotlib.pyplot as plt
15from matplotlib.animation import FFMpegWriter
16
17# Fixing random state for reproducibility
18np.random.seed(19680801)
19
20
21metadata = dict(title='Movie Test', artist='Matplotlib',
22                comment='Movie support!')
23writer = FFMpegWriter(fps=15, metadata=metadata)
24
25fig = plt.figure()
26l, = plt.plot([], [], 'k-o')
27
28plt.xlim(-5, 5)
29plt.ylim(-5, 5)
30
31x0, y0 = 0, 0
32
33with writer.saving(fig, "writer_test.mp4", 100):
34    for i in range(100):
35        x0 += 0.1 * np.random.randn()
36        y0 += 0.1 * np.random.randn()
37        l.set_data(x0, y0)
38        writer.grab_frame()
39