Skip to content
99 changes: 97 additions & 2 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import matplotlib.ticker as mticker
from matplotlib.testing.decorators import cleanup

import warnings


def test_MaxNLocator():
loc = mticker.MaxNLocator(nbins=5)
Expand Down Expand Up @@ -50,7 +52,6 @@ def test_AutoMinorLocator():

def test_LogLocator():
loc = mticker.LogLocator(numticks=5)

assert_raises(ValueError, loc.tick_values, 0, 1000)

test_value = np.array([1.00000000e-05, 1.00000000e-03, 1.00000000e-01,
Expand All @@ -63,6 +64,101 @@ def test_LogLocator():
assert_almost_equal(loc.tick_values(1, 100), test_value)


def test_LinearLocator_set_params():
"""
Create linear locator with presets={}, numticks=2 and change it to
something else. See if change was successful. Should not exception.
"""
loc = mticker.LinearLocator(numticks=2)
loc.set_params(numticks=8, presets={(0, 1): []})
nose.tools.assert_equal(loc.numticks, 8)
nose.tools.assert_equal(loc.presets, {(0, 1): []})


def test_LogLocator_set_params():
"""
Create log locator with default value, base=10.0, subs=[1.0], numdecs=4,
numticks=15 and change it to something else.
See if change was successful.
Should not exception.
"""
loc = mticker.LogLocator()
loc.set_params(numticks=8, numdecs=8, subs=[2.0], base=8)
nose.tools.assert_equal(loc.numticks, 8)
nose.tools.assert_equal(loc.numdecs, 8)
nose.tools.assert_equal(loc.base, 8)
nose.tools.assert_equal(loc.subs, [2.0])


def test_NullLocator_set_params():
"""
Create null locator, and attempt to call set_params() on it.
Should not exception, and should raise a warning.
"""
loc = mticker.NullLocator()
with warnings.catch_warnings(record=True) as w:
loc.set_params()
nose.tools.assert_equal(len(w), 1)


def test_MultipleLocator_set_params():
"""
Create multiple locator with 0.7 base, and change it to something else.
See if change was successful.
Should not exception.
"""
mult = mticker.MultipleLocator(base=0.7)
mult.set_params(base=1.7)
nose.tools.assert_equal(mult._base, 1.7)


def test_LogitLocator_set_params():
"""
Create logit locator with default minor=False, and change it to something
else. See if change was successful. Should not exception.
"""
loc = mticker.LogitLocator() # Defaults to false.
loc.set_params(minor=True)
nose.tools.assert_true(loc.minor)


def test_FixedLocator_set_params():
"""
Create fixed locator with 5 nbins, and change it to something else.
See if change was successful.
Should not exception.
"""
fixed = mticker.FixedLocator(range(0, 24), nbins=5)
fixed.set_params(nbins=7)
nose.tools.assert_equal(fixed.nbins, 7)


def test_IndexLocator_set_params():
"""
Create index locator with 3 base, 4 offset. and change it to something
else. See if change was successful.
Should not exception.
"""
index = mticker.IndexLocator(base=3, offset=4)
index.set_params(base=7, offset=7)
nose.tools.assert_equal(index._base, 7)
nose.tools.assert_equal(index.offset, 7)


def test_SymmetricalLogLocator_set_params():
"""
Create symmetrical log locator with default subs =[1.0] numticks = 15,
and change it to something else.
See if change was successful.
Should not exception.
"""
# since we only test for the params change. I will pass empty transform
sym = mticker.SymmetricalLogLocator(None)
sym.set_params(subs=[2.0], numticks=8)
nose.tools.assert_equal(sym._subs, [2.0])
nose.tools.assert_equal(sym.numticks, 8)


def test_LogFormatterExponent():
class FakeAxis(object):
"""Allow Formatter to be called without having a "full" plot set up."""
Expand Down Expand Up @@ -111,7 +207,6 @@ def test_formatstrformatter():
tmp_form = mticker.StrMethodFormatter('{x:05d}')
nose.tools.assert_equal('00002', tmp_form(2))


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
58 changes: 58 additions & 0 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@
from matplotlib import cbook
from matplotlib import transforms as mtransforms

import warnings

if six.PY3:
long = int

Expand Down Expand Up @@ -953,6 +955,14 @@ def tick_values(self, vmin, vmax):
"""
raise NotImplementedError('Derived must override')

def set_params(self, **kwargs):
"""
Do nothing, and rase a warning. Any locator class not supporting the
set_params() function will call this.
"""
warnings.warn("'set_params()' not defined for locator of type " +
str(type(self)))

def __call__(self):
"""Return the locations of the ticks"""
# note: some locators return data limits, other return view limits,
Expand Down Expand Up @@ -1025,6 +1035,13 @@ def __init__(self, base, offset):
self._base = base
self.offset = offset

def set_params(self, base=None, offset=None):
"""Set parameters within this locator"""
if base is not None:
self._base = base
if offset is not None:
self.offset = offset

def __call__(self):
"""Return the locations of the ticks"""
dmin, dmax = self.axis.get_data_interval()
Expand Down Expand Up @@ -1052,6 +1069,11 @@ def __init__(self, locs, nbins=None):
if self.nbins is not None:
self.nbins = max(self.nbins, 2)

def set_params(self, nbins=None):
"""Set parameters within this locator."""
if nbins is not None:
self.nbins = nbins

def __call__(self):
return self.tick_values(None, None)

Expand Down Expand Up @@ -1116,6 +1138,13 @@ def __init__(self, numticks=None, presets=None):
else:
self.presets = presets

def set_params(self, numticks=None, presets=None):
"""Set parameters within this locator."""
if presets is not None:
self.presets = presets
if numticks is not None:
self.numticks = numticks

def __call__(self):
'Return the locations of the ticks'
vmin, vmax = self.axis.get_view_interval()
Expand Down Expand Up @@ -1218,6 +1247,11 @@ class MultipleLocator(Locator):
def __init__(self, base=1.0):
self._base = Base(base)

def set_params(self, base):
"""Set parameters within this locator."""
if base is not None:
self._base = base

def __call__(self):
'Return the locations of the ticks'
vmin, vmax = self.axis.get_view_interval()
Expand Down Expand Up @@ -1319,6 +1353,7 @@ def __init__(self, *args, **kwargs):
self.set_params(**kwargs)

def set_params(self, **kwargs):
"""Set parameters within this locator."""
if 'nbins' in kwargs:
self._nbins = int(kwargs['nbins'])
if 'trim' in kwargs:
Expand Down Expand Up @@ -1453,6 +1488,17 @@ def __init__(self, base=10.0, subs=[1.0], numdecs=4, numticks=15):
self.numticks = numticks
self.numdecs = numdecs

def set_params(self, base=None, subs=None, numdecs=None, numticks=None):
"""Set parameters within this locator."""
if base is not None:
self.base = base
if subs is not None:
self.subs = subs
if numdecs is not None:
self.numdecs = numdecs
if numticks is not None:
self.numticks = numticks

def base(self, base):
"""
set the base of the log scaling (major tick every base**i, i integer)
Expand Down Expand Up @@ -1580,6 +1626,13 @@ def __init__(self, transform, subs=None):
self._subs = subs
self.numticks = 15

def set_params(self, subs=None, numticks=None):
"""Set parameters within this locator."""
if numticks is not None:
self.numticks = numticks
if subs is not None:
self._subs = subs

def __call__(self):
'Return the locations of the ticks'
# Note, these are untransformed coordinates
Expand Down Expand Up @@ -1728,6 +1781,11 @@ def __init__(self, minor=False):
"""
self.minor = minor

def set_params(self, minor=None):
"""Set parameters within this locator."""
if minor is not None:
self.minor = minor

def __call__(self):
'Return the locations of the ticks'
vmin, vmax = self.axis.get_view_interval()
Expand Down