{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n.. redirect-from:: /tutorials/intermediate/color_cycle\n\n\n# Styling with cycler\n\nDemo of custom property-cycle settings to control colors and other style\nproperties for multi-line plots.\n\n

Note

More complete documentation of the ``cycler`` API can be found\n [here](https://matplotlib.org/cycler/).

\n\nThis example demonstrates two different APIs:\n\n1. Setting the rc parameter specifying the default property cycle.\n This affects all subsequent Axes (but not Axes already created).\n2. Setting the property cycle for a single pair of Axes.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from cycler import cycler\n\nimport matplotlib.pyplot as plt\nimport numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we'll generate some sample data, in this case, four offset sine\ncurves.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.linspace(0, 2 * np.pi, 50)\noffsets = np.linspace(0, 2 * np.pi, 4, endpoint=False)\nyy = np.transpose([np.sin(x + phi) for phi in offsets])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now ``yy`` has shape\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(yy.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So ``yy[:, i]`` will give you the ``i``-th offset sine curve. Let's set the\ndefault ``prop_cycle`` using :func:`matplotlib.pyplot.rc`. We'll combine a\ncolor cycler and a linestyle cycler by adding (``+``) two ``cycler``'s\ntogether. See the bottom of this tutorial for more information about\ncombining different cyclers.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "default_cycler = (cycler(color=['r', 'g', 'b', 'y']) +\n cycler(linestyle=['-', '--', ':', '-.']))\n\nplt.rc('lines', linewidth=4)\nplt.rc('axes', prop_cycle=default_cycler)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we'll generate a figure with two Axes, one on top of the other. On the\nfirst axis, we'll plot with the default cycler. On the second axis, we'll\nset the ``prop_cycle`` using :func:`matplotlib.axes.Axes.set_prop_cycle`,\nwhich will only set the ``prop_cycle`` for this :mod:`matplotlib.axes.Axes`\ninstance. We'll use a second ``cycler`` that combines a color cycler and a\nlinewidth cycler.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "custom_cycler = (cycler(color=['c', 'm', 'y', 'k']) +\n cycler(lw=[1, 2, 3, 4]))\n\nfig, (ax0, ax1) = plt.subplots(nrows=2)\nax0.plot(yy)\nax0.set_title('Set default color cycle to rgby')\nax1.set_prop_cycle(custom_cycler)\nax1.plot(yy)\nax1.set_title('Set axes color cycle to cmyk')\n\n# Add a bit more space between the two plots.\nfig.subplots_adjust(hspace=0.3)\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting ``prop_cycle`` in the :file:`matplotlibrc` file or style files\n\nRemember, a custom cycler can be set in your :file:`matplotlibrc`\nfile or a style file (:file:`style.mplstyle`) under ``axes.prop_cycle``:\n\n```python\naxes.prop_cycle : cycler(color='bgrcmyk')\n```\n## Cycling through multiple properties\n\nYou can add cyclers:\n\n```python\nfrom cycler import cycler\ncc = (cycler(color=list('rgb')) +\n cycler(linestyle=['-', '--', '-.']))\nfor d in cc:\n print(d)\n```\nResults in:\n\n```python\n{'color': 'r', 'linestyle': '-'}\n{'color': 'g', 'linestyle': '--'}\n{'color': 'b', 'linestyle': '-.'}\n```\nYou can multiply cyclers:\n\n```python\nfrom cycler import cycler\ncc = (cycler(color=list('rgb')) *\n cycler(linestyle=['-', '--', '-.']))\nfor d in cc:\n print(d)\n```\nResults in:\n\n```python\n{'color': 'r', 'linestyle': '-'}\n{'color': 'r', 'linestyle': '--'}\n{'color': 'r', 'linestyle': '-.'}\n{'color': 'g', 'linestyle': '-'}\n{'color': 'g', 'linestyle': '--'}\n{'color': 'g', 'linestyle': '-.'}\n{'color': 'b', 'linestyle': '-'}\n{'color': 'b', 'linestyle': '--'}\n{'color': 'b', 'linestyle': '-.'}\n```\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }