所以我又回来了另一个愚蠢的问题。 考虑这段代码
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='+')
有没有办法只清除图表上的散点而不清除所有图像? 事实上,我正在编写一个代码,其中散点的外观与Tkinter Checkbutton绑定,我希望它在单击/取消单击按钮时显示/消失。
感谢您的帮助!
答案 0 :(得分:4)
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渲染函数都返回一个句柄,它有一些删除渲染项的方法。
请注意,您需要调用重绘以查看remove()
的效果 - 来自删除帮助(我的重点):
如果可能,请从图中删除艺术家。效果不会 在重新绘制图像之前可见,例如,使用 :甲基:
matplotlib.axes.Axes.draw_idle