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

Skip to content

Backport PR #10899: Update cycler docstrings and favor kwarg over two… #11084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def _check_deps():
'python': ('https://docs.python.org/3', None),
'numpy': ('https://docs.scipy.org/doc/numpy/', None),
'scipy': ('https://docs.scipy.org/doc/scipy/reference/', None),
'pandas': ('https://pandas.pydata.org/pandas-docs/stable', None)
'pandas': ('https://pandas.pydata.org/pandas-docs/stable', None),
'cycler': ('https://matplotlib.org/cycler', None),
}

explicit_order_folders = [
Expand Down
4 changes: 2 additions & 2 deletions examples/api/filled_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ def stack_hist(ax, stacked_data, sty_cycle, bottoms=None,

# set up style cycles
color_cycle = cycler(facecolor=plt.rcParams['axes.prop_cycle'][:4])
label_cycle = cycler('label', ['set {n}'.format(n=n) for n in range(4)])
hatch_cycle = cycler('hatch', ['/', '*', '+', '|'])
label_cycle = cycler(label=['set {n}'.format(n=n) for n in range(4)])
hatch_cycle = cycler(hatch=['/', '*', '+', '|'])

# Fixing random state for reproducibility
np.random.seed(19680801)
Expand Down
12 changes: 8 additions & 4 deletions examples/color/color_cycler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@

# 1. Setting prop cycle on default rc parameter
plt.rc('lines', linewidth=4)
plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'y']) +
cycler('linestyle', ['-', '--', ':', '-.'])))
plt.rc('axes', prop_cycle=(cycler(color=['r', 'g', 'b', 'y']) +
cycler(linestyle=['-', '--', ':', '-.'])))
fig, (ax0, ax1) = plt.subplots(nrows=2)
ax0.plot(yy)
ax0.set_title('Set default color cycle to rgby')

# 2. Define prop cycle for single set of axes
ax1.set_prop_cycle(cycler('color', ['c', 'm', 'y', 'k']) +
cycler('lw', [1, 2, 3, 4]))
# For the most general use-case, you can provide a cycler to
# `.set_prop_cycle`.
# Here, we use the convenient shortcut that we can alternatively pass
# one or more properties as keyword arguements. This creates and sets
# a cycler iterating simultaneously over all properties.
ax1.set_prop_cycle(color=['c', 'm', 'y', 'k'], lw=[1, 2, 3, 4])
ax1.plot(yy)
ax1.set_title('Set axes color cycle to cmyk')

Expand Down
25 changes: 16 additions & 9 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,16 +1186,22 @@ def set_prop_cycle(self, *args, **kwargs):
Call signatures::

set_prop_cycle(cycler)
set_prop_cycle(label, values)
set_prop_cycle(label=values[, label2=values2[, ...]])
set_prop_cycle(label, values)

Form 1 simply sets given `Cycler` object.
Form 1 sets given `~cycler.Cycler` object.

Form 2 creates and sets a `Cycler` from a label and an iterable.
Form 2 creates a `~cycler.Cycler` which cycles over one or more
properties simultaneously and set it as the property cycle of the
axes. If multiple properties are given, their value lists must have
the same length. This is just a shortcut for explicitly creating a
cycler and passing it to the function, i.e. it's short for
``set_prop_cycle(cycler(label=values label2=values2, ...))``.

Form 3 composes and sets a `Cycler` as an inner product of the
pairs of keyword arguments. In other words, all of the
iterables are cycled simultaneously, as if through zip().
Form 3 creates a `~cycler.Cycler` for a single property and set it
as the property cycle of the axes. This form exists for compatibility
with the original `cycler.cycler` interface. Its use is discouraged
in favor of the kwarg form, i.e. ``set_prop_cycle(label=values)``.

Parameters
----------
Expand All @@ -1216,8 +1222,7 @@ def set_prop_cycle(self, *args, **kwargs):
--------
Setting the property cycle for a single property:

>>> ax.set_prop_cycle(color=['red', 'green', 'blue']) # or
>>> ax.set_prop_cycle('color', ['red', 'green', 'blue'])
>>> ax.set_prop_cycle(color=['red', 'green', 'blue'])

Setting the property cycle for simultaneously cycling over multiple
properties (e.g. red circle, green plus, blue cross):
Expand All @@ -1228,7 +1233,9 @@ def set_prop_cycle(self, *args, **kwargs):
See Also
--------
matplotlib.rcsetup.cycler
Convenience function for creating your own cyclers.
Convenience function for creating validated cyclers for properties.
cycler.cycler
The original function for creating unvalidated cyclers.

"""
if args and kwargs:
Expand Down
21 changes: 11 additions & 10 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,22 +719,24 @@ def validate_hatch(s):

def cycler(*args, **kwargs):
"""
Creates a :class:`cycler.Cycler` object much like :func:`cycler.cycler`,
Creates a `~cycler.Cycler` object much like :func:`cycler.cycler`,
but includes input validation.

Call signatures::

cycler(cycler)
cycler(label, values)
cycler(label=values[, label2=values2[, ...]])
cycler(label, values)

Form 1 simply copies a given `Cycler` object.
Form 1 copies a given `~cycler.Cycler` object.

Form 2 creates a `Cycler` from a label and an iterable.
Form 2 creates a `~cycler.Cycler` which cycles over one or more
properties simultaneously. If multiple properties are given, their
value lists must have the same length.

Form 3 composes a `Cycler` as an inner product of the
pairs of keyword arguments. In other words, all of the
iterables are cycled simultaneously, as if through zip().
Form 3 creates a `~cycler.Cycler` for a single property. This form
exists for compatibility with the original cycler. Its use is
discouraged in favor of the kwarg form, i.e. ``cycler(label=values)``.

Parameters
----------
Expand All @@ -753,14 +755,13 @@ def cycler(*args, **kwargs):
Returns
-------
cycler : Cycler
New :class:`cycler.Cycler` for the given properties
A new :class:`~cycler.Cycler` for the given properties.

Examples
--------
Creating a cycler for a single property:

>>> c = cycler(color=['red', 'green', 'blue']) # or
>>> c = cycler('color', ['red', 'green', 'blue'])
>>> c = cycler(color=['red', 'green', 'blue'])

Creating a cycler for simultaneously cycling over multiple properties
(e.g. red circle, green plus, blue cross):
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/stackplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import six
from six.moves import xrange

from cycler import cycler
import numpy as np

__all__ = ['stackplot']
Expand Down Expand Up @@ -69,7 +67,7 @@ def stackplot(axes, x, *args, **kwargs):

colors = kwargs.pop('colors', None)
if colors is not None:
axes.set_prop_cycle(cycler('color', colors))
axes.set_prop_cycle(color=colors)

baseline = kwargs.pop('baseline', 'zero')
# Assume data passed has not been 'stacked', so stack it here.
Expand Down
10 changes: 5 additions & 5 deletions tutorials/intermediate/color_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
# cycler and a linestyle cycler by adding (``+``) two ``cycler``'s together.
# See the bottom of this tutorial for more information about combining
# different cyclers.
default_cycler = cycler('color', ['r', 'g', 'b', 'y']) \
+ cycler('linestyle', ['-', '--', ':', '-.'])
default_cycler = (cycler(color=['r', 'g', 'b', 'y']) +
cycler(linestyle=['-', '--', ':', '-.']))

plt.rc('lines', linewidth=4)
plt.rc('axes', prop_cycle=default_cycler)
Expand All @@ -52,8 +52,8 @@
# which will only set the ``prop_cycle`` for this :mod:`matplotlib.axes.Axes`
# instance. We'll use a second ``cycler`` that combines a color cycler and a
# linewidth cycler.
custom_cycler = cycler('color', ['c', 'm', 'y', 'k']) \
+ cycler('lw', [1, 2, 3, 4])
custom_cycler = (cycler(color=['c', 'm', 'y', 'k']) +
cycler(lw=[1, 2, 3, 4]))

fig, (ax0, ax1) = plt.subplots(nrows=2)
ax0.plot(yy)
Expand All @@ -76,7 +76,7 @@
#
# ..code-block:: python
#
# axes.prop_cycle : cycler('color', 'bgrcmyk')
# axes.prop_cycle : cycler(color='bgrcmyk')
#
# Cycling through multiple properties
# -----------------------------------
Expand Down