title: Matplotlib says fig.canvas is None, so I can't use fig.canvas.draw
body: <p>I haven't used Matplotlib much. Based on someone's advice, I'm trying to write some plotting codes using object-oriented paradigms as much as possible---and therefore trying to use pure Matplotlib (i.e. not relying on pyplot) to generate some simple figures.</p>
  
  <p>A stripped-down version of my code looks like this:</p>
  
  <pre><code>import matplotlib as mpl
  
  time = [0,1,2,3,4]
  cell = [1,2,1,2,1]
  sample = [3,2,3,4,4]
  
  (figHt, figWd) = (5, 8) # in
  lBorderWidth = bBorderWidth = rBorderWidth = tBorderWidth = 0.1
  lbwh = (lBorderWidth, bBorderWidth,
          (1-lBorderWidth-rBorderWidth),
          (1-tBorderWidth-bBorderWidth)) # left, bottom, width, height
  
  fig = mpl.figure.Figure(figsize=(figHt, figWd))
  
  ax = fig.add_axes(lbwh)
  lines1, = ax.plot(time,cell,'k--')
  lines2, = ax.plot(time,sample,'k-')
  
  fig.legend([lines1,lines2],['p','q'],'upper left')
  fig.canvas.draw()
  </code></pre>
  
  <p>But when I run it, Python complains when it reaches <code>fig.canvas.draw()</code> that <code>canvas</code> type is <code>None</code>.</p>
  
  <p>Based on a reading of the <a href="http://matplotlib.org/users/artists.html" rel="nofollow">Matplotlib Artists tutorial</a>, it seems like <code>pyplot</code> takes care of a few behind-the-scenes setup tasks, most notably establishing the connection between the Figure object and the desired renderer/backend. The tutorial says:</p>
  
  <blockquote>
    <p>In the example below, we create a Figure instance using matplotlib.pyplot.figure(), which is a convenience method for
    instantiating Figure instances and connecting them with your user
    interface or drawing toolkit FigureCanvas. As we will discuss below,
    this is not necessary – you can work directly with PostScript, PDF
    Gtk+, or wxPython FigureCanvas instances, instantiate your Figures
    directly and connect them yourselves – but since we are focusing here
    on the Artist API we’ll let pyplot handle some of those details for us</p>
  </blockquote>
  
  <p>Unfortunately, that particular page doesn't proceed beyond generating plots with <code>pyplot.figure()</code>, so I am still trying to discover what the required steps are. Again, I realize pyplot can simplify this task---just trying to grok how all the pieces fit together.</p>
  
  <p>I saw this description of a base class used by backends, <a href="http://matplotlib.org/api/backend_bases_api.html#matplotlib.backend_bases.FigureCanvasBase" rel="nofollow"><code>FigureCanvasBase</code></a>, and I assume that I need to assign <code>fig.canvas</code> one of <code>FigureCanvasBase</code>'s subclasses.</p>
  
  <p>Also, I verified that Python is using a default backend. So I know the problem isn't caused by lack of a backend.</p>
  
  <pre><code>&gt;&gt;&gt; matplotlib.backends.backend
  'Qt4Agg'
  </code></pre>
  
  <p>Thanks in advance for any help. In summary, two questions:</p>
  
  <ol>
  <li>What am I missing that is causing this to fail? Is it because I didn't assign the figure object a renderer?</li>
  <li>I mentioned that I suspected I needed a subclass of <code>FigureCanvasBase</code> to move forward. Even if the problem can probably be solved more elegantly, is there a way to search the Python environment for subclasses that inherit from FigureCanvasBase? This might come in handy in other problems. </li>
  </ol>