Updating annotations in mpl_interactions

@ianhi I am dealing with plots where each data point is annotated. For example, I am plotting a bunch of points (y vs x) , but next to the plotted point I am displaying in text, the y-value of the data point (with matplotlib’s annotate function).

Is there a dynamic equivalent of plt.annotate() in mpl_interactions? For example, if I have a bunch sliders which change the data points plotted, and hence their y-values and hence their annotated text.


plt.annotate (and Axes.annotate which is the actual implementation) should return an Annotation object (docs) which you can update the same way you are updating your other artists.

@echong not currently but it shouldn’t be too hard to implement. Would a dynamic plt.text also work? That’s even simpler to implement and I could probably do that today. Also happy to receive PRs for any of this.

Hi Ian, a dynamic plt.text() would be useful. I guess though for my current use case, the number of data points is not the same each time the plot updates, and hence the number of text objects will vary interactively. Not sure if that would play well.

In any case always happy to promote this work, it’s an awesome package! Additionally, do you have a scientific citation template that one can use for this?

There’s now iplt.text in version 0.22.0 :slight_smile: Text and Annotations — mpl-interactions

Should be possible with a little bit of work. I think the strategy is to start by creating the maximum number you’d ever need and then if their associated point is not present just set the text string to be empty so it doesn’t show up on the plot.

that’s wonderful to hear thank you! And also thank you for helping spur adding new functions please feel free to come with any and all suggestions, they are very helpful.

As for a citation I’ve been meaning to submit something to JOSS but have yet to do so. So if you wanted to cite (after a fashion) this a link to any subset or combination of the following would be amazing:
GitHub - ianhi/mpl-interactions: Sliders to control matplotlib and other interactive goodies. Works in any interactive backend and even uses ipywidgets when in a Jupyter notebook

Awesome, I’ll try it out! Thank you again!

@echong rough implementation of text appearing and disappearing with points

N = 25

x = np.linspace(0, np.pi, N)
y = np.sin(x)
max_x = (0.5, np.pi)

fig, ax = plt.subplots()

def fx(max_x):
    return x[x<max_x]

def fy(x_, max_x):
    # add underscore in the x so we can access the global x
    return y[x<max_x]
ctrls = iplt.plot(fx, fy,'o', max_x=max_x)

def gen_text_fxns(i):
    def fx(max_x):
        return x[i]
    def fy(x, max_x):
        return y[i]+0
    def s(max_x):
        if x[i]> max_x:
            return ""
            return f"{y[i]:.2f}"
    return fx, fy, s
with ctrls:
    for i in range(N):
1 Like