Skip to content

Commit 89db684

Browse files
committed
Tame that obscure MaxNLocator: change parameters after instantiation.
svn path=/trunk/matplotlib/; revision=8242
1 parent c20aee9 commit 89db684

File tree

5 files changed

+110
-21
lines changed

5 files changed

+110
-21
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2010-04-18 Control MaxNLocator parameters after instantiation,
2+
and via Axes.locator_params method, with corresponding
3+
pyplot function. -EF
4+
15
2010-04-18 Control ScalarFormatter offsets directly and via the
26
Axes.ticklabel_format() method, and add that to pyplot. -EF
37

boilerplate.py

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def %(func)s(%(argspec)s):
102102
'text',
103103
'annotate',
104104
'ticklabel_format',
105+
'locator_params',
105106
)
106107

107108
cmappable = {

lib/matplotlib/axes.py

+39
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,45 @@ def ticklabel_format(self, **kwargs):
19381938
raise AttributeError(
19391939
"This method only works with the ScalarFormatter.")
19401940

1941+
def locator_params(self, axis='both', tight=False, **kwargs):
1942+
"""
1943+
Convenience method for controlling tick locators.
1944+
1945+
Keyword arguments:
1946+
1947+
*axis*
1948+
['x' | 'y' | 'both'] Axis on which to operate;
1949+
default is 'both'.
1950+
1951+
*tight*
1952+
[True | False] Parameter passed to :meth:`autoscale_view`.
1953+
1954+
Remaining keyword arguments are passed to directly to the
1955+
:meth:`~matplotlib.ticker.MaxNLocator.set_params` method.
1956+
1957+
Typically one might want to reduce the maximum number
1958+
of ticks and use tight bounds when plotting small
1959+
subplots, for example::
1960+
1961+
ax.set_locator_params(tight=True, nbins=4)
1962+
1963+
Because the locator is involved in autoscaling,
1964+
:meth:`autoscale_view` is called automatically after
1965+
the parameters are changed.
1966+
1967+
This presently works only for the
1968+
:class:`~matplotlib.ticker.MaxNLocator` used
1969+
by default on linear axes, but it may be generalized.
1970+
"""
1971+
_x = axis in ['x', 'both']
1972+
_y = axis in ['y', 'both']
1973+
if _x:
1974+
self.xaxis.get_major_locator().set_params(**kwargs)
1975+
if _y:
1976+
self.yaxis.get_major_locator().set_params(**kwargs)
1977+
self.autoscale_view(tight=tight, scalex=_x, scaley=_y)
1978+
1979+
19411980
def set_axis_off(self):
19421981
"""turn off the axis"""
19431982
self.axison = False

lib/matplotlib/pyplot.py

+8
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,14 @@ def ticklabel_format(**kwargs):
25352535
draw_if_interactive()
25362536
return ret
25372537

2538+
# This function was autogenerated by boilerplate.py. Do not edit as
2539+
# changes will be lost
2540+
@docstring.copy_dedent(Axes.locator_params)
2541+
def locator_params(axis='both', tight=False, **kwargs):
2542+
ret = gca().locator_params(axis, tight, **kwargs)
2543+
draw_if_interactive()
2544+
return ret
2545+
25382546
# This function was autogenerated by boilerplate.py. Do not edit as
25392547
# changes will be lost
25402548
def autumn():

lib/matplotlib/ticker.py

+58-21
Original file line numberDiff line numberDiff line change
@@ -1052,37 +1052,74 @@ class MaxNLocator(Locator):
10521052
"""
10531053
Select no more than N intervals at nice locations.
10541054
"""
1055-
1056-
def __init__(self, nbins = 10, steps = None,
1057-
trim = True,
1058-
integer=False,
1059-
symmetric=False,
1060-
prune=None):
1055+
default_params = dict(nbins = 10,
1056+
steps = None,
1057+
trim = True,
1058+
integer=False,
1059+
symmetric=False,
1060+
prune=None)
1061+
def __init__(self, **kwargs):
10611062
"""
10621063
Keyword args:
1064+
1065+
*nbins*
1066+
Maximum number of intervals; one less than max number of ticks.
1067+
1068+
*steps*
1069+
Sequence of nice numbers starting with 1 and ending with 10;
1070+
e.g., [1, 2, 4, 5, 10]
1071+
1072+
*integer*
1073+
If True, ticks will take only integer values.
1074+
1075+
*symmetric*
1076+
If True, autoscaling will result in a range symmetric
1077+
about zero.
1078+
10631079
*prune*
1080+
['lower' | 'upper' | 'both' | None]
10641081
Remove edge ticks -- useful for stacked or ganged plots
10651082
where the upper tick of one axes overlaps with the lower
1066-
tick of the axes above it. One of 'lower' | 'upper' |
1067-
'both' | None. If prune=='lower', the smallest tick will
1083+
tick of the axes above it.
1084+
If prune=='lower', the smallest tick will
10681085
be removed. If prune=='upper', the largest tick will be
10691086
removed. If prune=='both', the largest and smallest ticks
10701087
will be removed. If prune==None, no ticks will be removed.
10711088
10721089
"""
1073-
self._nbins = int(nbins)
1074-
self._trim = trim
1075-
self._integer = integer
1076-
self._symmetric = symmetric
1077-
self._prune = prune
1078-
if steps is None:
1079-
self._steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
1080-
else:
1081-
if int(steps[-1]) != 10:
1082-
steps = list(steps)
1083-
steps.append(10)
1084-
self._steps = steps
1085-
if integer:
1090+
# I left "trim" out; it defaults to True, and it is not
1091+
# clear that there is any use case for False, so we may
1092+
# want to remove that kwarg. EF 2010/04/18
1093+
self.set_params(**self.default_params)
1094+
self.set_params(**kwargs)
1095+
1096+
def set_params(self, **kwargs):
1097+
if 'nbins' in kwargs:
1098+
self._nbins = int(kwargs['nbins'])
1099+
if 'trim' in kwargs:
1100+
self._trim = kwargs['trim']
1101+
if 'integer' in kwargs:
1102+
self._integer = kwargs['integer']
1103+
if 'symmetric' in kwargs:
1104+
self._symmetric = kwargs['symmetric']
1105+
if 'prune' in kwargs:
1106+
prune = kwargs['prune']
1107+
if prune is not None and prune not in ['upper', 'lower', 'both']:
1108+
raise ValueError(
1109+
"prune must be 'upper', 'lower', 'both', or None")
1110+
self._prune = prune
1111+
if 'steps' in kwargs:
1112+
steps = kwargs['steps']
1113+
if steps is None:
1114+
self._steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
1115+
else:
1116+
if int(steps[-1]) != 10:
1117+
steps = list(steps)
1118+
steps.append(10)
1119+
self._steps = steps
1120+
if 'integer' in kwargs:
1121+
self._integer = kwargs['integer']
1122+
if self._integer:
10861123
self._steps = [n for n in self._steps if divmod(n,1)[1] < 0.001]
10871124

10881125
def bin_boundaries(self, vmin, vmax):

0 commit comments

Comments
 (0)