{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n*****************\nSpecifying Colors\n*****************\n\nMatplotlib recognizes the following formats to specify a color:\n\n* 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)``);\n* a hex RGB or RGBA string (e.g., ``'#0F0F0F'`` or ``'#0F0F0F0F'``);\n* a string representation of a float value in ``[0, 1]`` inclusive for gray\n level (e.g., ``'0.5'``);\n* one of ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}``;\n* a X11/CSS4 color name;\n* a name from the `xkcd color survey `__;\n prefixed with ``'xkcd:'`` (e.g., ``'xkcd:sky blue'``);\n* one of ``{'tab:blue', 'tab:orange', 'tab:green',\n 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink',\n 'tab:gray', 'tab:olive', 'tab:cyan'}`` which are the Tableau Colors from the\n 'T10' categorical palette (which is the default color cycle);\n* a \"CN\" color spec, i.e. `'C'` followed by a single digit, which is an index\n into the default property cycle (``matplotlib.rcParams['axes.prop_cycle']``);\n the indexing occurs at artist creation time and defaults to black if the\n cycle does not include color.\n\nAll string specifications of color, other than \"CN\", are case-insensitive.\n\n\"CN\" color selection\n--------------------\n\n\"CN\" colors are converted to RGBA as soon as the artist is created. For\nexample,\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\n\nth = np.linspace(0, 2*np.pi, 128)\n\n\ndef demo(sty):\n mpl.style.use(sty)\n fig, ax = plt.subplots(figsize=(3, 3))\n\n ax.set_title('style: {!r}'.format(sty), color='C0')\n\n ax.plot(th, np.cos(th), 'C1', label='C1')\n ax.plot(th, np.sin(th), 'C2', label='C2')\n ax.legend()\n\ndemo('default')\ndemo('seaborn')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"will use the first color for the title and then plot using the second\nand third colors of each style's ``mpl.rcParams['axes.prop_cycle']``.\n\n\nxkcd v X11/CSS4\n---------------\n\nThe xkcd colors are derived from a user survey conducted by the\nwebcomic xkcd. `Details of the survey are available on the xkcd blog\n`__.\n\nOut of 148 colors in the CSS color list, there are 95 name collisions\nbetween the X11/CSS4 names and the xkcd names, all but 3 of which have\ndifferent hex values. For example ``'blue'`` maps to ``'#0000FF'``\nwhere as ``'xkcd:blue'`` maps to ``'#0343DF'``. Due to these name\ncollisions all of the xkcd colors have ``'xkcd:'`` prefixed. As noted in\nthe blog post, while it might be interesting to re-define the X11/CSS4 names\nbased on such a survey, we do not do so unilaterally.\n\nThe name collisions are shown in the table below; the color names\nwhere the hex values agree are shown in bold.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib._color_data as mcd\nimport matplotlib.patches as mpatch\n\noverlap = {name for name in mcd.CSS4_COLORS\n if \"xkcd:\" + name in mcd.XKCD_COLORS}\n\nfig = plt.figure(figsize=[4.8, 16])\nax = fig.add_axes([0, 0, 1, 1])\n\nfor j, n in enumerate(sorted(overlap, reverse=True)):\n weight = None\n cn = mcd.CSS4_COLORS[n]\n xkcd = mcd.XKCD_COLORS[\"xkcd:\" + n].upper()\n if cn == xkcd:\n weight = 'bold'\n\n r1 = mpatch.Rectangle((0, j), 1, 1, color=cn)\n r2 = mpatch.Rectangle((1, j), 1, 1, color=xkcd)\n txt = ax.text(2, j+.5, ' ' + n, va='center', fontsize=10,\n weight=weight)\n ax.add_patch(r1)\n ax.add_patch(r2)\n ax.axhline(j, color='k')\n\nax.text(.5, j + 1.5, 'X11', ha='center', va='center')\nax.text(1.5, j + 1.5, 'xkcd', ha='center', va='center')\nax.set_xlim(0, 3)\nax.set_ylim(0, j + 2)\nax.axis('off')"
]
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}