|
| 1 | +""" |
| 2 | +=================== |
| 3 | +Styling with cycler |
| 4 | +=================== |
| 5 | +
|
| 6 | +Demo of custom property-cycle settings to control colors and other style |
| 7 | +properties for multi-line plots. |
| 8 | +
|
| 9 | +.. note:: |
| 10 | +
|
| 11 | + More complete documentation of the ``cycler`` API can be found |
| 12 | + `here <http://matplotlib.org/cycler/>`_. |
| 13 | +
|
| 14 | +This example demonstrates two different APIs: |
| 15 | +
|
| 16 | +1. Setting the default rc parameter specifying the property cycle. |
| 17 | + This affects all subsequent axes (but not axes already created). |
| 18 | +2. Setting the property cycle for a single pair of axes. |
| 19 | +
|
| 20 | +""" |
| 21 | +from cycler import cycler |
| 22 | +import numpy as np |
| 23 | +import matplotlib.pyplot as plt |
| 24 | + |
| 25 | +############################################################################### |
| 26 | +# First we'll generate some sample data, in this case, four offset sine |
| 27 | +# curves. |
| 28 | +x = np.linspace(0, 2 * np.pi, 50) |
| 29 | +offsets = np.linspace(0, 2 * np.pi, 4, endpoint=False) |
| 30 | +yy = np.transpose([np.sin(x + phi) for phi in offsets]) |
| 31 | + |
| 32 | +############################################################################### |
| 33 | +# Now ``yy`` has shape |
| 34 | +print(yy.shape) |
| 35 | + |
| 36 | +############################################################################### |
| 37 | +# So ``yy[:, i]`` will give you the ``i``-th offset sine curve. Let's set the |
| 38 | +# default prop_cycle using :func:`matplotlib.pyplot.rc`. We'll combine a color |
| 39 | +# cycler and a linestyle cycler by adding (``+``) two ``cycler``'s together. |
| 40 | +# See the bottom of this tutorial for more information about combining |
| 41 | +# different cyclers. |
| 42 | +default_cycler = cycler('color', ['r', 'g', 'b', 'y']) \ |
| 43 | + + cycler('linestyle', ['-', '--', ':', '-.']) |
| 44 | + |
| 45 | +plt.rc('lines', linewidth=4) |
| 46 | +plt.rc('axes', prop_cycle=default_cycler) |
| 47 | + |
| 48 | +############################################################################### |
| 49 | +# Now we'll generate a figure with two axes, one on top of the other. On the |
| 50 | +# first axis, we'll plot with the default cycler. On the second axis, we'll |
| 51 | +# set the prop_cycler using :func:`matplotlib.axes.Axes.set_prop_cycle` |
| 52 | +# which will only set the ``prop_cycle`` for this :mod:`matplotlib.axes.Axes` |
| 53 | +# instance. We'll use a second ``cycler`` that combines a color cycler and a |
| 54 | +# linewidth cycler. |
| 55 | +custom_cycler = cycler('color', ['c', 'm', 'y', 'k']) \ |
| 56 | + + cycler('lw', [1, 2, 3, 4]) |
| 57 | + |
| 58 | +fig, (ax0, ax1) = plt.subplots(nrows=2) |
| 59 | +ax0.plot(yy) |
| 60 | +ax0.set_title('Set default color cycle to rgby') |
| 61 | +ax1.set_prop_cycle(custom_cycler) |
| 62 | +ax1.plot(yy) |
| 63 | +ax1.set_title('Set axes color cycle to cmyk') |
| 64 | + |
| 65 | +# Add a bit more space between the two plots. |
| 66 | +fig.subplots_adjust(hspace=0.3) |
| 67 | +plt.show() |
| 68 | + |
| 69 | +############################################################################### |
| 70 | +# Setting ``prop_cycler`` in the ``matplotlibrc`` file or style files |
| 71 | +# ------------------------------------------------------------------- |
| 72 | +# |
| 73 | +# Remember, if you want to set a custom ``prop_cycler`` in your |
| 74 | +# ``.matplotlibrc`` file or a style file (``style.mplstyle``), you can set the |
| 75 | +# ``axes.prop_cycle`` property: |
| 76 | +# |
| 77 | +# ..code-block:: python |
| 78 | +# |
| 79 | +# axes.prop_cycle : cycler('color', 'bgrcmyk') |
| 80 | +# |
| 81 | +# Cycling through multiple properties |
| 82 | +# ----------------------------------- |
| 83 | +# |
| 84 | +# You can add cyclers: |
| 85 | +# |
| 86 | +# .. code-block:: python |
| 87 | +# |
| 88 | +# from cycler import cycler |
| 89 | +# cc = (cycler(color=list('rgb')) + |
| 90 | +# cycler(linestyle=['-', '--', '-.'])) |
| 91 | +# for d in cc: |
| 92 | +# print(d) |
| 93 | +# |
| 94 | +# Results in: |
| 95 | +# |
| 96 | +# .. code-block:: python |
| 97 | +# |
| 98 | +# {'color': 'r', 'linestyle': '-'} |
| 99 | +# {'color': 'g', 'linestyle': '--'} |
| 100 | +# {'color': 'b', 'linestyle': '-.'} |
| 101 | +# |
| 102 | +# |
| 103 | +# You can multiply cyclers: |
| 104 | +# |
| 105 | +# .. code-block:: python |
| 106 | +# |
| 107 | +# from cycler import cycler |
| 108 | +# cc = (cycler(color=list('rgb')) * |
| 109 | +# cycler(linestyle=['-', '--', '-.'])) |
| 110 | +# for d in cc: |
| 111 | +# print(d) |
| 112 | +# |
| 113 | +# Results in: |
| 114 | +# |
| 115 | +# .. code-block:: python |
| 116 | +# |
| 117 | +# {'color': 'r', 'linestyle': '-'} |
| 118 | +# {'color': 'r', 'linestyle': '--'} |
| 119 | +# {'color': 'r', 'linestyle': '-.'} |
| 120 | +# {'color': 'g', 'linestyle': '-'} |
| 121 | +# {'color': 'g', 'linestyle': '--'} |
| 122 | +# {'color': 'g', 'linestyle': '-.'} |
| 123 | +# {'color': 'b', 'linestyle': '-'} |
| 124 | +# {'color': 'b', 'linestyle': '--'} |
| 125 | +# {'color': 'b', 'linestyle': '-.'} |
0 commit comments