8000 Merge pull request #28649 from tacaswell/fix/singular_norm_format_coords · matplotlib/matplotlib@109ab93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 109ab93

Browse files
authored
Merge pull request #28649 from tacaswell/fix/singular_norm_format_coords
FIX: improve formatting of image values in cases of singular norms
2 parents b622fbc + 16827b8 commit 109ab93

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

lib/matplotlib/artist.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,9 @@ def format_cursor_data(self, data):
13651365
delta = np.diff(
13661366
self.norm.boundaries[neigh_idx:cur_idx + 2]
13671367
).max()
1368-
1368+
elif self.norm.vmin == self.norm.vmax:
1369+
# singular norms, use delta of 10% of only value
1370+
delta = np.abs(self.norm.vmin * .1)
13691371
else:
13701372
# Midpoints of neighboring color intervals.
13711373
neighbors = self.norm.inverse(

lib/matplotlib/cbook.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,10 @@ def _g_sig_digits(value, delta):
22522252
it is known with an error of *delta*.
22532253
"""
22542254
if delta == 0:
2255+
if value == 0:
2256+
# if both value and delta are 0, np.spacing below returns 5e-324
2257+
# which results in rather silly results
2258+
return 3
22552259
# delta = 0 may occur when trying to format values over a tiny range;
22562260
# in that case, replace it by the distance to the closest float.
22572261
delta = abs(np.spacing(value))

lib/matplotlib/tests/test_image.py

Lines changed: 2 additions & 1 deletion
< 7673 /tr>
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ def test_cursor_data_nonuniform(xy, data):
390390
([[.123, .987]], "[0.123]"),
391391
([[np.nan, 1, 2]], "[]"),
392392
([[1, 1+1e-15]], "[1.0000000000000000]"),
393-
([[-1, -1]], "[-1.0000000000000000]"),
393+
([[-1, -1]], "[-1.0]"),
394+
([[0, 0]], "[0.00]"),
394395
])
395396
def test_format_cursor_data(data, text):
396397
from matplotlib.backend_bases import MouseEvent

0 commit comments

Comments
 (0)
0