Reposition annotations on relayout


I’ve got some annotations that look like this:

Screenshot 2023-06-01 at 14.08.13

The data in the trace is dynamically updated on pan, zoom, and when other bits of data in the page change. This often results in the annotations not being in the correct place:

Screenshot 2023-06-01 at 14.10.14

In this the x coordinate at 5000 isn’t in the data set anymore.

Is it possible to find the closest point to the old annotation and re-create an annotation at that closest location?

I’m creating the original annotation on “plotly_click” where the event data gives me the point object to use. But the “plotly_relayout” event doesn’t have data like that. If I could get a list of the point objects from somewhere then I could re-create the annotation, but I don’t know how to get that outside of the click event handler?