8000 Merge pull request #671 from dopplershift/anim-io · chrodan/matplotlib@8b1e5b8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b1e5b8

Browse files
committed
Merge pull request matplotlib#671 from dopplershift/anim-io
Refactor saving of movies to support multiple backends (ffmpeg and mencoder), optionally using backend-specific parameters and attempts to allow generic control of codec and bitrate. Also supports using pipes for writing frames rather than temporary files, which is cleaner and faster. Code is readily extensible to additional movie utilities.
2 parents af2efa4 + 40b91ce commit 8b1e5b8

File tree

8 files changed

+600
-64
lines changed

8 files changed

+600
-64
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2012-03-07 Refactor movie writing into useful classes that make use
2+
of pipes to write image data to ffmpeg or mencoder. Also
3+
improve settings for these and the ability to pass custom
4+
options. - RMM
5+
16
2012-02-29 errorevery keyword added to errorbar to enable errorbar
27
subsampling. fixes issue #600.
38

examples/animation/basic_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ def update_line(num, data, line):
2929

3030
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
3131
blit=True)
32-
#im_ani.save('im.mp4')
32+
#im_ani.save('im.mp4', metadata={'artist':'Guido'})
3333

3434
plt.show()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Same as basic_example, but writes files using a single MovieWriter instance
2+
# without putting on screen
3+
# -*- noplot -*-
4+
import numpy as np
5+
import matplotlib
6+
matplotlib.use("Agg")
7+
import matplotlib.pyplot as plt
8+
import matplotlib.animation as animation
9+
10+
def update_line(num, data, line):
11+
line.set_data(data[...,:num])
12+
return line,
13+
14+
# Set up formatting for the movie files
15+
Writer = animation.writers['ffmpeg']
16+
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
17+
18+
19+
fig1 = plt.figure()
20+
21+
data = np.random.rand(2, 25)
22+
l, = plt.plot([], [], 'r-')
23+
plt.xlim(0, 1)
24+
plt.ylim(0, 1)
25+
plt.xlabel('x')
26+
plt.title('test')
27+
line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
28+
interval=50, blit=True)
29+
line_ani.save('lines.mp4', writer=writer)
30+
31+
fig2 = plt.figure()
32+
33+
x = np.arange(-9, 10)
34+
y = np.arange(-9, 10).reshape(-1, 1)
35+
base = np.hypot(x, y)
36+
ims = []
37+
for add in np.arange(15):
38+
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 30)),))
39+
40+
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
41+
blit=True)
42+
im_ani.save('im.mp4', writer=writer)

examples/animation/dynamic_image2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def f(x, y):
2626
ani = animation.ArtistAnimation(fig, ims, interval=50, blit=True,
2727
repeat_delay=1000)
2828

29-
ani.save('dynamic_images.mp4')
29+
#ani.save('dynamic_images.mp4')
3030

3131

3232
plt.show()

examples/animation/moviewriter.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This example uses a MovieWriter directly to grab individual frames and
2+
# write them to a file. This avoids any event loop integration, but has
3+
# the advantage of working with even the Agg backend. This is not recommended
4+
# for use in an interactive setting.
5+
# -*- noplot -*-
6+
7+
import numpy as np
8+
import matplotlib
9+
matplotlib.use("Agg")
10+
import matplotlib.pyplot as plt
11+
import matplotlib.animation as manimation
12+
13+
FFMpegWriter = manimation.writers['ffmpeg']
14+
metadata = dict(title='Movie Test', artist='Matplotlib',
15+
comment='Movie support!')
16+
writer = FFMpegWriter(fps=15, metadata=metadata)
17+
18+
fig = plt.figure()
19+
l, = plt.plot([], [], 'k-o')
20+
21+
plt.xlim(-5, 5)
22+
plt.ylim(-5, 5)
23+
24+
x0,y0 = 0, 0
25+
26+
with writer.saving(fig, "writer_test.mp4", 100):
27+
for i in range(100):
28+
x0 += 0.1 * np.random.randn()
29+
y0 += 0.1 * np.random.randn()
30+
l.set_data(x0, y0)
31+
writer.grab_frame()
32+

0 commit comments

Comments
 (0)
0