Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_nan(ax, plot_args, click, targets):
ax.plot(*plot_args)
cursor = mplcursors.cursor()
_process_event("__mouse_click__", ax, click, 1)
assert len(cursor.selections) == len(ax.texts) == len(targets)
for sel, target in zip(cursor.selections, targets):
assert sel.target == approx(target)
def test_image_subclass(ax):
# Cannot move around `PcolorImage`s.
ax.pcolorfast(np.arange(3) ** 2, np.arange(3) ** 2, np.zeros((2, 2)))
cursor = mplcursors.cursor()
with pytest.warns(UserWarning):
_process_event("__mouse_click__", ax, (1, 1), 1)
assert len(cursor.selections) == 0
def test_hover(ax, hover):
l1, = ax.plot([0, 1])
l2, = ax.plot([1, 2])
cursor = mplcursors.cursor(hover=hover)
_process_event("motion_notify_event", ax, (.5, .5), 1)
assert len(cursor.selections) == 0 # No trigger if mouse button pressed.
_process_event("motion_notify_event", ax, (.5, .5))
assert cursor.selections[0].artist == l1
_process_event("motion_notify_event", ax, (.5, 1))
assert bool(cursor.selections) == (hover == HoverMode.Persistent)
_process_event("motion_notify_event", ax, (.5, 1.5))
assert cursor.selections[0].artist == l2
"hover", [True, HoverMode.Persistent, 2, HoverMode.Transient])
def test_hover(ax, hover):
l1, = ax.plot([0, 1])
l2, = ax.plot([1, 2])
cursor = mplcursors.cursor(hover=hover)
_process_event("motion_notify_event", ax, (.5, .5), 1)
assert len(cursor.selections) == 0 # No trigger if mouse button pressed.
_process_event("motion_notify_event", ax, (.5, .5))
assert cursor.selections[0].artist == l1
_process_event("motion_notify_event", ax, (.5, 1))
assert bool(cursor.selections) == (hover == HoverMode.Persistent)
_process_event("motion_notify_event", ax, (.5, 1.5))
assert cursor.selections[0].artist == l2
def test_steps_index():
index = _pick_info.Index(0, .5, .5)
assert np.floor(index) == 0 and np.ceil(index) == 1
assert str(index) == "0.(x=0.5, y=0.5)"
def test_containerartist(ax):
artist = _pick_info.ContainerArtist(ax.errorbar([], []))
str(artist)
repr(artist)
def test_selection_identity_comparison():
sel0, sel1 = [Selection(artist=None,
target=np.array([0, 0]),
dist=0,
annotation=None,
extras=[])
for _ in range(2)]
assert sel0 != sel1
def _internal_warnings(record):
return [
warning for warning in record
if Path(mplcursors.__file__).parent in Path(warning.filename).parents]
def cleanup():
with mpl.rc_context({"axes.unicode_minus": False}):
try:
yield
finally:
mplcursors.__warningregistry__ = {}
plt.close("all")
def plot_with_hover(tpf, gaiaXY, gaiaID, gaiaMAG, ticID, tmag):
fig, ax = plt.subplots()
ax.imshow(tpf.flux[0], origin='lower')
sc = ax.scatter(gaiaXY[0], gaiaXY[1], c='k', s=10)
plt.xlim([-0.5,8.5])
plt.ylim([-0.5,8.5])
mplcursors.cursor(sc).connect(
"add", lambda sel: sel.annotation.set_text("TIC ID = {}\nTmag = {}\nGaia ID = {}\nGmag = {}".format(ticID[sel.target.index],
tmag[sel.target.index],
gaiaID[sel.target.index],
gaiaMAG[sel.target.index])))
plt.show()