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

Skip to content

Commit fc207d4

Browse files
ryanbeltleeonadoh
authored andcommitted
last modify with testing and bug fix
1 parent 358c9c1 commit fc207d4

File tree

2 files changed

+87
-19
lines changed

2 files changed

+87
-19
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import matplotlib.ticker as mticker
1212
from matplotlib.testing.decorators import cleanup
1313

14+
import warnings
15+
1416

1517
def test_MaxNLocator():
1618
loc = mticker.MaxNLocator(nbins=5)
@@ -63,6 +65,80 @@ def test_LogLocator():
6365
assert_almost_equal(loc.tick_values(1, 100), test_value)
6466

6567

68+
def test_LogLocator_set_params():
69+
"""
70+
Create log locator with default value, base=10.0, subs=[1.0], numdecs=4, numticks=15
71+
and change it to something else.
72+
See if change was successful.
73+
Should not exception.
74+
"""
75+
loc = mticker.LogLocator()
76+
loc.set_params(numticks = 8, numdecs = 8, subs = [2.0], base = 8)
77+
nose.tools.assert_equal(loc.numticks, 8)
78+
nose.tools.assert_equal(loc.numdecs, 8)
79+
nose.tools.assert_equal(loc.base, 8)
80+
nose.tools.assert_equal(loc.subs, [2.0])
81+
82+
83+
def test_NullLocator_set_params():
84+
"""
85+
Create null locator, and attempt to call set_params() on it.
86+
Should not exception, and should raise a warning.
87+
"""
88+
loc = mticker.NullLocator()
89+
with warnings.catch_warnings(True) as w:
90+
loc.set_params()
91+
nose.tools.assert_equal(len(w), 1)
92+
93+
94+
def test_MultipleLocator_set_params():
95+
"""
96+
Create multiple locator with 0.7 base, and change it to something else.
97+
See if change was successful.
98+
Should not exception.
99+
"""
100+
mult = mticker.MultipleLocator(base = 0.7)
101+
mult.set_params(base = 1.7)
102+
nose.tools.assert_equal(mult.base, 1.7)
103+
104+
105+
def test_FixedLocator_set_params():
106+
"""
107+
Create fixed locator with 5 nbins, and change it to something else.
108+
See if change was successful.
109+
Should not exception.
110+
"""
111+
fixed = mticker.FixedLocator(range (0,24),nbins=5)
112+
fixed.set_params(nbins = 7)
113+
nose.tools.assert_equal(fixed.nbins, 7)
114+
115+
116+
def test_IndexLocator_set_params():
117+
"""
118+
Create index locator with 3 base, 4 offset. and change it to something else.
119+
See if change was successful.
120+
Should not exception.
121+
"""
122+
index = mticker.IndexLocator(base = 3, offset = 4)
123+
index.set_params(base = 7, offset = 7)
124+
nose.tools.assert_equal(index.base, 7)
125+
nose.tools.assert_equal(index.offset, 7)
126+
127+
128+
def test_SymmetricalLogLocator_set_params():
129+
"""
130+
Create symmetrical log locator with default subs =[1.0] numticks = 15,
131+
and change it to something else.
132+
See if change was successful.
133+
Should not exception.
134+
"""
135+
#since we only test for the params change. I will pass empty transform
136+
sym = mticker.SymmetricalLogLocator(None)
137+
sym.set_params(subs = [2.0], numticks = 8)
138+
nose.tools.assert_equal(sym._subs, [2.0])
139+
nose.tools.assert_equal(sym.numticks, 8)
140+
141+
66142
def test_LogFormatterExponent():
67143
class FakeAxis(object):
68144
"""Allow Formatter to be called without having a "full" plot set up."""
@@ -111,25 +187,6 @@ def test_formatstrformatter():
111187
tmp_form = mticker.StrMethodFormatter('{x:05d}')
112188
nose.tools.assert_equal('00002', tmp_form(2))
113189

114-
@cleanup
115-
def test_set_params():
116-
loc = mticker.LogLocator(numticks = 2)
117-
118-
assert_raises(ValueError, loc.tick_values, 0, 1000)
119-
#this should return number of ticks as 5
120-
test_value = np.array([ 1.0000000e-10, 1.0000000e-03, 1.0000000e+04, 1.0000000e+11,
121-
1.0000000e+18])
122-
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)
123-
#after the set_params(numticks=8), it should have 7 extra which should have 7 extra
124-
#numticks. Also it won't casue to thrown the exception for bugs # #3658
125-
loc.set_params(numticks = 8)
126-
test_value = np.array([ 1.0000000e-04, 1.0000000e-03, 1.0000000e-02, 1.0000000e-01,
127-
1.0000000e+00, 1.0000000e+01, 1.0000000e+02, 1.0000000e+03,
128-
1.0000000e+04, 1.0000000e+05, 1.0000000e+06, 1.0000000e+07])
129-
assert_almost_equal(loc.tick_values(0.001, 1.1e5), test_value)
130-
131-
132-
133190
if __name__ == '__main__':
134191
import nose
135192
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/ticker.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@
155155
from matplotlib import cbook
156156
from matplotlib import transforms as mtransforms
157157

158+
import warnings
159+
158160
if six.PY3:
159161
long = int
160162

@@ -953,6 +955,9 @@ def tick_values(self, vmin, vmax):
953955
"""
954956
raise NotImplementedError('Derived must override')
955957

958+
def set_params(self, **kwargs):
959+
warnings.warn("'set_params()' not defined for locator of type " + str(type(self)))
960+
956961
def __call__(self):
957962
"""Return the locations of the ticks"""
958963
# note: some locators return data limits, other return view limits,
@@ -1610,6 +1615,12 @@ def __init__(self, transform, subs=None):
16101615
self._subs = subs
16111616
self.numticks = 15
16121617

1618+
def set_params(self, **kwargs):
1619+
if 'numticks' in kwargs:
1620+
self.numticks = kwargs['numticks']
1621+
if 'subs' in kwargs:
1622+
self._subs = kwargs['subs']
1623+
16131624
def __call__(self):
16141625
'Return the locations of the ticks'
16151626
# Note, these are untransformed coordinates

0 commit comments

Comments
 (0)