8000 Improve formatting of imshow() cursor data when a colorbar exists. · matplotlib/matplotlib@4dda553 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4dda553

Browse files
committed
Improve formatting of imshow() cursor data when a colorbar exists.
When a colorbar exists, use its formatter to format cursor data. For example, after ``` imshow([[10000, 10001]]); colorbar() ``` currently the cursor data on either pixel is rendered as 1e4, but after this patch it becomes 0.0+1e4 / 1.0+1e4, or 10000.0 / 10001.0 if `rcParams["axes.formatter.useoffset"]` is set to False. (Even though the version with the offset text may not be the most esthetic, it's clearly more informative than the current behavior...) It would be nice if this worked even for ScalarMappables that don't have a colorbar; this may include extracting the Formatter selection code out of the colorbar code into something generally applicable to ScalarMappables, or just generating a hidden colorbar "on-the-fly" if needed just for the purpose of getting its Formatter.
1 parent e08d7ba commit 4dda553

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Improved formatting of image values under cursor when a colorbar is present
2+
```````````````````````````````````````````````````````````````````````````
3+
4+
When a colorbar is present, its formatter is now used to format the image
5+
values under the mouse cursor in the status bar. For example, for an image
6+
displaying the values 10,000 and 10,001, the statusbar will now (using default
7+
settings) display the values as ``0.0+1e4`` and ``1.0+1e4`` (or ``10000.0``
8+
and ``10001.0`` if the offset-text is disabled on the colorbar), whereas both
9+
values were previously displayed as ``1e+04``.

lib/matplotlib/artist.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import OrderedDict, namedtuple
22
from functools import wraps
33
import inspect
4+
from numbers import Number
45
import re
56
import warnings
67

@@ -1159,8 +1160,8 @@ def format_cursor_data(self, data):
11591160
data[0]
11601161
except (TypeError, IndexError):
11611162
data = [data]
1162-
data_str = ', '.join('{:0.3g}'.format(item) for item in data if
1163-
isinstance(item, (np.floating, np.integer, int, float)))
1163+
data_str = ', '.join('{:0.3g}'.format(item) for item in data
1164+
if isinstance(item, Number))
11641165
return "[" + data_str + "]"
11651166

11661167
@property

lib/matplotlib/cbook/__init__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,17 @@ def local_over_kwdict(local_var, kwargs, *keys):
302302

303303

304304
def strip_math(s):
305-
"""remove latex formatting from mathtext"""
306-
remove = (r'\mathdefault', r'\rm', r'\cal', r'\tt', r'\it', '\\', '{', '}')
307-
s = s[1:-1]
308-
for r in remove:
309-
s = s.replace(r, '')
305+
"""
306+
Remove latex formatting from mathtext.
307+
308+
Only handles fully math and fully non-math strings.
309+
"""
310+
if len(s) >= 2 and s[0] == s[-1] == "$":
311+
s = s[1:-1]
312+
remove = [
313+
r'\mathdefault', r'\rm', r'\cal', r'\tt', r'\it', '\\', '{', '}']
314+
for r in remove:
315+
s = s.replace(r, '')
310316
return s
311317

312318

lib/matplotlib/image.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,15 @@ def get_cursor_data(self, event):
907907
else:
908908
return arr[i, j]
909909

910+
def format_cursor_data(self, data):
911+
if self.colorbar:
912+
return ("["
913+
+ cbook.strip_math(self.colorbar.formatter(data))
914+
+ cbook.strip_math(self.colorbar.formatter.get_offset())
915+
+ "]")
916+
else:
917+
return super().format_cursor_data(data)
918+
910919

911920
class NonUniformImage(AxesImage):
912921
def __init__(self, ax, *, interpolation='nearest', **kwargs):

lib/matplotlib/tests/test_image.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,24 @@ def test_cursor_data():
262262
assert im.get_cursor_data(event) is None
263263

264264

265+
def test_format_cursor_data():
266+
from matplotlib.backend_bases import MouseEvent
267+
268+
fig, ax = plt.subplots()
269+
im = ax.imshow([[10000, 10001]])
270+
271+
xdisp, ydisp = ax.transData.transform_point([0, 0])
272+
event = MouseEvent('motion_notify_event', fig.canvas, xdisp, ydisp)
273+
assert im.get_cursor_data(event) == 10000
274+
assert im.format_cursor_data(im.get_cursor_data(event)) == "[1e+04]"
275+
276+
fig.colorbar(im)
277+
fig.canvas.draw() # This is necessary to set up the colorbar formatter.
278+
279+
assert im.get_cursor_data(event) == 10000
280+
assert im.format_cursor_data(im.get_cursor_data(event)) == "[0.0+1e4]"
281+
282+
265283
@image_comparison(baseline_images=['image_clip'], style='mpl20')
266284
def test_image_clip():
267285
d = [[1, 2], [3, 4]]

0 commit comments

Comments
 (0)
0