From af1823848ca5befc7501e722bbaa4056ef9fd397 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 10 Oct 2018 13:46:48 +0200 Subject: [PATCH] Create a hidden colorbar on-the-fly to format imshow cursor data. ... if no colorbar exists yet. --- doc/users/next_whats_new/2018-10-10-AL.rst | 8 ++++++++ lib/matplotlib/image.py | 15 ++++++++++----- lib/matplotlib/tests/test_image.py | 17 +++++------------ 3 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 doc/users/next_whats_new/2018-10-10-AL.rst diff --git a/doc/users/next_whats_new/2018-10-10-AL.rst b/doc/users/next_whats_new/2018-10-10-AL.rst new file mode 100644 index 000000000000..4aeb22c924cd --- /dev/null +++ b/doc/users/next_whats_new/2018-10-10-AL.rst @@ -0,0 +1,8 @@ +Improved formatting of image values under cursor when a colorbar is present +``````````````````````````````````````````````````````````````````````````` + +To format image values under the mouse cursor into the status bar, we now +use the image's colorbar formatter (creating a hidden colorbar on-the-fly if +necessary). For example, for an image displaying the values 10,000 and 10,001, +the statusbar will now (using default 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 656cc47b0481..213bf28aba0b 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -936,14 +936,19 @@ def get_cursor_data(self, event): return arr[i, j] def format_cursor_data(self, data): - if np.ndim(data) == 0 and self.colorbar: - return ( - "[" + if np.ndim(data): + return super().format_cursor_data(data) + if not self.colorbar: + from matplotlib.figure import Figure + fig = Figure() + ax = fig.subplots() + self.colorbar = fig.colorbar(self, cax=ax) + # This will call the locator and call set_locs() on the formatter. + ax.yaxis._update_ticks() + return ("[" + cbook.strip_math( self.colorbar.formatter.format_data_short(data)).strip() + "]") - else: - return super().format_cursor_data(data) class NonUniformImage(AxesImage): diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index b6c47acc1e11..25894f793256 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -288,11 +288,11 @@ def test_cursor_data(): @pytest.mark.parametrize( - "data, text_without_colorbar, text_with_colorbar", [ - ([[10001, 10000]], "[1e+04]", "[10001]"), - ([[.123, .987]], "[0.123]", "[0.123]"), + "data, text", [ + ([[10001, 10000]], "[10001]"), + ([[.123, .987]], "[0.123]"), ]) -def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar): +def test_format_cursor_data(data, text): from matplotlib.backend_bases import MouseEvent fig, ax = plt.subplots() @@ -302,14 +302,7 @@ def test_format_cursor_data(data, text_without_colorbar, text_with_colorbar): event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp) 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) == data[0][0] - assert im.format_cursor_data(im.get_cursor_data(event)) \ - == text_with_colorbar + == text @image_comparison(baseline_images=['image_clip'], style='mpl20')