title: Matplotlib - Force plot display and then return to main code
body: <p>This is a MWE of what I'm after, adapted from <a href="https://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue">this question</a>:</p>
  
  <pre><code>from matplotlib.pyplot import plot, draw, show
  
  def make_plot():
      plot([1,2,3])
      draw()
      print 'continue computation'
  
  print('Do something before plotting.')
  # Now display plot in a window
  make_plot()
  
  answer = raw_input('Back to main and window visible? ')
  if answer == 'y':
      print('Excellent')
  else:
      print('Nope')
  
  show()
  </code></pre>
  
  <p>What I want is: I call the function to make the plot, the plot window appears, and then I get to go back to the prompt so I can input some value (based on that image that just displayed) and carry on with the code (the window can then close or remain there, I don't care).</p>
  
  <p>What I get instead is that the window with the plot only appears <strong>after</strong> the code is completed, which is no good.</p>
  
  <hr>
  
  <h2>Add 1</h2>
  
  <p>I've tried the following with the same results, the plot window appears at the end of the code and not before:</p>
  
  <pre><code>from matplotlib.pyplot import plot, ion, draw
  
  ion() # enables interactive mode
  plot([1,2,3]) # result shows immediately (implicit draw())
  # at the end call show to ensure window won't close.
  draw()
  
  answer = raw_input('Back to main and window visible? ')
  if answer == 'y':
      print('Excellent')
  else:
      print('Nope')
  </code></pre>
  
  <p>The same happens if I change <code>draw()</code> for <code>show()</code>.</p>
  
  <hr>
  
  <h2>Add 2</h2>
  
  <p>I've tried the following approach:</p>
  
  <pre><code>from multiprocessing import Process
  from matplotlib.pyplot import plot, show
  
  def plot_graph(*args):
      for data in args:
          plot(data)
      show()
  
  p = Process(target=plot_graph, args=([1, 2, 3],))
  p.start()
  
  print 'computation continues...'
  
  print 'Now lets wait for the graph be closed to continue...:'
  p.join()
  </code></pre>
  
  <p>which results in a <code>Python kernel has crashed</code> error in <code>Canopy</code> with the message:</p>
  
  <pre><code>The kernel (user Python environment) has terminated with error code -6. This may be due to a bug in your code or in the kernel itself.
  
  Output captured from the kernel process is shown below.
  
  [IPKernelApp] To connect another client to this kernel, use:
  [IPKernelApp] --existing /tmp/tmp9cshhw.json
  QGtkStyle could not resolve GTK. Make sure you have installed the proper libraries.
  [xcb] Unknown sequence number while processing queue
  [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
  [xcb] Aborting, sorry about that.
  python: ../../src/xcb_io.c:274: poll_for_event: La declaraciÃ³n `!xcb_xlib_threads_sequence_lost' no se cumple.
  </code></pre>
  
  <p>I should mention I'm running <code>Canopy</code> in <code>elementary OS</code> which is based in <code>Ubuntu 12.04</code>.</p>
  
  <hr>
  
  <h2>Add 3</h2>
  
  <p>Also tried solution posted in <a href="https://stackoverflow.com/questions/11129731/can-i-generate-and-show-a-different-image-during-each-loop-with-matplotlib">this question</a>:</p>
  
  <pre><code>import numpy
  from matplotlib import pyplot as plt
  
  if __name__ == '__main__':
      x = [1, 2, 3]
      plt.ion() # turn on interactive mode
      for loop in range(0,3):
          y = numpy.dot(x, loop)
          plt.figure()
          plt.plot(x,y)
          plt.show()
          _ = raw_input("Press [enter] to continue.")
  </code></pre>
  
  <p>This displays empty plot windows as the code advances (ie: the user hits [enter]) and only displays the images after the code is finished.</p>
  
  <p>This solution (also in the same question) doesn't even display the plot windows:</p>
  
  <pre><code>import numpy
  from matplotlib import pyplot as plt
  if __name__ == '__main__':
      x = [1, 2, 3]
      plt.ion() # turn on interactive mode, non-blocking `show`
      for loop in range(0,3):
          y = numpy.dot(x, loop)
          plt.figure()   # create a new figure
          plt.plot(x,y)  # plot the figure
          plt.show()     # show the figure, non-blocking
          _ = raw_input("Press [enter] to continue.") # wait for input from the user
          plt.close()    # close the figure to show the next one.
  </code></pre>