Skip to content

Commit 9848661

Browse files
committed
Improve set_prop_cycle(). Docstring fixes.
1 parent 5a3ce17 commit 9848661

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

lib/matplotlib/axes/_base.py

+49-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import math
1010
from operator import itemgetter
1111

12-
from cycler import cycler, Cycler
1312
import numpy as np
1413
from numpy import ma
1514

@@ -33,6 +32,7 @@
3332
from matplotlib.offsetbox import OffsetBox
3433
from matplotlib.artist import allow_rasterization
3534
from matplotlib.cbook import iterable
35+
from matplotlib.rcsetup import cycler
3636

3737
rcParams = matplotlib.rcParams
3838

@@ -149,18 +149,20 @@ def __setstate__(self, state):
149149
self.__dict__ = state.copy()
150150
self.set_prop_cycle()
151151

152-
def set_prop_cycle(self, prop_cycler=None):
153-
if prop_cycler is None:
152+
def set_prop_cycle(self, *args, **kwargs):
153+
if not (args or kwargs) or (len(args) == 1 and args[0] is None):
154154
prop_cycler = rcParams['axes.prop_cycle']
155155
if prop_cycler is None and 'axes.color_cycle' in rcParams:
156156
clist = rcParams['axes.color_cycle']
157157
prop_cycler = cycler('color', clist)
158+
else:
159+
prop_cycler = cycler(*args, **kwargs)
160+
158161
self.prop_cycler = itertools.cycle(prop_cycler)
159162
# This should make a copy
160163
self._prop_keys = prop_cycler.keys
161164

162165
def __call__(self, *args, **kwargs):
163-
164166
if self.axes.xaxis is not None and self.axes.yaxis is not None:
165167
xunits = kwargs.pop('xunits', self.axes.xaxis.units)
166168

@@ -1055,14 +1057,51 @@ def clear(self):
10551057
"""clear the axes"""
10561058
self.cla()
10571059

1058-
def set_prop_cycle(self, prop_cycle):
1060+
def set_prop_cycle(self, *args, **kwargs):
10591061
"""
10601062
Set the property cycle for any future plot commands on this Axes.
10611063
1062-
*prop_cycle* is a :class:`Cycler` object.
1063-
Can also be `None` to reset to the cycle defined by the
1064-
current style.
1065-
"""
1064+
set_prop_cycle(arg)
1065+
set_prop_cycle(label, itr)
1066+
set_prop_cycle(label1=itr1[, label2=itr2[, ...]])
1067+
1068+
Form 1 simply sets given `Cycler` object.
1069+
1070+
Form 2 creates and sets a `Cycler` from a label and an iterable.
1071+
1072+
Form 3 composes and sets a `Cycler` as an inner product of the
1073+
pairs of keyword arguments. In other words, all of the
1074+
iterables are cycled simultaneously, as if through zip().
1075+
1076+
Parameters
1077+
----------
1078+
arg : Cycler
1079+
Set the given Cycler.
1080+
Can also be `None` to reset to the cycle defined by the
1081+
current style.
1082+
1083+
label : name
1084+
The property key. Must be a valid `Artist` property.
1085+
For example, 'color' or 'linestyle'. Aliases are allowed,
1086+
such as 'c' for 'color' and 'lw' for 'linewidth'.
1087+
1088+
itr : iterable
1089+
Finite-length iterable of the property values. These values
1090+
are validated and will raise a ValueError if invalid.
1091+
1092+
See Also
1093+
--------
1094+
:func:`cycler` Convenience function for creating your
1095+
own cyclers.
1096+
1097+
"""
1098+
if args and kwargs:
1099+
raise TypeError("Cannot supply both positional and keyword "
1100+
"arguments to this method.")
1101+
if len(args) == 1 and args[0] is None:
1102+
prop_cycle = None
1103+
else:
1104+
prop_cycle = cycler(*args, **kwargs)
10661105
self._get_lines.set_prop_cycle(prop_cycle)
10671106
self._get_patches_for_fill.set_prop_cycle(prop_cycle)
10681107

@@ -1076,7 +1115,7 @@ def set_color_cycle(self, clist):
10761115
"""
10771116
cbook.warn_deprecated(
10781117
'1.5', name='set_color_cycle', alternative='set_prop_cycle')
1079-
self.set_prop_cycle(cycler('color', clist))
1118+
self.set_prop_cycle('color', clist)
10801119

10811120
def ishold(self):
10821121
"""return the HOLD status of the axes"""

lib/matplotlib/rcsetup.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -622,26 +622,25 @@ def cycler(*args, **kwargs):
622622
623623
Form 1 simply copies a given `Cycler` object.
624624
625-
Form 2 composes a `Cycler` as an inner product of the
625+
Form 2 creates a `Cycler` from a label and an iterable.
626+
627+
Form 3 composes a `Cycler` as an inner product of the
626628
pairs of keyword arguments. In other words, all of the
627629
iterables are cycled simultaneously, as if through zip().
628630
629-
Form 3 creates a `Cycler` from a label and an iterable.
630-
This is useful for when the label cannot be a keyword argument
631-
(e.g., an integer or a name that has a space in it).
632-
633631
Parameters
634632
----------
635633
arg : Cycler
636634
Copy constructor for Cycler.
637635
638636
label : name
639-
The property key. In the 2-arg form of the function,
640-
the label can be any hashable object. In the keyword argument
641-
form of the function, it must be a valid python identifier.
637+
The property key. Must be a valid `Artist` property.
638+
For example, 'color' or 'linestyle'. Aliases are allowed,
639+
such as 'c' for 'color' and 'lw' for 'linewidth'.
642640
643641
itr : iterable
644-
Finite length iterable of the property values.
642+
Finite-length iterable of the property values. These values
643+
are validated and will raise a ValueError if invalid.
645644
646645
Returns
647646
-------
@@ -650,10 +649,10 @@ def cycler(*args, **kwargs):
650649
651650
"""
652651
if args and kwargs:
653-
raise TypeError("cyl() can only accept positional OR keyword "
652+
raise TypeError("cycler() can only accept positional OR keyword "
654653
"arguments -- not both.")
655654
elif not args and not kwargs:
656-
raise TypeError("cyl() must have positional OR keyword arguments")
655+
raise TypeError("cycler() must have positional OR keyword arguments")
657656

658657
if len(args) == 1:
659658
if not isinstance(args[0], Cycler):

0 commit comments

Comments
 (0)