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

Skip to content

Commit 13c6d88

Browse files
authored
Merge pull request #12459 from anntzer/imshow-cursordata
Improve formatting of imshow() cursor data when a colorbar exists.
2 parents 135bab0 + 3b37b08 commit 13c6d88

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-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: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,25 @@ 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+
for tex, plain in [
313+
(r"\times", "x"), # Specifically for Formatter support.
314+
(r"\mathdefault", ""),
315+
(r"\rm", ""),
316+
(r"\cal", ""),
317+
(r"\tt", ""),
318+
(r"\it", ""),
319+
("\\", ""),
320+
("{", ""),
321+
("}", ""),
322+
]:
323+
s = s.replace(tex, plain)
310324
return s
311325

312326

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)