From 16827b82f0a2b9f71dfbebf3e8895f13a96e5c98 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Fri, 2 Aug 2024 12:27:11 -0400 Subject: [PATCH] FIX: improve formatting of image values in cases of singular norms closes #28648 --- lib/matplotlib/artist.py | 4 +++- lib/matplotlib/cbook.py | 4 ++++ lib/matplotlib/tests/test_image.py | 3 ++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/artist.py b/lib/matplotlib/artist.py index 981365d852be..24aaa349ee05 100644 --- a/lib/matplotlib/artist.py +++ b/lib/matplotlib/artist.py @@ -1365,7 +1365,9 @@ def format_cursor_data(self, data): delta = np.diff( self.norm.boundaries[neigh_idx:cur_idx + 2] ).max() - + elif self.norm.vmin == self.norm.vmax: + # singular norms, use delta of 10% of only value + delta = np.abs(self.norm.vmin * .1) else: # Midpoints of neighboring color intervals. neighbors = self.norm.inverse( diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index e4f60aac37a8..2411784af3ec 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -2252,6 +2252,10 @@ def _g_sig_digits(value, delta): it is known with an error of *delta*. """ if delta == 0: + if value == 0: + # if both value and delta are 0, np.spacing below returns 5e-324 + # which results in rather silly results + return 3 # delta = 0 may occur when trying to format values over a tiny range; # in that case, replace it by the distance to the closest float. delta = abs(np.spacing(value)) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index dfacfccb3e0e..6de1754f9ed7 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -390,7 +390,8 @@ def test_cursor_data_nonuniform(xy, data): ([[.123, .987]], "[0.123]"), ([[np.nan, 1, 2]], "[]"), ([[1, 1+1e-15]], "[1.0000000000000000]"), - ([[-1, -1]], "[-1.0000000000000000]"), + ([[-1, -1]], "[-1.0]"), + ([[0, 0]], "[0.00]"), ]) def test_format_cursor_data(data, text): from matplotlib.backend_bases import MouseEvent 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