Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 13c6d88

Browse filesBrowse 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
Copy full SHA for 13c6d88

File tree

5 files changed

+58
-7
lines changed
Filter options

5 files changed

+58
-7
lines changed
+9Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/artist.py
+3-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/cbook/__init__.py
+19-5Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/image.py
+9Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+18Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.