gpt4 book ai didi

Python 和从图中删除注释

转载 作者:太空狗 更新时间:2023-10-30 02:12:58 25 4
gpt4 key购买 nike

我在 wxpython GUI 中使用 matplotlib。简而言之,我绘制了一堆数据。然后我单击一个数据点(使用位于以下位置的 DataCursor:

Is there a matplotlib equivalent of MATLAB's datacursormode?

Pop up annotations on matplotlib within wxPython

第二个链接是我的实际实现。我从其他类中引用了 datacursor 类。我想知道的是,如何通过单击事件按钮来删除注释?例如,我有一个更新散点图的事件按钮(通过使用 plot_handle.set_data 而不是清除图形)。但是,无论该点是否存在,注释都会保留在原处。我该如何删除它?

谢谢!

最佳答案

大多数 matplotlib 对象都有一个 remove() 函数(我认为它是从 Artist 继承的)。只需在您要删除的对象上调用它即可。

编辑:

如果你有

dc = DataCursor(...) # what ever aruguements you give it
# bunch of code
dc.annotation.remove()
del dc
plt.draw() # need to redraw to make remove visible

Python 没有“私有(private)”属性的概念,因此您可以直接到达对象内部并在注释上调用 remove。参见 tutorial on classes for more details .

不利的一面是,现在您拥有一个对象是一种奇怪的状态。如果您对它有任何其他引用,它们可能表现不佳。如果您想保留 DataCursor 对象,您可以使用它的 set_* 函数修改 annotation 对象或更改它的可见性以暂时隐藏它( doc )

关于Python 和从图中删除注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222397/

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