matplotlib / ipympl

Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/
BSD 3-Clause "New" or "Revised" License
1.59k stars 225 forks source link

[FR] Cursor Control #533

Closed Servinjesus1 closed 1 year ago

Servinjesus1 commented 1 year ago

Apologies if this is already doable, but I'd love to be able to control settings about the cursor in interactive plots. In particular, I'd appreciate the ability to modify the precision of the coordinate display. I have a plot with time on the x-axis and would appreciate a finer grained view into the time when zoomed out (right now I have to zoom in to get that information, which is a lot of data to re-render and takes time).

Secondly, it would be really interesting to set the cursor style, such as having an infinite crosshair style cursor for pinpointing data relative to one another (look up "infinite crosshair cursor for AutoCAD" to see what I might want that to look like).

I have no experience with using widgets (the actual ipywidgets), so I'm not sure how difficult this would be to implement.

Servinjesus1 commented 1 year ago

Answered my own request!

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor

fig, ax = plt.subplots()
x = np.arange(np.datetime64("2022-07-05 06:00"), np.datetime64("2022-07-12 14:00"), np.timedelta64(1, "h"))
y = np.random.rand(len(x))
ax.plot(x, y)

# Object is datetime.datetime. Can use that library or pandas' to_datetime to convert value of x and format as you like
ax.format_coord = lambda x, y: f"x={pd.to_datetime(x, unit='d'):%Y-%m-%d %H:%M} y={y:.3f}"

# Cursor
cursorargs=dict(color='k', linewidth=1, useblit=True)  # optional cursor settings
cursor = Cursor(ax, **cursorargs)