快速更新绘图中的单点

时间:2018-03-09 23:17:59

标签: python matplotlib

我有一个matplotlib图,其中有几个图。一个人的图像中包含大量的点。第二个有一个轴,我想用快速更新来更新点位置。

下面的代码段缩小,以有效地显示我想要做的事情。当我在ax_large绘图周围移动鼠标时,我希望它能够快速更新。

我在SO和其他地方找到了很多例子,并尝试了一些不同的选择。但似乎没有一个人能够正确地适应这个法案(或者至少我希望/期望,所以也许我的期望需要改变)。

代码:

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()

当您在ax_large移动时,我希望它能够快速更新该点的位置。

任何关于如何做到这一点的想法都会有所帮助。

...谢谢

1 个答案:

答案 0 :(得分:1)

你基本上忽略了blitting所需的大部分内容。参见例如

像往常一样,你需要

  • 绘制画布fig.canvas.draw()
  • 存储背景以供日后使用,fig.canvas.copy_from_bbox()
  • 更新点point.set_data
  • 恢复背景fig.canvas.restore_region
  • 画出点ax.draw_artist
  • blit the axes,fig.canvas.blit

因此

import matplotlib.pyplot as plt
import numpy as np

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(True)

        # set some limits to the axes
        self.ax.set_xlim(-5,5)
        self.ax.set_ylim(-5,5)
        # Draw the canvas once
        self.fig.canvas.draw()
        # Store the background for later
        self.background = self.fig.canvas.copy_from_bbox(self.ax.bbox)
        # Now create some point
        self.point, = self.ax.plot(0,0, 'go')
        # Create callback to mouse movement
        self.cid = self.fig.canvas.callbacks.connect('motion_notify_event', 
                                                     self.callback)
        plt.show()

    def callback(self, event):
        if event.inaxes == self.ax:
            # Update point's location            
            self.point.set_data(event.xdata, event.ydata)
            # Restore the background
            self.fig.canvas.restore_region(self.background)
            # draw the point on the screen
            self.ax.draw_artist(self.point)
            # blit the axes
            self.fig.canvas.blit(self.ax.bbox)


tt = Test()