From 351a080b339aefda06657ea03f67a7d0eb7b81e7 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 17 Mar 2019 16:10:17 +0100 Subject: [PATCH] Use format_data_short to format image cursor data. Despite its name, format_data_short is actually used to format the x/y cursor data; it makes sense to also use it for image data. By doing so, e.g. for `imshow([[.123, .987]])`, one gets the more accurate `.123` string when the mouse is over the first pixel instead of `.1` that `__call__` returns (which would have been intended as a tick label). --- doc/users/next_whats_new/2018-10-10-AL.rst | 5 ++--- lib/matplotlib/image.py | 9 +++++---- lib/matplotlib/tests/test_image.py | 19 +++++++++++++------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/doc/users/next_whats_new/2018-10-10-AL.rst b/doc/users/next_whats_new/2018-10-10-AL.rst index ec12e55e88ca..668650f1541d 100644 --- a/doc/users/next_whats_new/2018-10-10-AL.rst +++ b/doc/users/next_whats_new/2018-10-10-AL.rst @@ -4,6 +4,5 @@ Improved formatting of image values under cursor when a colorbar is present When a colorbar is present, its formatter is now used to format the image values under the mouse cursor in the status bar. For example, for an image displaying the values 10,000 and 10,001, the statusbar will now (using default -settings) display the values as ``0.0+1e4`` and ``1.0+1e4`` (or ``10000.0`` -and ``10001.0`` if the offset-text is disabled on the colorbar), whereas both -values were previously displayed as ``1e+04``. +settings) display the values as ``10000`` and ``10001``), whereas both values +were previously displayed as ``1e+04``. diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index a1ca853756f4..69b54bca6a80 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -934,10 +934,11 @@ def get_cursor_data(self, event): def format_cursor_data(self, data): if self.colorbar: - return ("[" - + cbook.strip_math(self.colorbar.formatter(data)) - + cbook.strip_math(self.colorbar.formatter.get_offset()) - + "]") + return ( + "[" + + cbook.strip_math( + self.colorbar.formatter.format_data_short(data)).strip() + + "]") else: return super().format_cursor_data(data) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 7ff84fd48e4b..5cf5a7662921 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -291,22 +291,29 @@ def test_cursor_data(): assert im.get_cursor_data(event) is None -def test_format_cursor_data(): +@pytest.mark.parametrize( + "data, text_without_colorbar, text_with_colorbar", [ + ([[10001, 10000]], "[1e+04]", "[10001]"), + ([[.123, .987]], "[0.123]", "[0.123]"), +]) +def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar): from matplotlib.backend_bases import MouseEvent fig, ax = plt.subplots() - im = ax.imshow([[10000, 10001]]) + im = ax.imshow(data) xdisp, ydisp = ax.transData.transform_point([0, 0]) event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) - assert im.get_cursor_data(event) == 10000 - assert im.format_cursor_data(im.get_cursor_data(event)) == "[1e+04]" + assert im.get_cursor_data(event) == data[0][0] + assert im.format_cursor_data(im.get_cursor_data(event)) \ + == text_without_colorbar fig.colorbar(im) fig.canvas.draw() # This is necessary to set up the colorbar formatter. - assert im.get_cursor_data(event) == 10000 - assert im.format_cursor_data(im.get_cursor_data(event)) == "[0.0+1e4]" + assert im.get_cursor_data(event) == data[0][0] + assert im.format_cursor_data(im.get_cursor_data(event)) \ + == text_with_colorbar @image_comparison(baseline_images=['image_clip'], style='mpl20')