From 5af615e5e17311d78e1c1776732eed11078a6178 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 8 Apr 2024 14:42:57 +0200 Subject: [PATCH] Switch to relying on mpl3.5's mappable value formatting. --- src/mplcursors/_mplcursors.py | 2 +- src/mplcursors/_pick_info.py | 28 ++++++---------------------- tests/test_mplcursors.py | 10 +++++----- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/mplcursors/_mplcursors.py b/src/mplcursors/_mplcursors.py index 1adc7a5..3646439 100644 --- a/src/mplcursors/_mplcursors.py +++ b/src/mplcursors/_mplcursors.py @@ -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. diff --git a/src/mplcursors/_pick_info.py b/src/mplcursors/_pick_info.py index 42ee4f8..135cbc5 100644 --- a/src/mplcursors/_pick_info.py +++ b/src/mplcursors/_pick_info.py @@ -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 @@ -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) @@ -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}" @@ -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}" diff --git a/tests/test_mplcursors.py b/tests/test_mplcursors.py index 0beeeca..dd7c45f 100644 --- a/tests/test_mplcursors.py +++ b/tests/test_mplcursors.py @@ -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 @@ -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()