wim
wim

Reputation: 363243

how to stop the matplotlib GUI thread from freezing up?

from matplotlib import pyplot as p
from scipy import zeros
from Queue import Queue
import random

w,h = 320,200

black = zeros((h,w,3), dtype='uint8')
red = black.copy(); red[:,:,0] = 255
green = black.copy(); green[:,:,1] = 255
blue = black.copy(); blue[:,:,2] = 255

def ants():
  from scipy import rand, dstack
  return dstack([(255*rand(h,w)).astype('uint8')]*3)

fig = p.figure()
axs = [fig.add_subplot(1,3,i) for i in xrange(3)]
[ax.imshow(black) for ax in axs]

q = Queue()

def update_image(ax):
  ## this takes some time
  import time
  time.sleep(3)
  ax.images[0].set_data(random.choice([red, green, blue]))

def hit(event):
  if event.inaxes in axs:
    update_axs = [event.inaxes]
  else:
    update_axs = axs
  for ax in update_axs:
    ax.images[0].set_data(ants())
  p.draw()
#  for ax in update_axs:
#    update_image(ax)
#  p.draw()

cid = fig.canvas.mpl_connect('button_press_event', hit)
p.show()

here is my code, which all works as expected. however when i uncomment those 3 lines in the event handler, there are some problems i didn't expect. first of all the GUI freezes up while update_image is working , and secondly the first call to draw() does not seem to get a chance to paint because i don't see the racing ants while update_image is working. what is a better way to setup this kind of thing in matplotlib so it works smoothly?

Upvotes: 2

Views: 3991

Answers (1)

unutbu
unutbu

Reputation: 880547

To avoid freezing the GUI, you need to run update_image in a separate thread or process. Using threading, you might do something like this:

import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as p
from scipy import zeros
import random
import threading


w,h = 320,200

black = zeros((h,w,3), dtype='uint8')
red = black.copy(); red[:,:,0] = 255
green = black.copy(); green[:,:,1] = 255
blue = black.copy(); blue[:,:,2] = 255

def ants():
    from scipy import rand, dstack
    return dstack([(255*rand(h,w)).astype('uint8')]*3)

fig = p.figure()
axs = [fig.add_subplot(1,3,i) for i in xrange(3)]
[ax.imshow(black) for ax in axs]

def update_image(ax):
    ## this takes some time
    import time
    time.sleep(3)
    ax.images[0].set_data(random.choice([red, green, blue]))
    ax.figure.canvas.draw()

def hit(event):
    if event.inaxes in axs:
        update_axs = [event.inaxes]
    else:
        update_axs = axs
    for ax in update_axs:
        ax.images[0].set_data(ants())
    p.draw()
    for ax in update_axs:
        t=threading.Thread(target=update_image,args=(ax,))
        t.daemon=True
        t.start()

cid = fig.canvas.mpl_connect('button_press_event', hit)
p.show()

Upvotes: 2

Related Questions