From 6c92776cbd3cf32b721c86a037deee3f813b030f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 31 Oct 2024 18:07:39 +0100 Subject: [PATCH] Factor out common formats strings in LogFormatter, LogFormatterExponent. The x < 1 and x > 10000 branches can come together. --- lib/matplotlib/ticker.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 0053031ece3e..25875b04d585 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -987,13 +987,7 @@ def set_locs(self, locs=None): self._sublabels = set(np.arange(1, b + 1)) def _num_to_string(self, x, vmin, vmax): - if x > 10000: - s = '%1.0e' % x - elif x < 1: - s = '%1.0e' % x - else: - s = self._pprint_val(x, vmax - vmin) - return s + return self._pprint_val(x, vmax - vmin) if 1 <= x <= 10000 else f"{x:1.0e}" def __call__(self, x, pos=None): # docstring inherited @@ -1053,15 +1047,14 @@ class LogFormatterExponent(LogFormatter): """ Format values for log axis using ``exponent = log_base(value)``. """ + def _num_to_string(self, x, vmin, vmax): fx = math.log(x) / math.log(self._base) - if abs(fx) > 10000: - s = '%1.0g' % fx - elif abs(fx) < 1: - s = '%1.0g' % fx - else: + if 1 <= abs(fx) <= 10000: fd = math.log(vmax - vmin) / math.log(self._base) s = self._pprint_val(fx, fd) + else: + s = f"{fx:1.0g}" return s