Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97bec72

Browse files
anntzerStephen-Chilcote
authored andcommitted
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 6b9ec8b commit 97bec72

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
@@ -2,6 +2,7 @@
22
from functools import wraps
33
import inspect
44
import logging
5+
from numbers import Number
56
import re
67
import warnings
78

@@ -1165,8 +1166,8 @@ def format_cursor_data(self, data):
11651166
data[0]
11661167
except (TypeError, IndexError):
11671168
data = [data]
1168-
data_str = ', '.join('{:0.3g}'.format(item) for item in data if
1169-
isinstance(item, (np.floating, np.integer, int, float)))
1169+
data_str = ', '.join('{:0.3g}'.format(item) for item in data
1170+
if isinstance(item, Number))
11701171
return "[" + data_str + "]"
11711172

11721173
@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
@@ -906,6 +906,15 @@ def get_cursor_data(self, event):
906906
else:
907907
return arr[i, j]
908908

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

910919
class NonUniformImage(AxesImage):
911920
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)