|
From: <ry...@us...> - 2010-08-26 03:23:32
|
Revision: 8660
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8660&view=rev
Author: ryanmay
Date: 2010-08-26 03:23:25 +0000 (Thu, 26 Aug 2010)
Log Message:
-----------
Add previous animation examples ported to new framework, as well as a few new ones.
Added Paths:
-----------
trunk/matplotlib/examples/animation/
trunk/matplotlib/examples/animation/animate_decay.py
trunk/matplotlib/examples/animation/dynamic_image.py
trunk/matplotlib/examples/animation/dynamic_image2.py
trunk/matplotlib/examples/animation/histogram.py
trunk/matplotlib/examples/animation/random_data.py
trunk/matplotlib/examples/animation/simple_3danim.py
trunk/matplotlib/examples/animation/simple_anim.py
trunk/matplotlib/examples/animation/strip_chart_demo.py
trunk/matplotlib/examples/animation/subplots.py
Added: trunk/matplotlib/examples/animation/animate_decay.py
===================================================================
--- trunk/matplotlib/examples/animation/animate_decay.py (rev 0)
+++ trunk/matplotlib/examples/animation/animate_decay.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,36 @@
+import numpy as np
+import matplotlib.pyplot as plt
+from animation import FuncAnimation
+
+def data_gen():
+ t = data_gen.t
+ cnt = 0
+ while cnt < 1000:
+ cnt+=1
+ t += 0.05
+ yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
+data_gen.t = 0
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+line, = ax.plot([], [], lw=2)
+ax.set_ylim(-1.1, 1.1)
+ax.set_xlim(0, 5)
+ax.grid()
+xdata, ydata = [], []
+def run(data):
+ # update the data
+ t,y = data
+ xdata.append(t)
+ ydata.append(y)
+ xmin, xmax = ax.get_xlim()
+
+ if t >= xmax:
+ ax.set_xlim(xmin, 2*xmax)
+ ax.figure.canvas.draw()
+ line.set_data(xdata, ydata)
+
+ return line,
+
+ani = FuncAnimation(fig, run, data_gen, blit=True, interval=10, repeat=False)
+plt.show()
Added: trunk/matplotlib/examples/animation/dynamic_image.py
===================================================================
--- trunk/matplotlib/examples/animation/dynamic_image.py (rev 0)
+++ trunk/matplotlib/examples/animation/dynamic_image.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+"""
+An animated image
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+from animation import FuncAnimation
+
+fig = plt.figure()
+
+def f(x, y):
+ return np.sin(x) + np.cos(y)
+
+x = np.linspace(0, 2 * np.pi, 120)
+y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
+
+im = plt.imshow(f(x, y), cmap=plt.get_cmap('jet'))
+
+def updatefig(*args):
+ global x,y
+ x += np.pi / 15.
+ y += np.pi / 20.
+ im.set_array(f(x,y))
+ return im,
+
+ani = FuncAnimation(fig, updatefig, interval=50, blit=True)
+plt.show()
Added: trunk/matplotlib/examples/animation/dynamic_image2.py
===================================================================
--- trunk/matplotlib/examples/animation/dynamic_image2.py (rev 0)
+++ trunk/matplotlib/examples/animation/dynamic_image2.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+"""
+An animated image
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+from animation import ArtistAnimation
+
+fig = plt.figure()
+
+def f(x, y):
+ return np.sin(x) + np.cos(y)
+
+x = np.linspace(0, 2 * np.pi, 120)
+y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
+
+ims = []
+for i in range(60):
+ x += np.pi / 15.
+ y += np.pi / 20.
+ ims.append([plt.imshow(f(x, y), cmap=plt.get_cmap('jet'))])
+
+ani = ArtistAnimation(fig, ims, interval=50, blit=True, repeat_delay=1000)
+plt.show()
Added: trunk/matplotlib/examples/animation/histogram.py
===================================================================
--- trunk/matplotlib/examples/animation/histogram.py (rev 0)
+++ trunk/matplotlib/examples/animation/histogram.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,62 @@
+"""
+This example shows how to use a path patch to draw a bunch of
+rectangles for an animated histogram
+"""
+import numpy as np
+
+import matplotlib.pyplot as plt
+import matplotlib.patches as patches
+import matplotlib.path as path
+from animation import FuncAnimation
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+
+# histogram our data with numpy
+data = np.random.randn(1000)
+n, bins = np.histogram(data, 100)
+
+# get the corners of the rectangles for the histogram
+left = np.array(bins[:-1])
+right = np.array(bins[1:])
+bottom = np.zeros(len(left))
+top = bottom + n
+nrects = len(left)
+
+# here comes the tricky part -- we have to set up the vertex and path
+# codes arrays using moveto, lineto and closepoly
+
+# for each rect: 1 for the MOVETO, 3 for the LINETO, 1 for the
+# CLOSEPOLY; the vert for the closepoly is ignored but we still need
+# it to keep the codes aligned with the vertices
+nverts = nrects*(1+3+1)
+verts = np.zeros((nverts, 2))
+codes = np.ones(nverts, int) * path.Path.LINETO
+codes[0::5] = path.Path.MOVETO
+codes[4::5] = path.Path.CLOSEPOLY
+verts[0::5,0] = left
+verts[0::5,1] = bottom
+verts[1::5,0] = left
+verts[1::5,1] = top
+verts[2::5,0] = right
+verts[2::5,1] = top
+verts[3::5,0] = right
+verts[3::5,1] = bottom
+
+barpath = path.Path(verts, codes)
+patch = patches.PathPatch(barpath, facecolor='green', edgecolor='yellow', alpha=0.5)
+ax.add_patch(patch)
+
+ax.set_xlim(left[0], right[-1])
+ax.set_ylim(bottom.min(), top.max())
+
+def animate(i):
+ # simulate new data coming in
+ data = np.random.randn(1000)
+ n, bins = np.histogram(data, 100)
+ top = bottom + n
+ verts[1::5,1] = top
+ verts[2::5,1] = top
+
+ani = FuncAnimation(fig, animate, 100, repeat=False)
+plt.show()
Added: trunk/matplotlib/examples/animation/random_data.py
===================================================================
--- trunk/matplotlib/examples/animation/random_data.py (rev 0)
+++ trunk/matplotlib/examples/animation/random_data.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,18 @@
+import numpy as np
+import matplotlib.pyplot as plt
+from animation import FuncAnimation
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+line, = ax.plot(np.random.rand(10))
+ax.set_ylim(0, 1)
+
+def update(data):
+ line.set_ydata(data)
+ return line,
+
+def data_gen():
+ while True: yield np.random.rand(10)
+
+ani = FuncAnimation(fig, update, data_gen, interval=100)
+plt.show()
Added: trunk/matplotlib/examples/animation/simple_3danim.py
===================================================================
--- trunk/matplotlib/examples/animation/simple_3danim.py (rev 0)
+++ trunk/matplotlib/examples/animation/simple_3danim.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,63 @@
+"""
+A simple example of an animated plot... In 3D!
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+import mpl_toolkits.mplot3d.axes3d as p3
+
+from animation import FuncAnimation
+
+def Gen_RandLine(length, dims=2) :
+ """
+ Create a line using a random walk algorithm
+
+ length is the number of points for the line.
+ dims is the number of dimensions the line has.
+ """
+ lineData = np.empty((dims, length))
+ lineData[:, 0] = np.random.rand(1, dims)
+ for index in xrange(1, length) :
+ # scaling the random numbers by 0.1 so
+ # movement is small compared to position.
+ # subtraction by 0.5 is to change the range to [-0.5, 0.5]
+ # to allow a line to move backwards.
+ step = ((np.random.rand(1, dims) - 0.5) * 0.1)
+ lineData[:, index] = lineData[:, index-1] + step
+
+ return lineData
+
+def update_lines(num, dataLines, lines) :
+ for line, data in zip(lines, dataLines) :
+ # NOTE: there is no .set_data() for 3 dim data...
+ line.set_data(data[0:2, :num])
+ line.set_3d_properties(data[2,:num])
+ return lines
+
+# Attaching 3D axis to the figure
+fig = plt.figure()
+ax = p3.Axes3D(fig)
+
+# Fifty lines of random 3-D lines
+data = [Gen_RandLine(25, 3) for index in xrange(50)]
+
+# Creating fifty line objects.
+# NOTE: Can't pass empty arrays into 3d version of plot()
+lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]
+
+# Setting the axes properties
+ax.set_xlim3d([0.0, 1.0])
+ax.set_xlabel('X')
+
+ax.set_ylim3d([0.0, 1.0])
+ax.set_ylabel('Y')
+
+ax.set_zlim3d([0.0, 1.0])
+ax.set_zlabel('Z')
+
+ax.set_title('3D Test')
+
+# Creating the Animation object
+line_ani = FuncAnimation(fig, update_lines, 25, fargs=(data, lines),
+ interval=50, blit=False)
+
+plt.show()
Added: trunk/matplotlib/examples/animation/simple_anim.py
===================================================================
--- trunk/matplotlib/examples/animation/simple_anim.py (rev 0)
+++ trunk/matplotlib/examples/animation/simple_anim.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,25 @@
+"""
+A simple example of an animated plot
+"""
+import numpy as np
+import matplotlib.pyplot as plt
+from animation import FuncAnimation
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+
+x = np.arange(0, 2*np.pi, 0.01) # x-array
+line, = ax.plot(x, np.sin(x))
+
+def animate(i):
+ line.set_ydata(np.sin(x+i/10.0)) # update the data
+ return line,
+
+#Init only required for blitting to give a clean slate.
+def init():
+ line.set_ydata(np.ma.array(x, mask=True))
+ return line,
+
+ani = FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
+ interval=25, blit=True)
+plt.show()
Added: trunk/matplotlib/examples/animation/strip_chart_demo.py
===================================================================
--- trunk/matplotlib/examples/animation/strip_chart_demo.py (rev 0)
+++ trunk/matplotlib/examples/animation/strip_chart_demo.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,49 @@
+"""
+Emulate an oscilloscope. Requires the animation API introduced in
+matplotlib 1.0 SVN.
+"""
+import matplotlib
+import numpy as np
+from matplotlib.lines import Line2D
+import matplotlib.pyplot as plt
+from animation import FuncAnimation
+
+class Scope:
+ def __init__(self, ax, maxt=10, dt=0.01):
+ self.ax = ax
+ self.dt = dt
+ self.maxt = maxt
+ self.tdata = [0]
+ self.ydata = [0]
+ self.line = Line2D(self.tdata, self.ydata)
+ self.ax.add_line(self.line)
+ self.ax.set_ylim(-.1, 1.1)
+ self.ax.set_xlim(0, self.maxt)
+
+ def update(self, y):
+ lastt = self.tdata[-1]
+ if lastt > self.tdata[0] + self.maxt: # reset the arrays
+ self.tdata = [self.tdata[-1]]
+ self.ydata = [self.ydata[-1]]
+ self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
+
+ t = self.tdata[-1] + self.dt
+ self.tdata.append(t)
+ self.ydata.append(y)
+ self.line.set_data(self.tdata, self.ydata)
+ return self.line,
+
+def emitter(p=0.01):
+ 'return a random value with probability p, else 0'
+ while True:
+ v = np.random.rand(1)
+ if v > p:
+ yield 0.
+ else:
+ yield np.random.rand(1)
+
+fig = plt.figure()
+ax = fig.add_subplot(111)
+scope = Scope(ax)
+ani = FuncAnimation(fig, scope.update, emitter, interval=10, blit=True)
+plt.show()
Added: trunk/matplotlib/examples/animation/subplots.py
===================================================================
--- trunk/matplotlib/examples/animation/subplots.py (rev 0)
+++ trunk/matplotlib/examples/animation/subplots.py 2010-08-26 03:23:25 UTC (rev 8660)
@@ -0,0 +1,93 @@
+import numpy as np
+import matplotlib.pyplot as plt
+from matplotlib.lines import Line2D
+from animation import TimedAnimation
+
+# This example uses subclassing, but there is no reason that the proper function
+# couldn't be set up and then use FuncAnimation. The code is long, but not
+# really complex. The length is due solely to the fact that there are a total
+# of 9 lines that need to be changed for the animation as well as 3 subplots
+# that need initial set up.
+class SubplotAnimation(TimedAnimation):
+ def __init__(self):
+ fig = plt.figure()
+ ax1 = fig.add_subplot(1, 2, 1)
+ ax2 = fig.add_subplot(2, 2, 2)
+ ax3 = fig.add_subplot(2, 2, 4)
+
+ self.t = np.linspace(0, 80, 400)
+ self.x = np.cos(2 * np.pi * self.t / 10.)
+ self.y = np.sin(2 * np.pi * self.t / 10.)
+ self.z = 10 * self.t
+
+ ax1.set_xlabel('x')
+ ax1.set_ylabel('y')
+ self.line1 = Line2D([], [], color='black')
+ self.line1a = Line2D([], [], color='red', linewidth=2)
+ self.line1e = Line2D([], [], color='red', marker='o', markeredgecolor='r')
+ ax1.add_line(self.line1)
+ ax1.add_line(self.line1a)
+ ax1.add_line(self.line1e)
+ ax1.set_xlim(-1, 1)
+ ax1.set_ylim(-2, 2)
+ ax1.set_aspect('equal', 'datalim')
+
+ ax2.set_xlabel('y')
+ ax2.set_ylabel('z')
+ self.line2 = Line2D([], [], color='black')
+ self.line2a = Line2D([], [], color='red', linewidth=2)
+ self.line2e = Line2D([], [], color='red', marker='o', markeredgecolor='r')
+ ax2.add_line(self.line2)
+ ax2.add_line(self.line2a)
+ ax2.add_line(self.line2e)
+ ax2.set_xlim(-1, 1)
+ ax2.set_ylim(0, 800)
+
+ ax3.set_xlabel('x')
+ ax3.set_ylabel('z')
+ self.line3 = Line2D([], [], color='black')
+ self.line3a = Line2D([], [], color='red', linewidth=2)
+ self.line3e = Line2D([], [], color='red', marker='o', markeredgecolor='r')
+ ax3.add_line(self.line3)
+ ax3.add_line(self.line3a)
+ ax3.add_line(self.line3e)
+ ax3.set_xlim(-1, 1)
+ ax3.set_ylim(0, 800)
+
+ TimedAnimation.__init__(self, fig, interval=50, blit=True)
+
+ def _draw_frame(self, framedata):
+ i = framedata
+ head = i - 1
+ head_len = 10
+ head_slice = (self.t > self.t[i] - 1.0) & (self.t < self.t[i])
+
+ self.line1.set_data(self.x[:i], self.y[:i])
+ self.line1a.set_data(self.x[head_slice], self.y[head_slice])
+ self.line1e.set_data(self.x[head], self.y[head])
+
+ self.line2.set_data(self.y[:i], self.z[:i])
+ self.line2a.set_data(self.y[head_slice], self.z[head_slice])
+ self.line2e.set_data(self.y[head], self.z[head])
+
+ self.line3.set_data(self.x[:i], self.z[:i])
+ self.line3a.set_data(self.x[head_slice], self.z[head_slice])
+ self.line3e.set_data(self.x[head], self.z[head])
+
+ self._drawn_artists = [self.line1, self.line1a, self.line1e,
+ self.line2, self.line2a, self.line2e,
+ self.line3, self.line3a, self.line3e]
+
+ def new_frame_seq(self):
+ return iter(range(self.t.size))
+
+ def _init_draw(self):
+ lines = [self.line1, self.line1a, self.line1e,
+ self.line2, self.line2a, self.line2e,
+ self.line3, self.line3a, self.line3e]
+ for l in lines:
+ l.set_data([], [])
+
+ani = SubplotAnimation()
+#ani.save('test_sub.mp4')
+plt.show()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|