{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Named color sequences\n\nMatplotlib's `~matplotlib.colors.ColorSequenceRegistry` allows access to\npredefined lists of colors by name e.g.\n``colors = matplotlib.color_sequences['Set1']``. This example shows all of the\nbuilt in color sequences.\n\nUser-defined sequences can be added via `.ColorSequenceRegistry.register`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib as mpl\n\n\ndef plot_color_sequences(names, ax):\n # Display each named color sequence horizontally on the supplied axes.\n\n for n, name in enumerate(names):\n colors = mpl.color_sequences[name]\n n_colors = len(colors)\n x = np.arange(n_colors)\n y = np.full_like(x, n)\n\n ax.scatter(x, y, facecolor=colors, edgecolor='dimgray', s=200, zorder=2)\n\n ax.set_yticks(range(len(names)), labels=names)\n ax.grid(visible=True, axis='y')\n ax.yaxis.set_inverted(True)\n ax.xaxis.set_visible(False)\n ax.spines[:].set_visible(False)\n ax.tick_params(left=False)\n\n\nbuilt_in_color_sequences = [\n 'tab10', 'tab20', 'tab20b', 'tab20c', 'Pastel1', 'Pastel2', 'Paired',\n 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'petroff10']\n\n\nfig, ax = plt.subplots(figsize=(6.4, 9.6), layout='constrained')\n\nplot_color_sequences(built_in_color_sequences, ax)\nax.set_title('Built In Color Sequences')\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.ColorSequenceRegistry`\n - `matplotlib.axes.Axes.scatter`\n\n.. tags::\n\n styling: color\n purpose: reference\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 }