作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我又带着另一个愚蠢的问题回来了。考虑这段代码
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 绑定(bind),我希望它在我单击/取消单击按钮时出现/消失。
感谢您的帮助!
最佳答案
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()
的效果——来自 remove 帮助(我强调):
Remove the artist from the figure if possible. The effect will not be visible until the figure is redrawn, e.g., with :meth:
matplotlib.axes.Axes.draw_idle
.
关于python - 清除 matplotlib 图像上的叠加散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176396/
我是一名优秀的程序员,十分优秀!