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