title: How to speed up tkinter embedded matplot lib and python
body: <p>I have a GUI that I am using to send a query data from an arduino. I then went to live graph this data without slow down. I currently am getting slow down with the more numbers that I am plotting and i had a similar problem when I first wrote this GUI in matlab. I wanted to know if i could make this faster as I eventually want to be able to graph to multiple windows at once. I am aware of the animation library from a previous suggestion but was unsuccessful with getting it to work insife of my tkinter object from what I understand that would be the ideal way to speed up the graphing besides for drawing straight to the canvas. I only need to plot about 200 points of data at a time. Here  is the current GUI code: </p>
  
  <pre><code>import Tkinter
  import numpy as np
  import serial
  import time
  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  from matplotlib.figure import Figure
  from matplotlib import pyplot as plt
  import matplotlib.animation as animation
  from math import cos, sin
  from collections import deque
  
  class App:
      def __init__(self, master):
  
          frame = Tkinter.Frame(master)
  
          self.Max_press = Tkinter.StringVar()
          self.Max_press.set("10")
          self.Min_press = Tkinter.StringVar()
          self.Min_press.set("0")
          self.Cycle_per_minute = Tkinter.StringVar()
          self.Cycle_per_minute.set("12")
          self.Duration_cycle = Tkinter.StringVar()
          self.Duration_cycle.set("1")
  
          self.respiration = Tkinter.LabelFrame(frame, text="Respiration 
          Testing", borderwidth=10, relief=Tkinter.GROOVE, padx=10, pady=10)
          self.respiration.grid(row=0, column=0, padx=20, pady=20)
  
          self.max_pressure = Tkinter.Label(self.respiration, text="Maximum 
          Pressure (mmHg)")
          self.max_pressure.grid(row=0, column=0, padx=5, pady=5)
  
          self.Max_pressure = 
          Tkinter.Entry(self.respiration,textvariable=self.Max_press)
          self.Max_pressure.grid(row=1, column=0, padx=5, pady=5)
  
          self.min_pressure = Tkinter.Label(self.respiration, text="Minimum 
          Pressure (mmHg)")
          self.min_pressure.grid(row=2, column=0, padx=5, pady=5)
  
          self.Min_pressure = Tkinter.Entry(self.respiration, 
          textvariable=self.Min_press)
          self.Min_pressure.grid(row=3, column=0, padx=5, pady=5)
  
          self.cycles_per_minute = Tkinter.Label(self.respiration, 
          text="Cycles Per Minute")
          self.cycles_per_minute.grid(row=4, column=0, padx=5, pady=5)
  
          self.Cycles_per_minute = 
          Tkinter.Entry(self.respiration,textvariable=self.Cycle_per_minute)
          self.Cycles_per_minute.grid(row=5, column=0, padx=5, pady=5)
  
          self.duration_of_test = Tkinter.Label(self.respiration, 
          text="Duration (minutes)")
          self.duration_of_test.grid(row=6, column=0, padx=5, pady=5)
  
          self.Duration_of_test = Tkinter.Entry(self.respiration, 
          textvariable=self.Duration_cycle)
          self.Duration_of_test.grid(row=7, column=0, padx=5, pady=5)
  
          self.run_respiration = Tkinter.Button(self.respiration, text="RUN 
          RESPIRATION", command=self.getData)
          self.run_respiration.grid(row=8, column=0, padx=5, pady=5)
  
          self.burst = Tkinter.LabelFrame(frame, text="Burst Test", 
          borderwidth=10, relief=Tkinter.GROOVE, padx=10, pady=10 )
          self.burst.grid(row=0, column=1, padx=20, pady=20)
  
          self.burst_pressure = Tkinter.Button(self.burst, text="RUN BURST 
          TEST")
          self.burst_pressure.grid(row=0, column=0, padx=5, pady=5)
  
          self.test_options = Tkinter.LabelFrame(frame, text="Test Options", 
          borderwidth=10, relief=Tkinter.GROOVE, padx=10, pady=10 )
          self.test_options.grid(row=0, column=2, padx=20, pady=35)
  
          self.stop = Tkinter.Button(self.test_options, text="STOP", bd=10, 
          height=5, width=10)
          self.stop.grid(row=0, column=0, padx=10, pady=25)
  
          self.pause = Tkinter.Button(self.test_options, text="PAUSE", bd=10, 
          height=5, width=10)
          self.pause.grid(row=1, column=0, padx=10, pady=25)
  
          self.reset = Tkinter.Button(self.test_options, text="RESET", bd=10, 
          height=5, width=10)
          self.reset.grid(row=2, column=0, padx=10, pady=25)
  
          self.save = Tkinter.Button(self.test_options, text="SAVE", bd=10, 
          height=5, width=10)
          self.save.grid(row=3, column=0, padx=10, pady=25)
  
          self.xdata = deque([0]*200,maxlen=200)
          self.ydata = deque([0]*200,maxlen=200)
  
          self.fig = plt.Figure()
          self.ax1 = self.fig.add_subplot(111)
          self.line, = self.ax1.plot(self.xdata, self.ydata, lw=2)
          self.canvas = FigureCanvasTkAgg(self.fig,master=master)
          self.canvas.show()
          self.canvas.get_tk_widget().grid(row=0, column=3, padx=20, pady=20)
          frame.grid(row=0, column=0, padx=20, pady=20)
  
      def getData(self):
          press_max = float(self.Max_press.get())
          press_min = float(self.Min_press.get())
          duration = float(self.Duration_cycle.get())*60*20
          cycle_time = float(self.Cycles_per_minute.get())
          i = 0
          x = []
          y = []
          amp = (press_max - press_min)/2
          offset = amp + press_min
          spb = 60/cycle_time
          while (i &lt; duration):
              x.append(i)
              sine = amp*np.sin((x[i]*(np.pi*4))/(2*spb)) + offset + 1
              y.append(sine)
              i = i + 1
          arduinoData = serial.Serial('com5', 115200)
          arduinoData.flushInput()
          press = []
          t = []
          i = 0
          start = time.time()
          while (i &lt; duration + 1):
              while (arduinoData.inWaiting()==0):
                  pass
              arduinoString = arduinoData.readline()
              dataArray = int(arduinoString)
              i = i + 1
              print (dataArray)
              self.plotData(dataArray,i)
      end = time.time()
      print (end - start)
  
      def plotData(self, y, x ):
          self.xdata.append(x)
          self.ydata.append(y)
          self.ax1.plot(self.xdata, self.ydata)
          self.canvas.show()
  
  
  
  root = Tkinter.Tk()
  app = App(root)
  root.mainloop()
  </code></pre>
  
  <p>But the most important part is here: </p>
  
  <pre><code>        self.fig = plt.Figure()
          self.ax1 = self.fig.add_subplot(111)
          self.line, = self.ax1.plot(self.xdata, self.ydata, lw=2)
          self.canvas = FigureCanvasTkAgg(self.fig,master=master)
          self.canvas.show()
          self.canvas.get_tk_widget().grid(row=0, column=3, padx=20, pady=20)
          frame.grid(row=0, column=0, padx=20, pady=20)
  
      def getData(self):
          press_max = float(self.Max_press.get())
          press_min = float(self.Min_press.get())
          duration = float(self.Duration_cycle.get())*60*20
          cycle_time = float(self.Cycles_per_minute.get())
          i = 0
          x = []
          y = []
          amp = (press_max - press_min)/2
          offset = amp + press_min
          spb = 60/cycle_time
          while (i &lt; duration):
              x.append(i)
              sine = amp*np.sin((x[i]*(np.pi*4))/(2*spb)) + offset + 1
              y.append(sine)
              i = i + 1
          arduinoData = serial.Serial('com5', 115200)
          arduinoData.flushInput()
          press = []
          t = []
          i = 0
          start = time.time()
          while (i &lt; duration + 1):
              while (arduinoData.inWaiting()==0):
                  pass
              arduinoString = arduinoData.readline()
              dataArray = int(arduinoString)
              i = i + 1
              print (dataArray)
              self.plotData(dataArray,i)
          end = time.time()
          print (end - start)
  
      def plotData(self, y, x ):
          self.xdata.append(x)
          self.ydata.append(y)
          self.ax1.plot(self.xdata, self.ydata)
          self.canvas.show()
  
  root = Tkinter.Tk()
  app = App(root)
  root.mainloop()
  </code></pre>