4

So I am back again with another silly question. Consider this piece of code

x = linspace(-10,10,100);
[X,Y]=meshgrid(x,x)
g = np.exp(-(square(X)+square(Y))/2)
plt.imshow(g)
scat = plt.scatter(50,50,c='r',marker='+')

Is there a way to clear only the scatter point on the graph without clearing all the image? In fact, I am writing a code where the appearance of the scatter point is bound with a Tkinter Checkbutton and I want it to appear/disappear when I click/unclick the button.

Thanks for your help!

4

1 回答 1

7

的返回句柄plt.scatter有几种方法,包括remove(). 所以你需要做的就是调用它。用你的例子:

x = np.linspace(-10,10,100);
[X,Y] = np.meshgrid(x,x)
g = np.exp(-(np.square(X) + np.square(Y))/2)
im_handle = plt.imshow(g)
scat = plt.scatter(50,50,c='r', marker='+')
# image, with scatter point overlayed
scat.remove()
plt.draw()
# underlying image, no more scatter point(s) now shown

# For completeness, can also remove the other way around:
plt.clf()
im_handle = plt.imshow(g)
scat = plt.scatter(50,50,c='r', marker='+')
# image with both components
im_handle.remove()
plt.draw()
# now just the scatter points remain.

(几乎?)所有 matplotlib 渲染函数都返回一个句柄,它有一些方法可以删除渲染的项目。

请注意,您需要调用 redraw 以查看remove()-- 从删除帮助(我的重点)中的效果:

如果可能,将艺术家从图中移除。在重新绘制图形之前,效果将不可见,例如,使用 :meth: matplotlib.axes.Axes.draw_idle

于 2013-10-04T09:54:08.273 回答