The accepted answer to this question provides a means of dynamically updating matplotlib plots in a Jupyter notebook when using the nbagg
backend. However, this solution doesn't allow any interaction with the plots until after the loop has finished, which means you can only interact with the final version of the plot. Is it possible to interact with a dynamically updated figure after each update step?
What I'm trying to do is access matplotlib events (clicks in this case) on each of the images and then plot the next image after X number of clicks. The following code works with the qt
backend, but I can't figure out how to convert it to work with nbagg
instead. At the moment I can get either the event handling or the dynamic plotting to work, but not both simultaneously.
import numpy as np
import time
import matplotlib.pyplot as plt
n_img = 4
img = np.random.rand(20,20,n_img)
fig,ax = plt.subplots()
im = ax.imshow(img[:,:,0])
clicks = []
def onclick(event):
clicks.append(('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
(event.button, event.x, event.y, event.xdata, event.ydata)))
cid = fig.canvas.mpl_connect('button_press_event', onclick)
i = 1
while i < n_img:
plt.pause(0.01)
time.sleep(0.1)
if len(clicks) > 2: # plot next image after 3 clicks
clicks = []
im.set_data(img[:,:,i])
plt.draw()
i += 1