code_junkie

Is there a way to detach matplotlib plots so that the computation can continue

Nov 13th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. from matplotlib.pyplot import *
  2. plot([1,2,3])
  3. show()
  4. # other code
  5.  
  6. from matplotlib import plot, draw, show
  7. plot([1,2,3])
  8. draw()
  9. print 'continue computation'
  10.  
  11. # at the end call show to ensure window won't close.
  12. show()
  13.  
  14. from matplotlib import plot, ion, show
  15. ion() # enables interactive mode
  16. plot([1,2,3]) # result shows immediatelly (implicit draw())
  17.  
  18. print 'continue computation'
  19.  
  20. # at the end call show to ensure window won't close.
  21. show()
  22.  
  23. from multiprocessing import Process
  24. from matplotlib import plot, show
  25.  
  26. def plot_graph(*args):
  27. for data in args:
  28. plot(data)
  29. show()
  30.  
  31. p = Process(target=plot_graph, args=([1, 2, 3],))
  32. p.start()
  33.  
  34. print 'yay'
  35. print 'computation continues...'
  36. print 'that rocks.'
  37.  
  38. print 'Now lets wait for the graph be closed to continue...:'
  39. p.join()
  40.  
  41. from matplotlib.pyplot import draw, figure, show
  42. f1, f2 = figure(), figure()
  43. af1 = f1.add_subplot(111)
  44. af2 = f2.add_subplot(111)
  45. af1.plot([1,2,3])
  46. af2.plot([6,5,4])
  47. draw()
  48. print 'continuing computation'
  49. show()
  50.  
  51. import sys
  52. import gtk, gobject
  53. import matplotlib
  54. matplotlib.use('GTKAgg')
  55. import pylab as p
  56. import numpy as nx
  57. import time
  58.  
  59. import threading
  60.  
  61.  
  62.  
  63. ax = p.subplot(111)
  64. canvas = ax.figure.canvas
  65.  
  66. # for profiling
  67. tstart = time.time()
  68.  
  69. # create the initial line
  70. x = nx.arange(0,2*nx.pi,0.01)
  71. line, = ax.plot(x, nx.sin(x), animated=True)
  72.  
  73. # save the clean slate background -- everything but the animated line
  74. # is drawn and saved in the pixel buffer background
  75. background = canvas.copy_from_bbox(ax.bbox)
  76.  
  77.  
  78. # just a plain global var to pass data (from main, to plot update thread)
  79. global mypass
  80.  
  81. # http://docs.python.org/library/multiprocessing.html#pipes-and-queues
  82. from multiprocessing import Pipe
  83. global pipe1main, pipe1upd
  84. pipe1main, pipe1upd = Pipe()
  85.  
  86.  
  87. # the kind of processing we might want to do in a main() function,
  88. # will now be done in a "main thread" - so it can run in
  89. # parallel with gobject.idle_add(update_line)
  90. def threadMainTest():
  91. global mypass
  92. global runthread
  93. global pipe1main
  94.  
  95. print "tt"
  96.  
  97. interncount = 1
  98.  
  99. while runthread:
  100. mypass += 1
  101. if mypass > 100: # start "speeding up" animation, only after 100 counts have passed
  102. interncount *= 1.03
  103. pipe1main.send(interncount)
  104. time.sleep(0.01)
  105. return
  106.  
  107.  
  108. # main plot / GUI update
  109. def update_line(*args):
  110. global mypass
  111. global t0
  112. global runthread
  113. global pipe1upd
  114.  
  115. if not runthread:
  116. return False
  117.  
  118. if pipe1upd.poll(): # check first if there is anything to receive
  119. myinterncount = pipe1upd.recv()
  120.  
  121. update_line.cnt = mypass
  122.  
  123. # restore the clean slate background
  124. canvas.restore_region(background)
  125. # update the data
  126. line.set_ydata(nx.sin(x+(update_line.cnt+myinterncount)/10.0))
  127. # just draw the animated artist
  128. ax.draw_artist(line)
  129. # just redraw the axes rectangle
  130. canvas.blit(ax.bbox)
  131.  
  132. if update_line.cnt>=500:
  133. # print the timing info and quit
  134. print 'FPS:' , update_line.cnt/(time.time()-tstart)
  135.  
  136. runthread=0
  137. t0.join(1)
  138. print "exiting"
  139. sys.exit(0)
  140.  
  141. return True
  142.  
  143.  
  144.  
  145. global runthread
  146.  
  147. update_line.cnt = 0
  148. mypass = 0
  149.  
  150. runthread=1
  151.  
  152. gobject.idle_add(update_line)
  153.  
  154. global t0
  155. t0 = threading.Thread(target=threadMainTest)
  156. t0.start()
  157.  
  158. # start the graphics update thread
  159. p.show()
  160.  
  161. print "out" # will never print - show() blocks indefinitely!
  162.  
  163. fig = plt.figure()
  164. ax = fig.add_subplot(1,1,1)
  165. ax.plot(x,y)
  166.  
  167. # set processing to continue when window closed
  168. def onclose(event):
  169. fig.canvas.stop_event_loop()
  170. fig.canvas.mpl_connect('close_event', onclose)
  171.  
  172. fig.show() # this call does not block on my system
  173. fig.canvas.start_event_loop_default() # block here until window closed
  174.  
  175. # continue with further processing, perhaps using result from callbacks
  176.  
  177. C:Python26libsite-packagesmatplotlibbackend_bases.py:2051: DeprecationWarning: Using default event loop until function specific to this GUI is implemented
  178. warnings.warn(str,DeprecationWarning)
Add Comment
Please, Sign In to add comment