Python 和从图中删除注释 [英] Python and Remove annotation from figure

查看:34
本文介绍了Python 和从图中删除注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using matplotlib inside of a wxpython GUI. In short, I have plotted a bunch of data. I then click on a data point (using DataCursor found at:

是否有与 MATLAB 的 datacursormode 等效的 matplotlib?

在 wxPython 中的 matplotlib 上弹出注释

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

The second link is my actual implementation. I refrense the datacursor class from other classes. What i'm wondering, how do i remove the annotation by clicking an event button? For instance, I have an event button that updates my scatter plot (by using plot_handle.set_data and not by clearing the figure). However, the annotation remains exactly where it was regardless if the point is there or not. How do i remove it?

谢谢!

推荐答案

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

Most matplotlib objects have a remove() function (I think it is inherited from Artist). Just call that on the object you want to remove.

如果你有

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 没有私有"属性的概念,因此您可以直接进入对象内部并在注释上调用 remove.请参阅有关类的教程以了解更多详情.

Python has no notion of 'private' attributes, so you can just reach inside you object and call remove on the annotation. See tutorial on classes for more details.

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

The down side of this is that now you have an object is a strange state. If you have any other references to it, they might behave badly. If you want to keep the DataCursor object around you can modify the annotation object using it's set_* functions or change it's visibility to temporarily hide it (doc)

这篇关于Python 和从图中删除注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆