title: Why matplotlib requires to plot only in the main thread?
body: <p>I'm trying to plot live the output of a generator.</p>
  
  <p>The following code works as expected (Ctrl-C terminates execution):</p>
  
  <pre><code>import numpy as np
  import pylab as p
  from Queue import Queue
  from threading import Thread
  import time
  
  def dataGenerator():
      while True:
          yield np.random.random()
  
  def populate():
      f = dataGenerator()
      while True:
          x = f.next(); y = f.next()
          q.put([x,y])
  
  q = Queue()
  
  p.figure(); p.hold(True); p.show(block=False)
  
  populatorThread = Thread(target=populate)
  populatorThread.daemon = True
  populatorThread.start()
  
  while True:
      data = q.get()
      x = data[0]
      y = data[1]
      p.plot(x,y,'o')
      p.draw()
      q.task_done()
  
  populatorThread.join()
  </code></pre>
  
  <p>However, if instead I put the plotting in a thread, I get <code>RuntimeError: main thread is not in main loop</code>:</p>
  
  <pre><code>import numpy as np
  import pylab as p
  from Queue import Queue
  from threading import Thread
  import time
  
  def dataGenerator():
      while True:
          yield np.random.random()
  
  def plotter():
      while True:
          data = q.get()
          x = data[0]
          y = data[1]
          p.plot(x,y,'o')
          p.draw()
          print x,y
          q.task_done()
  
  q = Queue()
  
  p.figure(); p.hold(True); p.show(block=False)
  
  plotThread = Thread(target=plotter)
  plotThread.daemon = True
  plotThread.start()
  
  f = dataGenerator()
  while True:
      x = f.next()
      y = f.next()
      q.put([x,y])
  
  plotThread.join()
  </code></pre>
  
  <p>Why does <code>matplotlib</code> care which thread does the plotting?</p>
  
  <p>EDIT: I'm not asking how to solve this but rather why is this happening in the first place.</p>