Guimoute
Guimoute

Reputation: 4649

How to delete an arrow created with ax.arrow()?

For context, I have a curve where I would like to highlight the width of a plateau with an annotation + an arrow (this one was made in Paint.NET).

enter image description here

To update the text and arrow, every time an input parameter changes I do:

ax.texts = [] # Clear the previous annotations.
ax.text(x, y, plateau_width_str)
??? # What goes here to clear the arrow? 
ax.arrow(x, y, dx=plateau_width, dy=0)

For now I'm not using gids here because I only have one text and one annotation at a time. What should be the third line? After calling ax.arrow() I tried exploring ax.collections and ax.patches but they are empty lists. Thanks in advance.

Upvotes: 0

Views: 1381

Answers (2)

I have tried the following:

ax.get_children() -> to get the list of all objects of axes after

ax.get_children()[0].remove() -> to remove the 0-element of axes list.

If you try ax.get_children() again you get a new list with the number of objects reduced, and the place "0" was substituted by the [1] element of the previous list. Then, every time you use ax.get_children()[0].remove() you will remove the sequence of elements, erasing each element that takes place which was removed before. Be careful!

Or you can try to choose the right element by setting the ID_number when you use ax.get_children()[ID_number].remove().

Upvotes: 2

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339560

You could directly create a method that removes the old and creates a new arrow, like self.create_arrow(*args, **kwargs).

It might look like

def create_arrow(self, *args, **kwargs):
    gid = kwargs.get("gid")
    if gid in self.some_dic:
        self.some_dic[gid].remove()
    arrow = self.ax.arrow(*args, **kwargs)
    self.some_dic.update({kwargs.get("gid") : arrow})

Where you have a dictionary self.some_dic to store the references in.

Upvotes: 1

Related Questions