Python 快速更新绘图中的单个点
我有一个matplotlib图,其中有几个图。一个图像中有大量的点。第二个有一个轴,我想在其中用快速更新点位置 下面的一段代码经过浓缩,有效地展示了我想要做的事情。当我在Python 快速更新绘图中的单个点,python,matplotlib,Python,Matplotlib,我有一个matplotlib图,其中有几个图。一个图像中有大量的点。第二个有一个轴,我想在其中用快速更新点位置 下面的一段代码经过浓缩,有效地展示了我想要做的事情。当我在ax\u large绘图上移动鼠标时,我希望它能快速更新 我在某地和其他地方找到了许多例子,并尝试了一些不同的选择。但似乎没有一个完全符合要求(或者至少如我所希望/期望的那样,因此我的期望可能需要改变) 守则: class Test: def __init__(self): self.fig = plt
ax\u large
绘图上移动鼠标时,我希望它能快速更新
我在某地和其他地方找到了许多例子,并尝试了一些不同的选择。但似乎没有一个完全符合要求(或者至少如我所希望/期望的那样,因此我的期望可能需要改变)
守则:
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
上移动时,我希望它能快速更新点的位置
任何关于如何做到这一点的想法都会很有帮助
谢谢…您实际上忽略了blitting所需的大部分内容。见例
- 绘制画布,
fig.canvas.Draw()
- 存储背景以便以后使用,
fig.canvas.copy\u from\u bbox()
- 更新点,
点。设置数据
- 还原背景,
fig.canvas.Restore\u区域
- 画点,
ax.画艺术家
- blit轴,
fig.canvas.blit
你基本上忽略了闪电战所需要的大部分东西。见例
- 绘制画布,
fig.canvas.Draw()
- 存储背景以便以后使用,
fig.canvas.copy\u from\u bbox()
- 更新点,
点。设置数据
- 还原背景,
fig.canvas.Restore\u区域
- 画点,
ax.画艺术家
- blit轴,
fig.canvas.blit
如果你看这些例子,你只实现了blitting所需的一半。如果你看这些例子,你只实现了blitting所需的一半。
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()