title: matplotlib - Drawing directly on the canvas
body: <p>Due to performance issues for dynamical updates, I need to draw directly a lot of rectangle on the canvas as very low level, that is to say without using <code>matplotlib.patches</code> and as we have to do with classical GUI.</p>
  
  <p>More precisely, I would like to only draw one rectangle and not only all the figure.</p>
  
  <p>Is it possible ?</p>
  
  <p>Here is my testing code using the link given by  Joe Kington.</p>
  
  <pre><code>#!/usr/bin/env python3
  
  from random import randint, choice
  import time
  
  import matplotlib.pyplot as plt
  import matplotlib.patches as mpatches
  import matplotlib.colors as colors
  import matplotlib
  
  back_color = "black"
  colors     = ['red', 'green', 'cyan', 'yellow']
  
  width  = 16
  height = 16
  
  ax     = plt.subplot(111)
  canvas = ax.figure.canvas
  
  ax.set_xlim([0, width])
  ax.set_ylim([0, height])
  
  def update():
      global ax, canvas, colors, width, height
  
      x = randint(0, width - 1)
      y = randint(0, height - 1)
  
      rect = mpatches.Rectangle(
          (x, y), 1, 1,
          facecolor = choice(colors),
          edgecolor = back_color
      )
  
      start = time.time()
      ax.draw_artist(rect)
      canvas.blit(ax.bbox)
      print("draw &gt;&gt;&gt;", time.time() - start)
  
  timer = canvas.new_timer(interval = 1)
  timer.add_callback(update)
  timer.start()
  
  plt.show()
  </code></pre>
  
  <p>Under Mac O$, I obtain the following message.</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backend_bases.py", line 1203, in _on_timer
      ret = func(*args, **kwargs)
    File "/Users/xxx/test.py", line 86, in update
      ax.draw_artist(rect)
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/axes.py", line 2100, in draw_artist
      a.draw(self._cachedRenderer)
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/artist.py", line 56, in draw_wrapper
      draw(artist, renderer, *args, **kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/patches.py", line 393, in draw
      gc = renderer.new_gc()
    File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backends/backend_macosx.py", line 97, in new_gc
      self.gc.save()
  RuntimeError: CGContextRef is NULL
  </code></pre>
  
  <h2>For Maverick users</h2>
  
  <p>I have a good news for the Mac users who want to play with the following codes, and also with animations. I've reinstalled Maverick OS on my Mac without any softwares, this is called a clean installation, and then I've installed Anaconda, and XQuark (<a href="http://support.apple.com/kb/ht5293" rel="nofollow">see this</a>).</p>
  
  <p>To make animations work, you just have to use the following two lines before any other matplotlib imports.</p>
  
  <pre><code>import matplotlib
  matplotlib.use('TkAgg')
  </code></pre>
  
  <p>I think this will work for any Mac OS supported by Anaconda. The use of XQuark is only needed for Maverick.</p>