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

Skip to content

Commit 2517eb4

Browse files
committed
Simplify -- use nbins == None to indicate auto
1 parent 616db8f commit 2517eb4

File tree

4 files changed

+17
-75
lines changed

4 files changed

+17
-75
lines changed

examples/units/basic_units.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import matplotlib.ticker as ticker
77
from matplotlib.axes import Axes
88
from matplotlib.cbook import iterable
9-
from matplotlib import rcParams
109

1110

1211
class ProxyDelegate(object):
@@ -329,12 +328,8 @@ def axisinfo(unit, axis):
329328
label=unit.fullname,
330329
)
331330
elif unit == degrees:
332-
if rcParams['_internal.classic_mode']:
333-
locator = ticker.ClassicAutoLocator()
334-
else:
335-
locator = ticker.AutoLocator()
336331
return units.AxisInfo(
337-
majloc=locator,
332+
majloc=ticker.AutoLocator(),
338333
majfmt=ticker.FormatStrFormatter(r'$%i^\circ$'),
339334
label=unit.fullname,
340335
)

lib/matplotlib/axis.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,7 @@ def get_children(self):
711711

712712
def cla(self):
713713
'clear the current axis'
714-
if rcParams['_internal.classic_mode']:
715-
self.set_major_locator(mticker.ClassicAutoLocator())
716-
else:
717-
self.set_major_locator(mticker.AutoLocator())
714+
self.set_major_locator(mticker.AutoLocator())
718715
self.set_major_formatter(mticker.ScalarFormatter())
719716
self.set_minor_locator(mticker.NullLocator())
720717
self.set_minor_formatter(mticker.NullFormatter())
@@ -2006,7 +2003,7 @@ def get_tick_space(self):
20062003
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
20072004
tick = self._get_tick(True)
20082005
# There is a heuristic here that the aspect ratio of tick text
2009-
# is no more than 2:1
2006+
# is no more than 3:1
20102007
size = tick.label1.get_size() * 3
20112008
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
20122009
self._tick_space = np.floor(length / size)

lib/matplotlib/scale.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import numpy as np
77
from numpy import ma
88

9-
from matplotlib import rcParams
109
from matplotlib.cbook import dedent
1110
from matplotlib.ticker import (NullFormatter, ScalarFormatter,
1211
LogFormatterMathtext, LogitFormatter)
1312
from matplotlib.ticker import (NullLocator, LogLocator, AutoLocator,
14-
SymmetricalLogLocator, LogitLocator,
15-
ClassicAutoLocator)
13+
SymmetricalLogLocator, LogitLocator)
1614
from matplotlib.transforms import Transform, IdentityTransform
1715
from matplotlib import docstring
1816

@@ -73,11 +71,7 @@ def set_default_locators_and_formatters(self, axis):
7371
Set the locators and formatters to reasonable defaults for
7472
linear scaling.
7573
"""
76-
if rcParams['_internal.classic_mode']:
77-
locator = ClassicAutoLocator()
78-
else:
79-
locator = AutoLocator()
80-
axis.set_major_locator(locator)
74+
axis.set_major_locator(AutoLocator())
8175
axis.set_major_formatter(ScalarFormatter())
8276
axis.set_minor_locator(NullLocator())
8377
axis.set_minor_formatter(NullFormatter())

lib/matplotlib/ticker.py

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ class MaxNLocator(Locator):
13331333
"""
13341334
Select no more than N intervals at nice locations.
13351335
"""
1336-
default_params = dict(nbins=10,
1336+
default_params = dict(nbins=None,
13371337
steps=None,
13381338
trim=True,
13391339
integer=False,
@@ -1345,7 +1345,9 @@ def __init__(self, *args, **kwargs):
13451345
Keyword args:
13461346
13471347
*nbins*
1348-
Maximum number of intervals; one less than max number of ticks.
1348+
Maximum number of intervals; one less than max number of
1349+
ticks. If `None`, the number of bins will be
1350+
automatically determined based on the length of the axis.
13491351
13501352
*steps*
13511353
Sequence of nice numbers starting with 1 and ending with 10;
@@ -1412,6 +1414,8 @@ def set_params(self, **kwargs):
14121414

14131415
def bin_boundaries(self, vmin, vmax):
14141416
nbins = self._nbins
1417+
if nbins is None:
1418+
nbins = self.axis.get_tick_space()
14151419
scale, offset = scale_range(vmin, vmax, nbins)
14161420
if self._integer:
14171421
scale = max(1, scale)
@@ -1462,52 +1466,6 @@ def view_limits(self, dmin, dmax):
14621466
return np.take(self.bin_boundaries(dmin, dmax), [0, -1])
14631467

14641468

1465-
class AutoSpacedLocator(MaxNLocator):
1466-
"""
1467-
Behaves like a MaxNLocator, except N is automatically determined
1468-
from the length of the axis.
1469-
"""
1470-
def __init__(self, steps=None, integer=False, symmetric=False, prune=None):
1471-
"""
1472-
Keyword args:
1473-
1474-
*steps*
1475-
Sequence of nice numbers starting with 1 and ending with 10;
1476-
e.g., [1, 2, 4, 5, 10]
1477-
1478-
*integer*
1479-
If True, ticks will take only integer values.
1480-
1481-
*symmetric*
1482-
If True, autoscaling will result in a range symmetric
1483-
about zero.
1484-
1485-
*prune*
1486-
['lower' | 'upper' | 'both' | None]
1487-
Remove edge ticks -- useful for stacked or ganged plots
1488-
where the upper tick of one axes overlaps with the lower
1489-
tick of the axes above it.
1490-
If prune=='lower', the smallest tick will
1491-
be removed. If prune=='upper', the largest tick will be
1492-
removed. If prune=='both', the largest and smallest ticks
1493-
will be removed. If prune==None, no ticks will be removed.
1494-
1495-
"""
1496-
self.set_params(**self.default_params)
1497-
self.set_params(steps=steps, integer=integer, symmetric=symmetric,
1498-
prune=prune)
1499-
1500-
def set_params(self, **kwargs):
1501-
if 'nbins' in kwargs:
1502-
raise TypeError(
1503-
"set_params got an unexpected keyword argument 'nbins'")
1504-
return super(AutoSpacedLocator, self).set_params(**kwargs)
1505-
1506-
def __call__(self):
1507-
self._nbins = self.axis.get_tick_space()
1508-
return super(AutoSpacedLocator, self).__call__()
1509-
1510-
15111469
def decade_down(x, base=10):
15121470
'floor x to the nearest lower decade'
15131471
if x == 0.0:
@@ -1931,15 +1889,13 @@ def tick_values(self, vmin, vmax):
19311889
return self.raise_if_exceeds(np.array(ticklocs))
19321890

19331891

1934-
class AutoLocator(AutoSpacedLocator):
1892+
class AutoLocator(MaxNLocator):
19351893
def __init__(self):
1936-
AutoSpacedLocator.__init__(self, steps=[1, 2, 5, 10])
1937-
1938-
1939-
class ClassicAutoLocator(MaxNLocator):
1940-
# Used only for classic style
1941-
def __init__(self):
1942-
MaxNLocator.__init__(self, nbins=9, steps=[1, 2, 5, 10])
1894+
if rcParams['_internal.classic_mode']:
1895+
nbins = 9
1896+
else:
1897+
nbins = None
1898+
MaxNLocator.__init__(self, nbins=nbins, steps=[1, 2, 5, 10])
19431899

19441900

19451901
class AutoMinorLocator(Locator):

0 commit comments

Comments
 (0)