Skip to content

Commit e7aba70

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #28649: FIX: improve formatting of image values in cases of singular norms
1 parent 486e1e2 commit e7aba70

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
@@ -1346,7 +1346,9 @@ def format_cursor_data(self, data):
13461346
delta = np.diff(
13471347
self.norm.boundaries[neigh_idx:cur_idx + 2]
13481348
).max()
1349-
1349+
elif self.norm.vmin == self.norm.vmax:
1350+
# singular norms, use delta of 10% of only value
1351+
delta = np.abs(self.norm.vmin * .1)
13501352
else:
13511353
# Midpoints of neighboring color intervals.
13521354
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
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ def test_cursor_data_nonuniform(xy, data):
411411
([[.123, .987]], "[0.123]"),
412412
([[np.nan, 1, 2]], "[]"),
413413
([[1, 1+1e-15]], "[1.0000000000000000]"),
414-
([[-1, -1]], "[-1.0000000000000000]"),
414+
([[-1, -1]], "[-1.0]"),
415+
([[0, 0]], "[0.00]"),
415416
])
416417
def test_format_cursor_data(data, text):
417418
from matplotlib.backend_bases import MouseEvent

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy