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

Skip to content

Commit 494531e

Browse files
committed
Improvements to property cycler. Closes #5875
* Allow key normalization to Cycler objects * Validation is performed at more entry points * Requires Cycler v0.9+
1 parent 44e7356 commit 494531e

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -704,15 +704,7 @@ def cycler(*args, **kwargs):
704704
if not isinstance(args[0], Cycler):
705705
raise TypeError("If only one positional argument given, it must "
706706
" be a Cycler instance.")
707-
708-
c = args[0]
709-
unknowns = c.keys - (set(_prop_validators.keys()) |
710-
set(_prop_aliases.keys()))
711-
if unknowns:
712-
# This is about as much validation I can do
713-
raise TypeError("Unknown artist properties: %s" % unknowns)
714-
else:
715-
return Cycler(c)
707+
return validate_cycler(args[0])
716708
elif len(args) == 2:
717709
pairs = [(args[0], args[1])]
718710
elif len(args) > 2:
@@ -767,6 +759,17 @@ def validate_cycler(s):
767759
else:
768760
raise ValueError("object was not a string or Cycler instance: %s" % s)
769761

762+
unknowns = cycler_inst.keys - (set(_prop_validators.keys()) |
763+
set(_prop_aliases.keys()))
764+
if unknowns:
765+
raise ValueError("Unknown artist properties: %s" % unknowns)
766+
767+
# Not a full validation, but it'll at least normalize property names
768+
# A fuller validation would require v0.10 of cycler.
769+
for prop in cycler_inst.keys:
770+
norm_prop = _prop_aliases.get(prop, prop)
771+
cycler_inst.change_key(prop, norm_prop)
772+
770773
return cycler_inst
771774

772775

lib/matplotlib/tests/test_cycles.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_colorcycle_basic():
3131
def test_marker_cycle():
3232
fig = plt.figure()
3333
ax = fig.add_subplot(111)
34-
ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']) +
34+
ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) +
3535
cycler('marker', ['.', '*', 'x']))
3636
xs = np.arange(10)
3737
ys = 0.25 * xs + 2
@@ -47,7 +47,7 @@ def test_marker_cycle():
4747
fig = plt.figure()
4848
ax = fig.add_subplot(111)
4949
# Test keyword arguments, numpy arrays, and generic iterators
50-
ax.set_prop_cycle(color=np.array(['r', 'g', 'y']),
50+
ax.set_prop_cycle(c=np.array(['r', 'g', 'y']),
5151
marker=iter(['.', '*', 'x']))
5252
xs = np.arange(10)
5353
ys = 0.25 * xs + 2
@@ -66,7 +66,7 @@ def test_marker_cycle():
6666
def test_linestylecycle_basic():
6767
fig = plt.figure()
6868
ax = fig.add_subplot(111)
69-
ax.set_prop_cycle(cycler('linestyle', ['-', '--', ':']))
69+
ax.set_prop_cycle(cycler('ls', ['-', '--', ':']))
7070
xs = np.arange(10)
7171
ys = 0.25 * xs + 2
7272
ax.plot(xs, ys, label='solid', lw=4)
@@ -84,7 +84,7 @@ def test_linestylecycle_basic():
8484
def test_fillcycle_basic():
8585
fig = plt.figure()
8686
ax = fig.add_subplot(111)
87-
ax.set_prop_cycle(cycler('color', ['r', 'g', 'y']) +
87+
ax.set_prop_cycle(cycler('c', ['r', 'g', 'y']) +
8888
cycler('hatch', ['xx', 'O', '|-']) +
8989
cycler('linestyle', ['-', '--', ':']))
9090
xs = np.arange(10)
@@ -130,7 +130,7 @@ def test_valid_input_forms():
130130
ax.set_prop_cycle(None)
131131
ax.set_prop_cycle(cycler('linewidth', [1, 2]))
132132
ax.set_prop_cycle('color', 'rgywkbcm')
133-
ax.set_prop_cycle('linewidth', (1, 2))
133+
ax.set_prop_cycle('lw', (1, 2))
134134
ax.set_prop_cycle('linewidth', [1, 2])
135135
ax.set_prop_cycle('linewidth', iter([1, 2]))
136136
ax.set_prop_cycle('linewidth', np.array([1, 2]))
@@ -179,6 +179,11 @@ def test_invalid_input_forms():
179179
'linewidth', {'1': 1, '2': 2})
180180
assert_raises((TypeError, ValueError), ax.set_prop_cycle,
181181
linewidth=1, color='r')
182+
assert_raises((TypeError, ValueError), ax.set_prop_cycle, 'foobar', [1, 2])
183+
assert_raises((TypeError, ValueError), ax.set_prop_cycle,
184+
foobar=[1, 2])
185+
assert_raises((TypeError, ValueError), ax.set_prop_cycle,
186+
cycler(foobar=[1, 2]))
182187

183188

184189
if __name__ == '__main__':

lib/matplotlib/tests/test_rcparams.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ def test_validators():
321321
("cycler('c', 'rgb') * cycler('linestyle', ['-', '--'])",
322322
(cycler('color', 'rgb') *
323323
cycler('linestyle', ['-', '--']))),
324+
(cycler('ls', ['-', '--']),
325+
cycler('linestyle', ['-', '--'])),
326+
(cycler(mew=[2, 5]),
327+
cycler('markeredgewidth', [2, 5])),
324328
),
325329
# This is *so* incredibly important: validate_cycler() eval's
326330
# an arbitrary string! I think I have it locked down enough,
@@ -342,6 +346,7 @@ def test_validators():
342346
('cycler("waka", [1, 2, 3])', ValueError), # not a property
343347
('cycler(c=[1, 2, 3])', ValueError), # invalid values
344348
("cycler(lw=['a', 'b', 'c'])", ValueError), # invalid values
349+
(cycler('waka', [1, 3, 5]), ValueError), # not a property
345350
)
346351
},
347352
{'validator': validate_hatch,

setupext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ def check(self):
11861186
return "using cycler version %s" % cycler.__version__
11871187

11881188
def get_install_requires(self):
1189-
return ['cycler']
1189+
return ['cycler>=0.9']
11901190

11911191

11921192
class Dateutil(SetupPackage):

0 commit comments

Comments
 (0)