Want elegant way to remove items from a plot

So, I have this big plot, containing lots of data. Over this plot, I am painting some arrows to showcase certain properties.

Now my program is interactive, and based on what actions the user takes, the arrows are to change. Since the plot contains a lot of data, I do not want to recreate it each time this happens. I just want the arrows to change.

For a lot of render objects in Bokeh, you can state a source, and with that source changing, the rendering changes. Something like that would be nice here, but this seems not to exist for arrows.

Therefore, what I have to do is for each new set of arrows, first delete the old set of arrows. And the code for that so far is ugly. So far, it works like this, with there being a class around all of this.

Important class members:
self.plot stores the actual plot
self.arrows stores the list of the current arrows

def update_arrows(self, data) -> None:
    self.clear_arrows()
    <do something with data>
    for <some iteration>:
        ...
        arrow = Arrow(...)
        self.plot.add_layout(arrow)
        self.arrows.append(arrow)

def clear_arrows(self) -> None:
        item_array = getattr(self.plot, "center")
        for arrow in self.arrows:
            item_array.remove(arrow)
        self.arrows = []

As one can see, in clear_arrows(), I am using a bit of black magic. I read into the source code of Bokeh, and found that with getattr, I can access the items within a plot. This doesn’t look like an official functionality that was made for the outside at all. But I haven’t found anything else which would work for me, say a fictional method plot.remove_layout(arrow).

Is there a more elegant way to do things here?

Do you always have the same number of arrows?

No. They are somewhat stable in their amount, but can easily vary in the exact number.
Also they do have an upper bound (that isn’t too high).

You were probably thinking about reordering them?
Given my somewhat stable amount, I might be able to have some turning invisible, but that would feel kinda dirty to me.

I don’t have any immediate suggestions, but I should state that the plan is for arrows to becomethe same as other “regular” glyphs (i.e. driven by a CDS that you can update), in the near-ish future.

Kay, then I guess I’ll keep my black magic for now and switch to the new structure as soon as it is out. Thanks for the answer.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.