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

Skip to content

Commit 02a0455

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 f93222a commit 02a0455

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

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 s[:1] == 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,13 @@ 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 (cbook.strip_math(self.colorbar.formatter(data))
913+
+ cbook.strip_math(self.colorbar.formatter.get_offset()))
914+
else:
915+
return super().format_cursor_data(data)
916+
910917

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

0 commit comments

Comments
 (0)