|
| 1 | +.. _colors: |
| 2 | + |
| 3 | +***************** |
| 4 | +Specifying Colors |
| 5 | +***************** |
| 6 | + |
| 7 | +In almost all places in matplotlib where a color can be specified by the user it can be provided as: |
| 8 | + |
| 9 | +* ``(r, g, b)`` tuples |
| 10 | +* ``(r, g, b, a)`` tuples |
| 11 | +* hex string, ex ``#OFOFOF`` |
| 12 | +* float value between [0, 1] for gray level |
| 13 | +* One of ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}`` |
| 14 | +* valid css4/X11 color names |
| 15 | +* valid name from the `XKCD color survey |
| 16 | + <http://blog.xkcd.com/2010/05/03/color-survey-results/>`__ These |
| 17 | + names are available both with and with out spaces. In the case of name clashes |
| 18 | + the css/X11 names have priority. To ensure colors |
| 19 | + from the XKCD mapping are used prefix the space-less name with |
| 20 | + ``'XKCD'``. |
| 21 | + |
| 22 | +All string specifications of color are case-insensitive. |
| 23 | + |
| 24 | +Internally, mpl is moving to storing all colors as RGBA float quadruples. |
| 25 | + |
| 26 | +Name clash between CSS4/X11 and XKCD |
| 27 | +------------------------------------ |
| 28 | + |
| 29 | +The color names in the XKCD survey include spaces (unlike css4/X11 |
| 30 | +names). Matplotlib exposes all of the XKCD colors both with and |
| 31 | +without spaces. |
| 32 | + |
| 33 | +There are 95 (out of 148 colors in the css color list) conflicts |
| 34 | +between the css4/X11 names and the XKCD names. Given that these are |
| 35 | +the standard color names of the web, matplotlib should follow these |
| 36 | +conventions. To accesses the XKCD colors which are shadowed by css4, |
| 37 | +prefix the colorname with ``'XKCD'``, for example ``'blue'`` maps to |
| 38 | +``'#0000FF'`` where as ``'XKCDblue'`` maps to ``'#0343DF'``. |
| 39 | + |
| 40 | +.. plot:: |
| 41 | + |
| 42 | + import matplotlib.pyplot as plt |
| 43 | + import matplotlib._color_data as mcd |
| 44 | + |
| 45 | + import matplotlib.patches as mpatch |
| 46 | + overlap = (set(mcd.CSS4_COLORS) & set(mcd.XKCD_COLORS)) |
| 47 | + |
| 48 | + fig = plt.figure(figsize=[4.8, 16]) |
| 49 | + ax = fig.add_axes([0, 0, 1, 1]) |
| 50 | + |
| 51 | + j = 0 |
| 52 | + |
| 53 | + for n in sorted(overlap, reverse=True): |
| 54 | + cn = mcd.CSS4_COLORS[n] |
| 55 | + xkcd = mcd.XKCD_COLORS[n].upper() |
| 56 | + if cn != xkcd: |
| 57 | + print (n, cn, xkcd) |
| 58 | + |
| 59 | + r1 = mpatch.Rectangle((0, j), 1, 1, color=cn) |
| 60 | + r2 = mpatch.Rectangle((1, j), 1, 1, color=xkcd) |
| 61 | + txt = ax.text(2, j+.5, ' ' + n, va='center', fontsize=10) |
| 62 | + ax.add_patch(r1) |
| 63 | + ax.add_patch(r2) |
| 64 | + ax.axhline(j, color='k') |
| 65 | + j += 1 |
| 66 | + |
| 67 | + ax.text(.5, j+.1, 'X11', ha='center') |
| 68 | + ax.text(1.5, j+.1, 'XKCD', ha='center') |
| 69 | + ax.set_xlim(0, 3) |
| 70 | + ax.set_ylim(0, j + 1) |
| 71 | + ax.axis('off') |
0 commit comments