Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a40d22b

Browse files
authored
Merge pull request #26609 from anntzer/aml
Cleanup AutoMinorLocator implementation.
2 parents a04f0bf + 66c02bc commit a40d22b

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

lib/matplotlib/ticker.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,7 @@ class AutoMinorLocator(Locator):
28932893
Dynamically find minor tick positions based on the positions of
28942894
major ticks. The scale must be linear with major ticks evenly spaced.
28952895
"""
2896+
28962897
def __init__(self, n=None):
28972898
"""
28982899
*n* is the number of subdivisions of the interval between
@@ -2908,47 +2909,33 @@ def __init__(self, n=None):
29082909
self.ndivs = n
29092910

29102911
def __call__(self):
2911-
"""Return the locations of the ticks."""
2912+
# docstring inherited
29122913
if self.axis.get_scale() == 'log':
2913-
_api.warn_external('AutoMinorLocator does not work with '
2914-
'logarithmic scale')
2914+
_api.warn_external('AutoMinorLocator does not work on logarithmic scales')
29152915
return []
29162916

29172917
majorlocs = self.axis.get_majorticklocs()
2918-
try:
2919-
majorstep = majorlocs[1] - majorlocs[0]
2920-
except IndexError:
2921-
# Need at least two major ticks to find minor tick locations
2922-
# TODO: Figure out a way to still be able to display minor
2923-
# ticks without two major ticks visible. For now, just display
2924-
# no ticks at all.
2918+
if len(majorlocs) < 2:
2919+
# Need at least two major ticks to find minor tick locations.
2920+
# TODO: Figure out a way to still be able to display minor ticks with less
2921+
# than two major ticks visible. For now, just display no ticks at all.
29252922
return []
2923+
majorstep = majorlocs[1] - majorlocs[0]
29262924

29272925
if self.ndivs is None:
2928-
2929-
if self.axis.axis_name == 'y':
2930-
self.ndivs = mpl.rcParams['ytick.minor.ndivs']
2931-
else:
2932-
# for x and z axis
2933-
self.ndivs = mpl.rcParams['xtick.minor.ndivs']
2926+
self.ndivs = mpl.rcParams[
2927+
'ytick.minor.ndivs' if self.axis.axis_name == 'y'
2928+
else 'xtick.minor.ndivs'] # for x and z axis
29342929

29352930
if self.ndivs == 'auto':
2936-
2937-
majorstep_no_exponent = 10 ** (np.log10(majorstep) % 1)
2938-
2939-
if np.isclose(majorstep_no_exponent, [1.0, 2.5, 5.0, 10.0]).any():
2940-
ndivs = 5
2941-
else:
2942-
ndivs = 4
2931+
majorstep_mantissa = 10 ** (np.log10(majorstep) % 1)
2932+
ndivs = 5 if np.isclose(majorstep_mantissa, [1, 2.5, 5, 10]).any() else 4
29432933
else:
29442934
ndivs = self.ndivs
29452935

29462936
minorstep = majorstep / ndivs
29472937

2948-
vmin, vmax = self.axis.get_view_interval()
2949-
if vmin > vmax:
2950-
vmin, vmax = vmax, vmin
2951-
2938+
vmin, vmax = sorted(self.axis.get_view_interval())
29522939
t0 = majorlocs[0]
29532940
tmin = round((vmin - t0) / minorstep)
29542941
tmax = round((vmax - t0) / minorstep) + 1
@@ -2957,5 +2944,5 @@ def __call__(self):
29572944
return self.raise_if_exceeds(locs)
29582945

29592946
def tick_values(self, vmin, vmax):
2960-
raise NotImplementedError('Cannot get tick locations for a '
2961-
'%s type.' % type(self))
2947+
raise NotImplementedError(
2948+
f"Cannot get tick locations for a {type(self).__name__}")

0 commit comments

Comments
 (0)