title: Matplotlib animation inside your own GUI
body: <p>I'm writing software in Python. I need to embed a Matplotlib time-animation into a self-made GUI. Here are some more details about them:</p>
  
  <h3> 1. The GUI </h3>
  
  <p>The GUI is written in Python as well, using the PyQt4 library. My GUI is not very different from the common GUIs you can find on the net. I just subclass <strong>QtGui.QMainWindow</strong> and add some buttons, a layout, ... </p>
  
  <h3> 2. The animation </h3>
  
  <p>The Matplotlib animation is based on the <strong>animation.TimedAnimation</strong> class. Here is the code for the animation:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from matplotlib.lines import Line2D
  import matplotlib.animation as animation
  
          class CustomGraph(animation.TimedAnimation):
  
          def __init__(self):
  
              self.n = np.linspace(0, 1000, 1001)
              self.y = 1.5 + np.sin(self.n/20)
              #self.y = np.zeros(self.n.size)
  
  
              # The window
              self.fig = plt.figure()
              ax1 = self.fig.add_subplot(1, 2, 1)
              self.mngr = plt.get_current_fig_manager() 
              self.mngr.window.setGeometry(50,100,2000, 800)
  
              # ax1 settings
              ax1.set_xlabel('time')
              ax1.set_ylabel('raw data')
              self.line1 = Line2D([], [], color='blue')
              ax1.add_line(self.line1)
              ax1.set_xlim(0, 1000)
              ax1.set_ylim(0, 4)
  
  
              animation.TimedAnimation.__init__(self, self.fig, interval=20, blit=True)
  
  
          def _draw_frame(self, framedata):
              i = framedata
              print(i)
  
  
              self.line1.set_data(self.n[ 0 : i ], self.y[ 0 : i ])
  
              self._drawn_artists = [self.line1]
  
          def new_frame_seq(self):
              return iter(range(self.n.size))
  
          def _init_draw(self):
              lines = [self.line1]
              for l in lines:
                  l.set_data([], [])
  
          def showMyAnimation(self):
              plt.show()
  
  
      ''' End Class '''
  
  
      if __name__== '__main__':
          print("Define myGraph")
          myGraph = CustomGraph()
          myGraph.showMyAnimation()
  </code></pre>
  
  <p>This code produces a simple animation:</p>
  
  <p><a href="https://i.stack.imgur.com/oHfhf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/oHfhf.png" alt="Sine wave animation"></a></p>
  
  <p>The animation itself works fine. Run the code, the animation pops up in a small window and it starts running. But how do I embed the animation in my own self-made GUI?</p>
  
  <h3> 3. Embedding the animation in a self-made GUI </h3>
  
  <p>I have done some research to find out. Here are some things I tried. I have added the following code to the python file. Note that this added code is actually an extra class definition:</p>
  
  <pre><code>from PyQt4 import QtGui
  from PyQt4 import QtCore
  from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  
  class CustomFigCanvas(FigureCanvas):
  
      def __init__(self):
  
          self.myGraph = CustomGraph()
          FigureCanvas.__init__(self, self.myGraph.fig)
  </code></pre>
  
  <p>What I try to do here is embedding the <strong>CustomGraph()</strong> object - which is essentially my animation - into a <strong>FigureCanvas</strong>.</p>
  
  <p>I wrote my GUI in another python file (but still in the same folder). Normally I can add Widgets to my GUI. I believe that an object from the class <strong>CustomFigCanvas(..)</strong> is a Widget through inheritance. So this is what I try in my GUI:</p>
  
  <pre><code>    ..
      myFigCanvas = CustomFigCanvas()
      self.myLayout.addWidget(myFigCanvas)
      ..
  </code></pre>
  
  <p>It works to some extent. I get indeed a figure displayed in my GUI. But the figure is empty. The animation doesn't run:</p>
  
  <p><a href="https://i.stack.imgur.com/kUTNN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kUTNN.png" alt="my GUI with dysfunctional animation"></a></p>
  
  <p>And there is even another strange phenomenon going on. My GUI displays this empty figure, but I get simultaneously a regular Matplotlib popup window with my animation figure in it. Also this animation is not running.</p>
  
  <p>There is clearly something that I'm missing here. Please help me to figure out this problem. I appreciate very much all help.</p>