gpt4 book ai didi

python - 清除 matplotlib 图像上的叠加散点图

转载 作者:行者123 更新时间:2023-11-28 22:53:39 25 4
gpt4 key购买 nike

所以我又带着另一个愚蠢的问题回来了。考虑这段代码

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com