我使用Matplotlib允许用户使用鼠标点击选择有趣的数据点,使用与此答案非常相似的方法.
实际上,散热图显示在热图图像上,鼠标单击可以添加或删除散点图.
我的数据是在后台使用绘制的pcolormesh(),因此当我使用axis.figure.canvas.draw()散点图更新画布时,会重新绘制背景热图.考虑到热图的大小,这对于可用的接口来说太慢了.
有没有一种方法可以有选择地重新绘制散点而不重绘背景?
示例代码:
points = [] # Holds a list of (x,y) scatter points
def onclick(event):
# Click event handler to add points
points.append( (event.x, event.y) )
ax.figure.canvas.draw()
fig = plt.figure()
ax = plt.figure()
# Plot the background
ax.pcolormesh(heatmap_data)
# Add the listener to handle clicks
cid = fig.canvas.mpl_connect("button_press_event", onclick)
plt.show()
Run Code Online (Sandbox Code Playgroud)