8000 Merge pull request #4172 from ryanbelt/master · wypd/matplotlib@c6f7c65 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6f7c65

Browse files
committed
Merge pull request matplotlib#4172 from ryanbelt/master
axes.locator_params fails with LogLocator (and most Locator subclasses) matplotlib#3658
2 parents ed0ce1a + e340cc8 commit c6f7c65

File tree

2 files changed

+155
-2
lines changed

2 files changed

+155
-2
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 97 additions & 2 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)
@@ -50,7 +52,6 @@ def test_AutoMinorLocator():
5052

5153
def test_LogLocator():
5254
loc = mticker.LogLocator(numticks=5)
53-
5455
assert_raises(ValueError, loc.tick_values, 0, 1000)
5556

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

6566

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

114-
115210
if __name__ == '__main__':
116211
import nose
117212
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/ticker.py

Lines changed: 58 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,14 @@ def tick_values(self, vmin, vmax):
953955
"""
954956
raise NotImplementedError('Derived must override')
955957

958+
def set_params(self, **kwargs):
959+
"""
960+
Do nothing, and rase a warning. Any locator class not supporting the
961+
set_params() function will call this.
962+
"""
963+
warnings.warn("'set_params()' not defined for locator of type " +
964+
str(type(self)))
965+
956966
def __call__(self):
957967
"""Return the locations of the ticks"""
958968
# note: some locators return data limits, other return view limits,
@@ -1025,6 +1035,13 @@ def __init__(self, base, offset):
10251035
self._base = base
10261036
self.offset = offset
10271037

1038+
def set_params(self, base=None, offset=None):
1039+
"""Set parameters within this locator"""
1040+
if base is not None:
1041+
self._base = base
1042+
if offset is not None:
1043+
self.offset = offset
1044+
10281045
def __call__(self):
10291046
"""Return the locations of the ticks"""
10301047
dmin, dmax = self.axis.get_data_interval()
@@ -1052,6 +1069,11 @@ def __init__(self, locs, nbins=None):
10521069
if self.nbins is not None:
10531070
self.nbins = max(self.nbins, 2)
10541071

1072+
def set_params(self, nbins=None):
1073+
"""Set parameters within this locator."""
1074+
if nbins is not None:
1075+
self.nbins = nbins
1076+
10551077
def __call__(self):
10561078
return self.tick_values(None, None)
10571079

@@ -1116,6 +1138,13 @@ def __init__(self, numticks=None, presets=None):
11161138
else:
11171139
self.presets = presets
11181140

1141+
def set_params(self, numticks=None, presets=None):
1142+
"""Set parameters within this locator."""
1143+
if presets is not None:
1144+
self.presets = presets
1145+
if numticks is not None:
1146+
self.numticks = numticks
1147+
11191148
def __call__(self):
11201149
'Return the locations of the ticks'
11211150
vmin, vmax = self.axis.get_view_interval()
@@ -1218,6 +1247,11 @@ class MultipleLocator(Locator):
12181247
def __init__(self, base=1.0):
12191248
self._base = Base(base)
12201249

1250+
def set_params(self, base):
1251+
"""Set parameters within this locator."""
1252+
if base is not None:
1253+
self._base = base
1254+
12211255
def __call__(self):
12221256
'Return the locations of the ticks'
12231257
vmin, vmax = self.axis.get_view_interval()
@@ -1319,6 +1353,7 @@ def __init__(self, *args, **kwargs):
13191353
self.set_params(**kwargs)
13201354

13211355
def set_params(self, **kwargs):
1356+
"""Set parameters within this locator."""
13221357
if 'nbins' in kwargs:
13231358
self._nbins = int(kwargs['nbins'])
13241359
if 'trim' in kwargs:
@@ -1453,6 +1488,17 @@ def __init__(self, base=10.0, subs=[1.0], numdecs=4, numticks=15):
14531488
self.numticks = numticks
14541489
self.numdecs = numdecs
14551490

1491+
def set_params(self, base=None, subs=None, numdecs=None, numticks=None):
1492+
"""Set parameters within this locator."""
1493+
if base is not None:
1494+
self.base = base
1495+
if subs is not None:
1496+
self.subs = subs
1497+
if numdecs is not None:
1498+
self.numdecs = numdecs
1499+
if numticks is not None:
1500+
self.numticks = numticks
1501+
14561502
def base(self, base):
14571503
"""
14581504
set the base of the log scaling (major tick every base**i, i integer)
@@ -1580,6 +1626,13 @@ def __init__(self, transform, subs=None):
15801626
self._subs = subs
15811627
self.numticks = 15
15821628

1629+
def set_params(self, subs=None, numticks=None):
1630+
"""Set parameters within this locator."""
1631+
if numticks is not None:
1632+
self.numticks = numticks
1633+
if subs is not None:
1634+
self._subs = subs
1635+
15831636
def __call__(self):
15841637
'Return the locations of the ticks'
15851638
# Note, these are untransformed coordinates
@@ -1728,6 +1781,11 @@ def __init__(self, minor=False):
17281781
"""
17291782
self.minor = minor
17301783

1784+
def set_params(self, minor=None):
1785+
"""Set parameters within this locator."""
1786+
if minor is not None:
1787+
self.minor = minor
1788+
17311789
def __call__(self):
17321790
'Return the locations of the ticks'
17331791
vmin, vmax = self.axis.get_view_interval()

0 commit comments

Comments
 (0)
0