Skip to content

Commit

Permalink
Switch to relying on mpl3.5's mappable value formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Apr 8, 2024
1 parent 1159910 commit 5af615e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/mplcursors/_mplcursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def add_selection(self, pi):
get_cached_renderer = (
figure.canvas.get_renderer
if hasattr(figure.canvas, "get_renderer")
else axes.get_renderer_cache) # matplotlib <3.6.
else axes.get_renderer_cache) # mpl<3.6.
renderer = get_cached_renderer()
if renderer is None:
figure.canvas.draw() # Needed below anyways.
Expand Down
28 changes: 6 additions & 22 deletions src/mplcursors/_pick_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,10 @@ def _format_coord_unspaced(ax, pos):
return ""
else:
x, y = pos
return f"x={ax.format_xdata(x)}\ny={ax.format_ydata(y)}"
# In mpl<3.3 (before #16776) format_x/ydata included trailing
# spaces, hence the rstrip() calls.
return (f"x={ax.format_xdata(x).rstrip()}\n"
f"y={ax.format_ydata(y).rstrip()}")


@functools.singledispatch
Expand All @@ -546,25 +549,6 @@ def _strip_math(s):
return cbook.strip_math(s) if len(s) >= 2 and s[0] == s[-1] == "$" else s


def _format_scalarmappable_value(artist, idx): # matplotlib/matplotlib#12473.
data = artist.get_array()[idx]
if np.ndim(data) == 0:
if not artist.colorbar:
fig = Figure()
ax = fig.subplots()
artist.colorbar = Colorbar(ax, artist)
# This hack updates the ticks without actually paying the cost of
# drawing (RendererBase.draw_path raises NotImplementedError).
try:
ax.yaxis.draw(RendererBase())
except NotImplementedError:
pass
fmt = artist.colorbar.formatter.format_data_short
return "[" + _strip_math(fmt(data).strip()) + "]"
else:
return artist.format_cursor_data(data) # Includes brackets.


@get_ann_text.register(Line2D)
@get_ann_text.register(LineCollection)
@get_ann_text.register(PatchCollection)
Expand All @@ -581,7 +565,7 @@ def _(sel):
# to involve an arbitrary scaling).
and artist.get_array() is not None
and len(artist.get_array()) == len(artist.get_offsets())):
value = _format_scalarmappable_value(artist, sel.index)
value = artist.format_cursor_data(artist.get_array()[sel.index])
text = f"{text}\n{value}"
if re.match("[^_]", label):
text = f"{label}\n{text}"
Expand All @@ -596,7 +580,7 @@ def _(sel):
def _(sel):
artist = sel.artist
text = _format_coord_unspaced(artist.axes, sel.target)
cursor_text = _format_scalarmappable_value(artist, sel.index)
cursor_text = artist.format_cursor_data(artist.get_array()[sel.index])
return f"{text}\n{cursor_text}"


Expand Down
10 changes: 5 additions & 5 deletions tests/test_mplcursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,16 @@ def test_image(ax, origin):
# Annotation text includes image value.
_process_event("__mouse_click__", ax, (.25, .25), 1)
sel, = cursor.selections
assert _parse_annotation(
sel, r"x=(.*)\ny=(.*)\n\[0\]") == approx((.25, .25))
assert _parse_annotation( # Since mpl3.5 the value repr has extra zeros.
sel, r"x=(.*)\ny=(.*)\n\[0(?:\.00)?\]") == approx((.25, .25))
# Moving around.
_process_event("key_press_event", ax, (.123, .456), "shift+right")
sel, = cursor.selections
assert _parse_annotation(sel, r"x=(.*)\ny=(.*)\n\[1\]") == (1, 0)
assert _parse_annotation(sel, r"x=(.*)\ny=(.*)\n\[1(?:\.00)\]") == (1, 0)
assert array[sel.index] == 1
_process_event("key_press_event", ax, (.123, .456), "shift+right")
sel, = cursor.selections
assert _parse_annotation(sel, r"x=(.*)\ny=(.*)\n\[0\]") == (0, 0)
assert _parse_annotation(sel, r"x=(.*)\ny=(.*)\n\[0(?:\.00)\]") == (0, 0)
assert array[sel.index] == 0
_process_event("key_press_event", ax, (.123, .456), "shift+up")
sel, = cursor.selections
Expand All @@ -269,7 +269,7 @@ def test_image(ax, origin):
assert array[sel.index] == {"upper": 4, "lower": 2}[origin]
_process_event("key_press_event", ax, (.123, .456), "shift+down")
sel, = cursor.selections
assert _parse_annotation(sel, r"x=(.*)\ny=(.*)\n\[0\]") == (0, 0)
assert _parse_annotation(sel, r"x=(.*)\ny=(.*)\n\[0(?:\.00)\]") == (0, 0)
assert array[sel.index] == 0

cursor = mplcursors.cursor()
Expand Down

0 comments on commit 5af615e

Please sign in to comment.