{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Color Demo\n\nMatplotlib recognizes the following formats to specify a color:\n\n1) an RGB or RGBA tuple of float values in ``[0, 1]`` (e.g. ``(0.1, 0.2, 0.5)``\n or ``(0.1, 0.2, 0.5, 0.3)``). RGBA is short for Red, Green, Blue, Alpha;\n2) a hex RGB or RGBA string (e.g., ``'#0F0F0F'`` or ``'#0F0F0F0F'``);\n3) a shorthand hex RGB or RGBA string, equivalent to the hex RGB or RGBA\n string obtained by duplicating each character, (e.g., ``'#abc'``, equivalent\n to ``'#aabbcc'``, or ``'#abcd'``, equivalent to ``'#aabbccdd'``);\n4) a string representation of a float value in ``[0, 1]`` inclusive for gray\n level (e.g., ``'0.5'``);\n5) a single letter string, i.e. one of\n ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}``, which are short-hand notations\n for shades of blue, green, red, cyan, magenta, yellow, black, and white;\n6) a X11/CSS4 (\"html\") color name, e.g. ``\"blue\"``;\n7) a name from the [xkcd color survey](https://xkcd.com/color/rgb/)_,\n prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``);\n8) a \"Cn\" color spec, i.e. ``'C'`` followed by a number, which is an index into\n the default property cycle (:rc:`axes.prop_cycle`); the indexing is intended\n to occur at rendering time, and defaults to black if the cycle does not\n include color.\n9) one of ``{'tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple',\n 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan'}`` which are\n the Tableau Colors from the 'tab10' categorical palette (which is the\n default color cycle);\n\nFor more information on colors in matplotlib see\n\n* the `colors_def` tutorial;\n* the `matplotlib.colors` API;\n* the :doc:`/gallery/color/named_colors` example.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nt = np.linspace(0.0, 2.0, 201)\ns = np.sin(2 * np.pi * t)\n\n# 1) RGB tuple:\nfig, ax = plt.subplots(facecolor=(.18, .31, .31))\n# 2) hex string:\nax.set_facecolor('#eafff5')\n# 3) gray level string:\nax.set_title('Voltage vs. time chart', color='0.7')\n# 4) single letter color string\nax.set_xlabel('Time [s]', color='c')\n# 5) a named color:\nax.set_ylabel('Voltage [mV]', color='peachpuff')\n# 6) a named xkcd color:\nax.plot(t, s, 'xkcd:crimson')\n# 7) Cn notation:\nax.plot(t, .7*s, color='C4', linestyle='--')\n# 8) tab notation:\nax.tick_params(labelcolor='tab:orange')\n\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.colors`\n - `matplotlib.axes.Axes.plot`\n - `matplotlib.axes.Axes.set_facecolor`\n - `matplotlib.axes.Axes.set_title`\n - `matplotlib.axes.Axes.set_xlabel`\n - `matplotlib.axes.Axes.set_ylabel`\n - `matplotlib.axes.Axes.tick_params`\n\n.. tags::\n\n styling: color\n plot-type: line\n level: beginner\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 }