|
2 | 2 | import matplotlib.pyplot as plt
|
3 | 3 | from matplotlib import cbook
|
4 | 4 | from matplotlib import colors
|
5 |
| -from itertools import chain |
6 | 5 |
|
7 |
| -n = len(colors.cnames) |
8 |
| -ncols = n / 15 |
9 |
| -nrows = n / ncols |
| 6 | +colors_ = colors.cnames.items() |
10 | 7 |
|
11 |
| -names = colors.cnames.keys() |
12 |
| -hex_ = colors.cnames.values() |
| 8 | +#add the single letter colors |
| 9 | +for name, rgb in colors.ColorConverter.colors.items(): |
| 10 | + hex_ = colors.rgb2hex(rgb) |
| 11 | + colors_.append((name, hex_)) |
| 12 | + |
| 13 | +#hex color values |
| 14 | +hex_ = [color[1] for color in colors_] |
| 15 | +#rgb equivalent |
13 | 16 | rgb = [colors.hex2color(color) for color in hex_]
|
| 17 | +#hsv equivalent |
14 | 18 | hsv = [colors.rgb_to_hsv(color) for color in rgb]
|
15 | 19 |
|
| 20 | +#split the hsv to sort |
16 | 21 | hue = [color[0] for color in hsv]
|
17 | 22 | sat = [color[1] for color in hsv]
|
18 | 23 | val = [color[2] for color in hsv]
|
19 | 24 |
|
| 25 | +#sort by hue, saturation and value |
20 | 26 | ind = np.lexsort((val, sat, hue))
|
21 |
| - |
22 |
| -names_ = [] |
23 |
| -colors_ = [] |
24 |
| - |
25 |
| -for i in ind: |
26 |
| - names_.append(names[i]) |
27 |
| - colors_.append(hex_[i]) |
28 |
| - |
29 |
| -fig, axes_ = plt.subplots(nrows=nrows, ncols=ncols) |
30 |
| -axes = list(chain(*axes_)) |
31 |
| - |
32 |
| -for i in range(n): |
33 |
| - title = axes[i].set_title(names_[i]) |
| 27 | +sorted_colors = [colors_[i] for i in ind] |
| 28 | + |
| 29 | +n = len(sorted_colors) |
| 30 | +ncols = 10 |
| 31 | +nrows = int(np.ceil(1. * n / ncols)) |
| 32 | + |
| 33 | +fig = plt.figure() |
| 34 | +for i, (name, color) in enumerate(sorted_colors): |
| 35 | + ax = fig.add_subplot(nrows, ncols, i + 1, |
| 36 | + axisbg=color, |
| 37 | + xticks=[], yticks=[]) |
| 38 | + title = ax.set_title(name) |
34 | 39 | title.set_size('xx-small')
|
35 |
| - axes[i].set_axis_bgcolor(colors_[i]) |
36 |
| - axes[i].spines['right'].set_visible(False) |
37 |
| - axes[i].spines['top'].set_visible(False) |
38 |
| - axes[i].spines['bottom'].set_visible(False) |
39 |
| - axes[i].spines['left'].set_visible(False) |
40 |
| - axes[i].set_xticks([]) |
41 |
| - axes[i].set_yticks([]) |
| 40 | + ax.spines['right'].set_visible(False) |
| 41 | + ax.spines['top'].set_visible(False) |
| 42 | + ax.spines['bottom'].set_visible(False) |
| 43 | + ax.spines['left'].set_visible(False) |
42 | 44 |
|
43 | 45 | plt.tight_layout()
|
44 | 46 | plt.show()
|
0 commit comments