|
11 | 11 |
|
12 | 12 | import numpy as np |
13 | 13 | import matplotlib.pyplot as plt |
14 | | -from matplotlib import colors |
| 14 | +from matplotlib import colors as mcolors |
15 | 15 |
|
16 | 16 |
|
17 | | -colors_ = list(six.iteritems(colors.cnames)) |
18 | | - |
19 | | -# Add the single letter colors. |
20 | | -for name, rgb in six.iteritems(colors.ColorConverter.colors): |
21 | | - hex_ = colors.rgb2hex(rgb) |
22 | | - colors_.append((name, hex_)) |
23 | | - |
24 | | -# Transform to hex color values. |
25 | | -hex_ = [color[1] for color in colors_] |
26 | | -# Get the rgb equivalent. |
27 | | -rgb = [colors.hex2color(color) for color in hex_] |
28 | | -# Get the hsv equivalent. |
29 | | -hsv = [colors.rgb_to_hsv(color) for color in rgb] |
30 | | - |
31 | | -# Split the hsv values to sort. |
32 | | -hue = [color[0] for color in hsv] |
33 | | -sat = [color[1] for color in hsv] |
34 | | -val = [color[2] for color in hsv] |
35 | | - |
36 | | -# Get the color names by themselves. |
37 | | -names = [color[0] for color in colors_] |
| 17 | +colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) |
38 | 18 |
|
39 | 19 | # Sort by hue, saturation, value and name. |
40 | | -ind = np.lexsort((names, val, sat, hue)) |
41 | | -sorted_colors = [colors_[i] for i in ind] |
| 20 | +by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) |
| 21 | + for name, color in colors.items()) |
| 22 | + |
| 23 | +# Get the sorted color names. |
| 24 | +sorted_names = [name for hsv, name in by_hsv] |
42 | 25 |
|
43 | | -n = len(sorted_colors) |
| 26 | +n = len(sorted_names) |
44 | 27 | ncols = 4 |
45 | 28 | nrows = int(np.ceil(1. * n / ncols)) |
46 | 29 |
|
|
53 | 36 | # col width |
54 | 37 | w = X / ncols |
55 | 38 |
|
56 | | -for i, (name, color) in enumerate(sorted_colors): |
| 39 | +for i, name in enumerate(sorted_names): |
57 | 40 | col = i % ncols |
58 | 41 | row = int(i / ncols) |
59 | 42 | y = Y - (row * h) - h |
|
68 | 51 |
|
69 | 52 | # Add extra black line a little bit thicker to make |
70 | 53 | # clear colors more visible. |
71 | | - ax.hlines(y, xi_line, xf_line, color='black', linewidth=(h * 0.7)) |
72 | | - ax.hlines(y + h * 0.1, xi_line, xf_line, color=color, linewidth=(h * 0.6)) |
| 54 | + ax.hlines( |
| 55 | + y, xi_line, xf_line, color='black', linewidth=(h * 0.7)) |
| 56 | + ax.hlines( |
| 57 | + y + h * 0.1, xi_line, xf_line, color=colors[name], linewidth=(h * 0.6)) |
73 | 58 |
|
74 | 59 | ax.set_xlim(0, X) |
75 | 60 | ax.set_ylim(0, Y) |
|
0 commit comments