title: Real time live graphs in Jupyter Notebook
body: <p>I have just started learning python to plot realtime gragh. I  have tried solutions provided on stackoverflow but none of them are working. Below is my code and it isn't woorking. Please help</p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import pyautogui as pg
  from matplotlib.animation import FuncAnimation
  
  %matplotlib notebook
  
  binSize = 512
  # fig(ax1,ax2) = plt.subplots(2,figsize=(12,6))
  f = []
  def animate(i):
      try:
          while True:
              x, y = pg.position()
              f.append(x)
      except KeyboardInterrupt:
             print('') 
  #     f.append(15) 
      if len(f)&lt;binSize :
          plt.cla()
          plt.plot(f, color='c',LineWidth=1.5,label=&quot;Noisy&quot;) 
      else:
          plt.cla()
          plt.plot(f[-binSize:],color='c',LineWidth=1.5,label=&quot;Noisy&quot;)
  ani = FuncAnimation(plt.gcf(),animate,interval=1);
  </code></pre>
  <p>So I have updated the code and trying to draw two subplots but after sometime</p>
  <ol>
  <li>Upper graph stopped clearing the canvas (Mouse X coordinates)</li>
  <li>Lower graph stopped updating the plot (FFT)</li>
  <li>When data grows beyond the binSize, notebook freezes and plots update really slowly</li>
  </ol>
  <pre><code>
  
      %matplotlib notebook
      
      binSize = 256
      # fig(ax1,ax2) = plt.subplots(2,figsize=(12,6))
      f = []
      t = 0
      dt = 1
      fig,axs = plt.subplots(2,1) 
      
      def animate(i):
          x, y = pg.position() 
          f.append(x) 
          n = len(f)
          if n&lt;binSize : 
              plt.sca(axs[0])
              plt.cla()
              plt.plot(f, color='c',LineWidth=1.5,label=&quot;MOUSE&quot;) 
          else:
              fhat = np.fft.fft(f,binSize)
              PSD = fhat*np.conj(fhat)/binSize
              freq  = (1/(dt*binSize))*np.arange(binSize)
              L = np.arange(1,np.floor(binSize/2),dtype='int')
              
              # update the code third time
        
                 
              axs[0].clear() 
              axs[0].plot(f[-binSize:], color='c',LineWidth=1.5,label=&quot;MOUSE&quot;) 
  #           axs[0].xlim(0,binSize) # this stopped the FFT graph to be plotted
          
  #           plt.cla()
              axs[1].clear()  
              axs[1].plot(freq[L],PSD[L],color='r',LineWidth=2,label=&quot;FFT&quot;)  
      #         plt.xlim(t[0],t[-1])
      #         plt.legend()
      
      #         plt.sca(axs[1])
      #         plt.plot(freq[L],PSD[L],color='c',LineWidth=2,label=&quot;Mouse FFT&quot;) 
      #         plt.xlim(0,300)
      #         plt.legend()
      #         plt.cla()
      #         plt.plot(f[-binSize:],color='c',LineWidth=1.5,label=&quot;Mouse&quot;)
              
      ani = FuncAnimation(plt.gcf(),animate,interval=dt)  
  </code></pre>