Guest User

wx_mpl_anim_example

a guest
May 10th, 2016
218
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. import matplotlib
  3. matplotlib.use('wxAgg')
  4. from matplotlib.figure import Figure
  5. import matplotlib.animation as animation
  6. from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
  7.  
  8. import wx
  9. import numpy as np
  10.  
  11.  
  12. class Frame(wx.Frame):
  13.     def __init__(self,*args,**kwargs):
  14.         wx.Frame.__init__(self,*args,**kwargs)
  15.        
  16.         self.initCtrls()
  17.         self.initAnim()
  18.         self.Show()
  19.        
  20.     def initCtrls(self):
  21.         self.mainsz = wx.BoxSizer(wx.VERTICAL)
  22.         self.figure = Figure()
  23.         self.axes = self.figure.add_subplot(111)
  24.         self.canvas = FigureCanvas(self, wx.ID_ANY, self.figure)
  25.        
  26.         self.mainsz.Add(self.canvas, 1, wx.EXPAND)
  27.         self.SetSizer(self.mainsz)
  28.        
  29.     def initAnim(self):
  30.         data = np.loadtxt("example.txt", delimiter=",")
  31.         self.x = data[:,0]
  32.         self.y = data[:,1]
  33.        
  34.         self.line1, = self.axes.plot([],[], '-')
  35.         self.line2, = self.axes.plot([],[],'--')
  36.         self.axes.set_xlim(np.min(self.x), np.max(self.x))
  37.         self.axes.set_ylim(np.min(self.y), np.max(self.y))
  38.        
  39.         K = 0.75
  40.         ani = animation.FuncAnimation(self.figure, self.animate, frames=len(self.x), fargs=(K,),
  41.                                       interval=100, blit=True)
  42.         self.canvas.draw()
  43.    
  44.     def animate(self,i,k):
  45.         self.line1.set_xdata(self.x[:i])
  46.         self.line1.set_ydata(self.y[:i])
  47.         self.line2.set_xdata(self.x[:i])
  48.         self.line2.set_ydata(k*self.y[:i])
  49.         return self.line1, self.line2
  50.  
  51.  
  52. if __name__ == "__main__":
  53.     app = wx.App()
  54.     fr = Frame(None, wx.ID_ANY, "Example")
  55.     app.MainLoop()
RAW Paste Data