From 2ccd0857ec735ab88c6f38ba4124ae2b3bf67c2c Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 27 Aug 2019 23:46:24 -0400 Subject: [PATCH] Merge pull request #15140 from dstansby/mask-fmt FIX: ScalarFormatter formatting of masked values --- lib/matplotlib/tests/test_image.py | 15 +++++++++++++++ lib/matplotlib/ticker.py | 2 ++ 2 files changed, 17 insertions(+) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index ae803000ddee..22cd78d46820 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -989,3 +989,18 @@ def test_deprecation(): with pytest.warns(MatplotlibDeprecationWarning): # Enough arguments to pass "shape" positionally. obj.imshow(data, *[None] * 10) + + +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]' diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 823ae19692a4..d1aa0c7fd112 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