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

查看:211
本文介绍了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对象,或更改其可见性以暂时隐藏它(

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天全站免登陆