Python and remove annotation from drawing

I am using matplotlib inside the wxpython GUI. In short, I built a bunch of data. Then I click on the data point (using the DataCursor found at:

Is there a matplotlib equivalent to datacursormode matlab?

or

Popup annotations on matplotlib in wxPython

The second link is my actual implementation. I am discarding the datacursor class from other classes. What am I interested in, how to remove annotation by clicking event button? For example, I have an event button that updates my scatter plot (using plot_handle.set_data, and not by clearing the shape). However, the annotation remains exactly where it was, regardless of whether the point exists or not. How to remove it?

Thanks!

+4
source share
2 answers

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

Edit:

If you

 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 does not have the concept of 'private' attributes, so you can just get inside the object and cause the annotation to be deleted. See the class textbook for more details.

The downside of this is that you now have an odd state object. If you have other links to it, they can behave badly. If you want to keep the DataCursor object around, you can change the annotation object using its set_* or change its visibility to temporarily hide it ( doc )

+6
source

You need to add it as an artist to remove it as an artist

 arrow2 = matplotlib.text.Annotation("I love it",xy=(0.5,0.5),xycoords='data',arrowprops=dict(arrowstyle="-")) ax2.add_artist(arrow2) arrow2.remove() 
+1
source

All Articles