mplcursors
mplcursors copied to clipboard
mplcursors with pcolormesh fails
mplcursors seems to work fine with contourf function, but fails with pcolormesh. Any idea? See below for example:
- works
import numpy as np
import matplotlib.pyplot as plt
import mplcursors
np.random.seed(42)
fig, ax = plt.subplots()
cf = ax.contour(np.random.random((10, 10)))
cursor = mplcursors.cursor()
@cursor.connect("add")
def on_add(sel):
ann = sel.annotation
# `cf.collections.index(sel.artist)` is the index of the selected line
# among all those that form the contour plot.
# `cf.cvalues[...]` is the corresponding value.
ann.set_text("{}\nz={:.3g}".format(
ann.get_text(), cf.cvalues[cf.collections.index(sel.artist)]))
plt.show()
- Does not work, returns error message:
UserWarning: Pick support for QuadMesh is missing. warnings.warn(f"Pick support for {type(artist).name} is missing.")
import numpy as np
import matplotlib.pyplot as plt
import mplcursors
np.random.seed(42)
fig, ax = plt.subplots()
cf = ax.pcolormesh(np.random.random((10, 10)))
cursor = mplcursors.cursor()
@cursor.connect("add")
def on_add(sel):
ann = sel.annotation
# `cf.collections.index(sel.artist)` is the index of the selected line
# among all those that form the contour plot.
# `cf.cvalues[...]` is the corresponding value.
ann.set_text("{}\nz={:.3g}".format(
ann.get_text(), cf.cvalues[cf.collections.index(sel.artist)]))
plt.show()
That's what the warning says: no one wrote a picker for QuadMeshes. Someone needs to write a compute_pick(), a get_ann_text() and possibly a move() implementation for that class.