Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8673b07

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents e4d7a9e + adf6680 commit 8673b07

11 files changed

Lines changed: 934 additions & 141 deletions

File tree

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+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import numpy as np
2+
3+
from matplotlib.widgets import LassoSelector
4+
from matplotlib.path import Path
5+
6+
7+
class SelectFromCollection(object):
8+
"""Select indices from a matplotlib collection using `LassoSelector`.
9+
10+
Selected indices are saved in the `ind` attribute. This tool highlights
11+
selected points by fading them out (i.e., reducing their alpha values).
12+
If your collection has alpha < 1, this tool will permanently alter them.
13+
14+
Note that this tool selects collection objects based on their *origins*
15+
(i.e., `offsets`).
16+
17+
Parameters
18+
----------
19+
ax : :class:`~matplotlib.axes.Axes`
20+
Axes to interact with.
21+
22+
collection : :class:`matplotlib.collections.Collection` subclass
23+
Collection you want to select from.
24+
25+
alpha_other : 0 <= float <= 1
26+
To highlight a selection, this tool sets all selected points to an
27+
alpha value of 1 and non-selected points to `alpha_other`.
28+
"""
29+
def __init__(self, ax, collection, alpha_other=0.3):
30+
self.canvas = ax.figure.canvas
31+
self.collection = collection
32+
self.alpha_other = alpha_other
33+
34+
self.xys = collection.get_offsets()
35+
self.Npts = len(self.xys)
36+
37+
# Ensure that we have separate colors for each object
38+
self.fc = collection.get_facecolors()
39+
if len(self.fc) == 0:
40+
raise ValueError('Collection must have a facecolor')
41+
elif len(self.fc) == 1:
42+
self.fc = np.tile(self.fc, self.Npts).reshape(self.Npts, -1)
43+
44+
self.lasso = LassoSelector(ax, onselect=self.onselect)
45+
self.ind = []
46+
47+
def onselect(self, verts):
48+
path = Path(verts)
49+
self.ind = np.nonzero([path.contains_point(xy) for xy in self.xys])[0]
50+
self.fc[:, -1] = self.alpha_other
51+
self.fc[self.ind, -1] = 1
52+
self.collection.set_facecolors(self.fc)
53+
self.canvas.draw_idle()
54+
55+
def disconnect(self):
56+
self.lasso.disconnect_events()
57+
self.fc[:, -1] = 1
58+
self.collection.set_facecolors(self.fc)
59+
self.canvas.draw_idle()
60+
61+
62+
if __name__ == '__main__':
63+
import matplotlib.pyplot as plt
64+
65+
plt.ion()
66+
data = np.random.rand(100, 2)
67+
68+
subplot_kw = dict(xlim=(0,1), ylim=(0,1), autoscale_on=False)
69+
fig, ax = plt.subplots(subplot_kw=subplot_kw)
70+
71+
pts = ax.scatter(data[:, 0], data[:, 1], s=80)
72+
selector = SelectFromCollection(ax, pts)
73+
74+
plt.draw()
75+
raw_input('Press any key to accept selected points')
76+
print "Selected points:"
77+
print selector.xys[selector.ind]
78+
selector.disconnect()
79+
80+
# Block end of script so you can check that the lasso is disconnected.
81+
raw_input('Press any key to quit')
82+

0 commit comments

Comments
 (0)