mplcursors icon indicating copy to clipboard operation
mplcursors copied to clipboard

HowTo: Programatically add cursors

Open bryceschober opened this issue 5 years ago • 4 comments

How could I programmatically add cursors? My full use-case is that I'd like to be able to save/restore cursors between sessions, and it seems like the first step would be to capture the "add" events and save them off... but how would I restore them? It's not clear from the API docs whether you support adding cursors by xy points.

bryceschober avatar Nov 10 '20 00:11 bryceschober

I guess it depends on your exact application? If all you want to save is xy positions I would probably just programatically re-send click events to the canvas at these positions (by instantiating your own MouseEvents where needed); if you can actually save the objects in the cursor.selections attribute I guess Cursor.add_selection can be made to work for that purpose?

anntzer avatar Nov 10 '20 08:11 anntzer

I'll close this for lack of activity, but feel free to request a reopen if needed.

anntzer avatar Jan 14 '21 17:01 anntzer

Let's reopen this per #75 (and also because I now think a simpler programmatic API for adding cursors would indeed be nice to have). I guess the API should be something like def cursor.select(xy: tuple[float, float]) -> Selection where xy is in screen coordinates (i.e. it would be up to the user to call ax.transData(xydata) or ax.transAxes(xyaxes) or whatnot and pass that to select()), as I can't think of an easy way to guess what coordinate system the user may be wanting (even in the simplest case where all cursor targets are on the same axes, the user could reasonably want either data coordinates or axes relative coordinates; and even if we choose to prefer one of them here, everything will fall apart if the targets are not all in the same axes). Thoughts?

anntzer avatar Apr 26 '24 23:04 anntzer

Just want to chime in on this as I just discovered this lib and love it btw! The usecase for me would be to automatically select the cursor to the max (or min) Y-value in the plot for each dataseries. Would be nice if I didn't have to specify that value, it could just always select that and persist until a user clicks or hovers somewhere else then the regular behavior takes over.

bayank avatar May 09 '24 15:05 bayank