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

Skip to content
Prev Previous commit
Next Next commit
last modify with testing and bug fix
  • Loading branch information
ryanbelt authored and leeonadoh committed Mar 3, 2015
commit fc207d4dc7e2b2485d7a3df7895b6d91b72385fc
95 changes: 76 additions & 19 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 @@ -63,6 +65,80 @@ def test_LogLocator():
assert_almost_equal(loc.tick_values(1, 100), test_value)


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(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_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,25 +187,6 @@ def test_formatstrformatter():
tmp_form = mticker.StrMethodFormatter('{x:05d}')
nose.tools.assert_equal('00002', tmp_form(2))

@cleanup
def test_set_params():
loc = mticker.LogLocator(numticks = 2)

assert_raises(ValueError, loc.tick_values, 0, 1000)
#this should return number of ticks as 5
test_value = np.array([ 1.0000000e-10, 1.0000000e-03, 1.0000000e+04, 1.0000000e+11,
1.0000000e+18])
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)
#after the set_params(numticks=8), it should have 7 extra which should have 7 extra
#numticks. Also it won't casue to thrown the exception for bugs # #3658
loc.set_params(numticks = 8)
test_value = np.array([ 1.0000000e-04, 1.0000000e-03, 1.0000000e-02, 1.0000000e-01,
1.0000000e+00, 1.0000000e+01, 1.0000000e+02, 1.0000000e+03,
1.0000000e+04, 1.0000000e+05, 1.0000000e+06, 1.0000000e+07])
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)



if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
11 changes: 11 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,9 @@ def tick_values(self, vmin, vmax):
"""
raise NotImplementedError('Derived must override')

def set_params(self, **kwargs):
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 @@ -1610,6 +1615,12 @@ def __init__(self, transform, subs=None):
self._subs = subs
self.numticks = 15

def set_params(self, **kwargs):
if 'numticks' in kwargs:
self.numticks = kwargs['numticks']
if 'subs' in kwargs:
self._subs = kwargs['subs']

def __call__(self):
'Return the locations of the ticks'
# Note, these are untransformed coordinates
Expand Down