From e9be828388c9fda85b887143d738630a2a823e8c Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 27 Aug 2019 10:01:45 +0100 Subject: [PATCH 1/2] Fix ScalarFormatter formatting of masked values --- lib/matplotlib/ticker.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 0c936d2079f1..f06a91214d70 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -629,6 +629,8 @@ def format_data_short(self, value): """ if self._useLocale: return locale.format_string('%-12g', (value,)) + elif isinstance(value, np.ma.MaskedArray) and value.mask: + return '' else: return '%-12g' % value From 2b02440f6da89feb6d6b2d064c4a33655be5f98f Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 27 Aug 2019 10:46:37 +0100 Subject: [PATCH 2/2] Add masked value formatting test --- lib/matplotlib/tests/test_image.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index fc05ae6894b7..d56e5d3d9eb4 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1103,3 +1103,18 @@ def test_respects_bbox(): buf_after = io.BytesIO() fig.savefig(buf_after, format="rgba") assert buf_before.getvalue() != buf_after.getvalue() # Not all white. + + +def test_image_cursor_formatting(): + fig, ax = plt.subplots() + # Create a dummy image to be able to call format_cursor_data + im = ax.imshow(np.zeros((4, 4))) + + data = np.ma.masked_array([0], mask=[True]) + assert im.format_cursor_data(data) == '[]' + + data = np.ma.masked_array([0], mask=[False]) + assert im.format_cursor_data(data) == '[0]' + + data = np.nan + assert im.format_cursor_data(data) == '[nan]'