8000 Adjust number of ticks based on length of axis by mdboom · Pull Request #5588 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Adjust number of ticks based on length of axis #5588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 14, 2015
Prev Previous commit
Next Next commit
Simplify -- use nbins == None to indicate auto
  • Loading branch information
mdboom committed Dec 10, 2015
commit 5a2f9620d1fc5fd1e27a1af26b8c0333c99327f9
7 changes: 1 addition & 6 deletions examples/units/basic_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import matplotlib.ticker as ticker
from matplotlib.axes import Axes
from matplotlib.cbook import iterable
from matplotlib import rcParams


class ProxyDelegate(object):
Expand Down Expand Up @@ -329,12 +328,8 @@ def axisinfo(unit, axis):
label=unit.fullname,
)
elif unit == degrees:
if rcParams['_internal.classic_mode']:
locator = ticker.ClassicAutoLocator()
else:
locator = ticker.AutoLocator()
return units.AxisInfo(
majloc=locator,
majloc=ticker.AutoLocator(),
majfmt=ticker.FormatStrFormatter(r'$%i^\circ$'),
label=unit.fullname,
)
Expand Down
7 changes: 2 additions & 5 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,7 @@ def get_children(self):

def cla(self):
'clear the current axis'
if rcParams['_internal.classic_mode']:
self.set_major_locator(mticker.ClassicAutoLocator())
else:
self.set_major_locator(mticker.AutoLocator())
self.set_major_locator(mticker.AutoLocator())
self.set_major_formatter(mticker.ScalarFormatter())
self.set_minor_locator(mticker.NullLocator())
self.set_minor_formatter(mticker.NullFormatter())
Expand Down Expand Up @@ -2006,7 +2003,7 @@ def get_tick_space(self):
length = ((ends[1][0] - ends[0][0]) / self.axes.figure.dpi) * 72.0
tick = self._get_tick(True)
# There is a heuristic here that the aspect ratio of tick text
# is no more than 2:1
# is no more than 3:1
size = tick.label1.get_size() * 3
size *= np.cos(np.deg2rad(tick.label1.get_rotation()))
self._tick_space = np.floor(length / size)
Expand Down
10 changes: 2 additions & 8 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
E 8000 xpand Up @@ -6,13 +6,11 @@
import numpy as np
from numpy import ma

from matplotlib import rcParams
from matplotlib.cbook import dedent
from matplotlib.ticker import (NullFormatter, ScalarFormatter,
LogFormatterMathtext, LogitFormatter)
from matplotlib.ticker import (NullLocator, LogLocator, AutoLocator,
SymmetricalLogLocator, LogitLocator,
ClassicAutoLocator)
SymmetricalLogLocator, LogitLocator)
from matplotlib.transforms import Transform, IdentityTransform
from matplotlib import docstring

Expand Down Expand Up @@ -73,11 +71,7 @@ def set_default_locators_and_formatters(self, axis):
Set the locators and formatters to reasonable defaults for
linear scaling.
"""
if rcParams['_internal.classic_mode']:
locator = ClassicAutoLocator()
else:
locator = AutoLocator()
axis.set_major_locator(locator)
axis.set_major_locator(AutoLocator())
axis.set_major_formatter(ScalarFormatter())
axis.set_minor_locator(NullLocator())
axis.set_minor_formatter(NullFormatter())
Expand Down
68 changes: 12 additions & 56 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ class MaxNLocator(Locator):
"""
Select no more than N intervals at nice locations.
"""
default_params = dict(nbins=10,
default_params = dict(nbins=None,
steps=None,
trim=True,
integer=False,
Expand All @@ -1349,7 +1349,9 @@ def __init__(self, *args, **kwargs):
Keyword args:

*nbins*
Maximum number of intervals; one less than max number of ticks.
Maximum number of intervals; one less than max number of
ticks. If `None`, the number of bins will be
automatically determined based on the length of the axis.

*steps*
Sequence of nice numbers starting with 1 and ending with 10;
Expand Down Expand Up @@ -1416,6 +1418,8 @@ def set_params(self, **kwargs):

def bin_boundaries(self, vmin, vmax):
nbins = self._nbins
if nbins is None:
nbins = self.axis.get_tick_space()
scale, offset = scale_range(vmin, vmax, nbins)
if self._integer:
scale = max(1, scale)
Expand Down Expand Up @@ -1472,52 +1476,6 @@ def view_limits(self, dmin, dmax):
return dmin, dmax


class AutoSpacedLocator(MaxNLocator):
"""
Behaves like a MaxNLocator, except N is automatically determined
from the length of the axis.
"""
def __init__(self, steps=None, integer=False, symmetric=False, prune=None):
"""
Keyword args:

*steps*
Sequence of nice numbers starting with 1 and ending with 10;
e.g., [1, 2, 4, 5, 10]

*integer*
If True, ticks will take only integer values.

*symmetric*
If True, autoscaling will result in a range symmetric
about zero.

*prune*
['lower' | 'upper' | 'both' | None]
Remove edge ticks -- useful for stacked or ganged plots
where the upper tick of one axes overlaps with the lower
tick of the axes above it.
If prune=='lower', the smallest tick will
be removed. If prune=='upper', the largest tick will be
removed. If prune=='both', the largest and smallest ticks
will be removed. If prune==None, no ticks will be removed.

"""
self.set_params(**self.default_params)
self.set_params(steps=steps, integer=integer, symmetric=symmetric,
prune=prune)

def set_params(self, **kwargs):
if 'nbins' in kwargs:
raise TypeError(
"set_params got an unexpected keyword argument 'nbins'")
return super(AutoSpacedLocator, self).set_params(**kwargs)

def __call__(self):
self._nbins = self.axis.get_tick_space()
return super(AutoSpacedLocator, self).__call__()


def decade_down(x, base=10):
'floor x to the nearest lower decade'
if x == 0.0:
Expand Down Expand Up @@ -1945,15 +1903,13 @@ def tick_values(self, vmin, vmax):
return self.raise_if_exceeds(np.array(ticklocs))


class AutoLocator(AutoSpacedLocator):
class AutoLocator(MaxNLocator):
def __init__(self):
AutoSpacedLocator.__init__(self, steps=[1, 2, 5, 10])


class ClassicAutoLocator(MaxNLocator):
# Used only for classic style
def __init__(self):
MaxNLocator.__init__(self, nbins=9, steps=[1, 2, 5, 10])
if rcParams['_internal.classic_mode']:
nbins = 9
else:
nbins = None
MaxNLocator.__init__(self, nbins=nbins, steps=[1, 2, 5, 10])


class AutoMinorLocator(Locator):
Expand Down
0