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 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()
def __generate_crosshairs(self, xlabel=None, ylabel=None, decimals=2, **kwargs): # this is the main program
warnings.simplefilter('ignore') # required when using fill_between due to warning in mplcursors: "UserWarning: Pick support for PolyCollection is missing."
ch = cursor(hover=True)
add_lines_and_text_with_kwargs = lambda _: crosshairs.__add_lines_and_text_to_crosshairs(_, decimals, **kwargs) # adds the line's kwargs before connecting it to cursor
ch.connect("add", add_lines_and_text_with_kwargs)
plt.gcf().canvas.mpl_connect('axes_leave_event', crosshairs.__hide_crosshairs) # hide the crosshairs and text when the mouse leaves the axes
# does the annotation part
if xlabel is None:
xlabel = 'x'
if ylabel is None:
ylabel = 'y'
warnings.simplefilter('ignore') # required when using fill_between due to warning in mplcursors: "UserWarning: Pick support for PolyCollection is missing."
annot = cursor(multiple=True, bindings={"toggle_visible": "h"})
format_annotation_labeled = lambda _: crosshairs.__format_annotation(_, decimals, [xlabel, ylabel]) # adds the labels to the 'format_annotation' function before connecting it to cursor
annot.connect("add", format_annotation_labeled)
def main():
fig, axes = plt.subplots(ncols=2)
num = 5
xy = np.random.random((num, 2))
lines = []
for i in range(num):
line, = axes[0].plot((i + 1) * np.arange(10))
lines.append(line)
points = []
for x, y in xy:
point, = axes[1].plot([x], [y], linestyle="none", marker="o")
points.append(point)
cursor = mplcursors.cursor(points + lines, highlight=True)
pairs = dict(zip(points, lines))
pairs.update(zip(lines, points))
@cursor.connect("add")
def on_add(sel):
sel.extras.append(cursor.add_highlight(pairs[sel.artist]))
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import mplcursors
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots()
# Plot a series of lines with increasing slopes.
lines = []
for i in range(1, 20):
line, = ax.plot(x, i * x, label=f"$y = {i}x$")
lines.append(line)
mplcursors.cursor(lines, highlight=True)
plt.show()
def wrapper(self, *args, **kwargs):
rv = wrapper.__wrapped__(self, *args, **kwargs)
if self not in cursors:
cursor = mplcursors.cursor(self, **options)
if cursor.artists:
cursors[self] = cursor
else:
# No artist yet; skip possible
# initialization code.
cursor.remove()
return rv
module.Figure.draw = wrapper
ax = plt.gca()
ax.axis("equal")
plt.axis([-10, 10, -10, 10])
plt.suptitle(title, fontsize=16)
plt.subplots_adjust(top=0.95)
if save_png:
plt.savefig(image_path)
if show_window:
# only require mplcursors if we need an interactive plot
import mplcursors
# cooordinate_to_correlation[(sel.target.x, sel.target.y)]["corr_sun"])
cursors = mplcursors.cursor(hover=True)
@cursors.connect("add")
def _(sel):
sel.annotation.set(position=(15, -15))
# Note: Needs to be set separately due to matplotlib/matplotlib#8956.
sel.annotation.get_bbox_patch().set(fc="lightgrey")
sel.annotation.arrow_patch.set(arrowstyle="simple", fc="white", alpha=0)
sel.annotation.set_text("Correlation to sun \n{}".format(cordinate_to_correlation[ (sel.target[0],sel.target[1])]["corr_sun"]))
plt.show()
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()