I have a matplotlib figure in which I have several plots. One has an image with a large number of points in it. The second has an axis in which I want to update a point location with fast updates.
The piece of code below is condensed down to show effectively what I would like to do. As I move the mouse around the ax_large
plot I want it to update quite quickly.
I found numerous examples on SO and other places and was trying some different options. But none seem to fit the bill quite correctly (or at least as I would hope / expect, so maybe my expectations need to change).
The code:
class Test:
def __init__(self):
self.fig = plt.figure(1)
# Axis with large plot
self.ax_large = plt.subplot(121)
self.ax_large.imshow(np.random.random((5000,5000)))
# Follow the point
self.ax = plt.subplot(122)
self.ax.grid('on')
self.fig.canvas.callbacks.connect('motion_notify_event', self.callback)
self.point = self.ax.plot(0,0, 'go')
plt.show()
def callback(self, event):
if event.inaxes == self.ax:
print('Updating to {} {}'.format(event.xdata, event.ydata))
self.point[0].set_data(event.xdata, event.ydata)
# Option 1. Works, bu super slow if there are other large sub-plots
plt.draw()
# Option 2. Doesn't update
# self.fig.canvas.blit(self.ax.bbox)
# Option 3. Works but then grid goes away
# self.ax.redraw_in_frame()
# self.fig.canvas.blit(self.ax.bbox)
# Option 4. Doesn't update
# self.ax.draw_artist(self.point[0])
# Option 5. Draws new point but does not remove the "old" one
# self.ax.draw_artist(self.point[0])
# self.fig.canvas.blit(self.ax.bbox)
if __name__ == '__main__':
tt = Test()
As you move around the ax_large
I was hoping it would be a fast update for the point's location.
Any ideas on how to do this would be helpful.
Thanks...