mplcursors icon indicating copy to clipboard operation
mplcursors copied to clipboard

mplcursors with pcolormesh fails

Open julbod opened this issue 5 years ago • 2 comments

mplcursors seems to work fine with contourf function, but fails with pcolormesh. Any idea? See below for example:

  1. 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()
  1. 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()

julbod avatar Apr 17 '20 10:04 julbod

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.

anntzer avatar Apr 17 '20 10:04 anntzer