From 17420ec0cb105457f980edc655419dd79744c370 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 28 Apr 2019 21:04:35 +0200 Subject: [PATCH] Don't try to use the colorbar formatter to format RGBA data. A colorbar doesn't make much sense for RGBA data, but matplotlib does allow one to be constructed; when formatting the cursor data for a RGBA image we must be careful to not use the colorbar formatter. Otherwise, after `plt.imshow(np.random.rand(5, 5, 3)); plt.colorbar()`, when moving the mouse cursor over the image, we get ``` File "/usr/lib/python3.7/site-packages/matplotlib/ticker.py", line 604, in format_data_short return '%-12g' % value File "/usr/lib/python3.7/site-packages/numpy/ma/core.py", line 4296, in __float__ raise TypeError("Only length-1 arrays can be converted " TypeError: Only length-1 arrays can be converted to Python scalars ``` --- lib/matplotlib/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 03b651edafb8..73fd7e827f7e 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -930,7 +930,7 @@ def get_cursor_data(self, event): return arr[i, j] def format_cursor_data(self, data): - if self.colorbar: + if np.ndim(data) == 0 and self.colorbar: return ( "[" + cbook.strip_math(