8000 BUG: fix minpos handling and other log ticker problems · matplotlib/matplotlib@6f53d3d · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f53d3d

Browse files
committed
BUG: fix minpos handling and other log ticker problems
Closes #7595, #7493, #7587. Bbox._minpos is now initialized to [np.inf, np.inf] instead of [1e-7, 1e-7]. Old code with a colorbar in which the formatter is set to a LogFormatter and a SymlogNorm is used now works again (although the results are better in 2.0 if the colorbar is left to handle the formatter automatically). LogLocator now has its own nonsingular() method which provides a reasonable starting point for a log axis when no data have been plotted.
1 parent 1c1fd38 commit 6f53d3d

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

lib/matplotlib/scale.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
264264
"""
265265
Limit the domain to positive values.
266266
"""
267+
if not np.isfinite(minpos):
268+
minpos = 1e-300 # This value should rarely if ever
269+
# end up with a visible effect.
270+
267271
return (minpos if vmin <= 0 else vmin,
268272
minpos if vmax <= 0 else vmax)
269273

lib/matplotlib/ticker.py

Lines changed: 34 additions & 19 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,16 @@ def set_locs(self, locs=None):
906906
except AttributeError:
907907
pass
908908

909+
if vmin > vmax:
910+
vmin, vmax = vmax, vmin
911+
912+
if linthresh is None and vmin <= 0:
913+
# It's probably a colorbar with
914+
# a format kwarg setting a LogFormatter in the manner
915+
# that worked with 1.5.x, but that doesn't work now.
916+
self._sublabels = set((1,)) # label powers of base
917+
return
918+
909919
if linthresh is not None: # symlog
910920
# Only compute the number of decades in the logarithmic part of the
911921
# axis
@@ -938,7 +948,7 @@ def __call__(self, x, pos=None):
938948
Return the format for tick val `x`.
939949
"""
940950
b = self._base
941-
if x == 0.0:
951+
if x == 0.0: # Symlog
942952
return '0'
943953
sign = np.sign(x)
944954
x = abs(x)
@@ -1999,36 +2009,41 @@ def view_limits(self, vmin, vmax):
19992009
'Try to choose the view limits intelligently'
20002010
b = self._base
20012011

2002-
if vmax < vmin:
2003-
vmin, vmax = vmax, vmin
2012+
vmin, vmax = self.nonsingular(vmin, vmax)
20042013

20052014
if self.axis.axes.name == 'polar':
20062015
vmax = math.ceil(math.log(vmax) / math.log(b))
20072016
vmin = b ** (vmax - self.numdecs)
2008-
return vmin, vmax
2009-
2010-
minpos = self.axis.get_minpos()
2011-
2012-
if minpos <= 0 or not np.isfinite(minpos):
2013-
raise ValueError(
2014-
"Data has no positive values, and therefore can not be "
2015-
"log-scaled.")
2016-
2017-
if vmin <= 0:
2018-
vmin = minpos
20192017

20202018
if rcParams['axes.autolimit_mode'] == 'round_numbers':
20212019
if not is_decade(vmin, self._base):
20222020
vmin = decade_down(vmin, self._base)
20232021
if not is_decade(vmax, self._base):
20242022
vmax = decade_up(vmax, self._base)
20252023

2026-
if vmin == vmax:
2027-
vmin = decade_down(vmin, self._base)
2028-
vmax = decade_up(vmax, self._base)
2024+
return vmin, vmax
20292025

2030-
result = mtransforms.nonsingular(vmin, vmax)
2031-
return result
2026+
def nonsingular(self, vmin, vmax):
2027+
if not np.isfinite(vmin) or not np.isfinite(vmax):
2028+
return 1, 100 # initial range, no data plotted yet
2029+
2030+
if vmin > vmax:
2031+
vmin, vmax = vmax, vmin
2032+
if vmax <= 0:
2033+
warnings.warn(
2034+
"Data has no positive values, and therefore cannot be "
2035+
"log-scaled.")
2036+
return 1, 100
2037+
2038+
minpos = self.axis.get_minpos()
2039+
if not np.isfinite(minpos):
2040+
minpos = 1e-300 # This should never take effect.
2041+
if vmin <= 0:
2042+
vmin = minpos
2043+
if vmin == vmax:
2044+
vmin = decade_down(vmin, self._base)
2045+
vmax = decade_up(vmax, self._base)
2046+
return vmin, vmax
20322047

20332048

20342049
class SymmetricalLogLocator(Locator):

lib/matplotlib/transforms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ def __init__(self, points, **kwargs):
791791
raise ValueError('Bbox points must be of the form '
792792
'"[[x0, y0], [x1, y1]]".')
793793
self._points = points
794-
self._minpos = np.array([0.0000001, 0.0000001])
794+
self._minpos = np.array([np.inf, np.inf])
795795
self._ignore = True
796796
# it is helpful in some contexts to know if the bbox is a
797797
# default or has been mutated; we store the orig points to

0 commit comments

Comments
 (0)
0