Revision: 7515 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7515&view=rev Author: ryanmay Date: 2009-08-21 17:39:46 +0000 (Fri, 21 Aug 2009)
Log Message: ----------- Add an example showing how to dynamically resample for plotting using event handling. Added Paths: ----------- trunk/matplotlib/examples/event_handling/resample.py Added: trunk/matplotlib/examples/event_handling/resample.py =================================================================== --- trunk/matplotlib/examples/event_handling/resample.py (rev 0) +++ trunk/matplotlib/examples/event_handling/resample.py 2009-08-21 17:39:46 UTC (rev 7515) @@ -0,0 +1,53 @@ +import numpy as np +import matplotlib.pyplot as plt +from scikits.audiolab import wavread + +# A class that will downsample the data and recompute when zoomed. +class DataDisplayDownsampler(object): + def __init__(self, xdata, ydata): + self.origYData = ydata + self.origXData = xdata + self.numpts = 3000 + self.delta = xdata[-1] - xdata[0] + + def resample(self, xstart, xend): + # Very simple downsampling that takes the points within the range + # and picks every Nth point + mask = (self.origXData > xstart) & (self.origXData < xend) + xdata = self.origXData[mask] + ratio = int(xdata.size / self.numpts) + 1 + xdata = xdata[::ratio] + + ydata = self.origYData[mask] + ydata = ydata[::ratio] + + return xdata, ydata + + def update(self, ax): + # Update the line + lims = ax.viewLim + if np.abs(lims.width - self.delta) > 1e-8: + self.delta = lims.width + xstart, xend = lims.intervalx + self.line.set_data(*self.downsample(xstart, xend)) + ax.figure.canvas.draw_idle() + +# Read data +data = wavread('/usr/share/sounds/purple/receive.wav')[0] +ydata = np.tile(data[:, 0], 100) +xdata = np.arange(ydata.size) + +d = DataDisplayDownsampler(xdata, ydata) + +fig = plt.figure() +ax = fig.add_subplot(1, 1, 1) + +#Hook up the line +xdata, ydata = d.downsample(xdata[0], xdata[-1]) +d.line, = ax.plot(xdata, ydata) +ax.set_autoscale_on(False) # Otherwise, infinite loop + +# Connect for changing the view limits +ax.callbacks.connect('xlim_changed', d.update) + +plt.show() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ Matplotlib-checkins mailing list Matplotlib-checkins@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins