|
| 1 | +""" |
| 2 | +Visualization of named colors. |
| 3 | +
|
| 4 | +Simple plot example with the named colors and its visual representation. |
| 5 | +""" |
| 6 | + |
1 | 7 | import numpy as np
|
2 | 8 | import matplotlib.pyplot as plt
|
3 |
| -from matplotlib import cbook |
4 |
| -from matplotlib import colors |
5 |
| - |
| 9 | +from matplotlib import colors |
| 10 | + |
6 | 11 | colors_ = colors.cnames.items()
|
7 |
| - |
8 |
| -#add the single letter colors |
| 12 | + |
| 13 | +# Add the single letter colors. |
9 | 14 | for name, rgb in colors.ColorConverter.colors.items():
|
10 | 15 | hex_ = colors.rgb2hex(rgb)
|
11 | 16 | colors_.append((name, hex_))
|
12 |
| - |
13 |
| -#hex color values |
| 17 | + |
| 18 | +# Transform to hex color values. |
14 | 19 | hex_ = [color[1] for color in colors_]
|
15 |
| -#rgb equivalent |
| 20 | +# Get the rgb equivalent. |
16 | 21 | rgb = [colors.hex2color(color) for color in hex_]
|
17 |
| -#hsv equivalent |
| 22 | +# Get the hsv equivalent. |
18 | 23 | hsv = [colors.rgb_to_hsv(color) for color in rgb]
|
19 |
| - |
20 |
| -#split the hsv to sort |
| 24 | + |
| 25 | +# Split the hsv values to sort. |
21 | 26 | hue = [color[0] for color in hsv]
|
22 | 27 | sat = [color[1] for color in hsv]
|
23 | 28 | val = [color[2] for color in hsv]
|
24 |
| - |
25 |
| -#sort by hue, saturation and value |
| 29 | + |
| 30 | +# Sort by hue, saturation and value. |
26 | 31 | ind = np.lexsort((val, sat, hue))
|
27 | 32 | sorted_colors = [colors_[i] for i in ind]
|
28 |
| - |
| 33 | + |
29 | 34 | n = len(sorted_colors)
|
30 | 35 | ncols = 3
|
31 | 36 | nrows = int(np.ceil(1. * n / ncols))
|
32 |
| - |
| 37 | + |
33 | 38 | fig = plt.figure(figsize=(ncols*5, nrows*4))
|
34 | 39 | for i, (name, color) in enumerate(sorted_colors):
|
35 |
| - ax = fig.add_subplot(nrows, ncols, i + 1, |
36 |
| - xticks=[], yticks=[]) |
| 40 | + ax = fig.add_subplot(nrows, ncols, i + 1) |
37 | 41 | ax.text(0.55, 0.5, name, fontsize=12,
|
38 |
| - horizontalalignment='left', |
39 |
| - verticalalignment='center') |
40 |
| - |
41 |
| - ax.hlines(0.5, 0, 0.5, color='black', linewidth=9) |
| 42 | + horizontalalignment='left', |
| 43 | + verticalalignment='center') |
| 44 | + |
| 45 | + # Add extra black line a little bit thicker to make |
| 46 | + # clear colors more visible. |
| 47 | + ax.hlines(0.5, 0, 0.5, color='black', linewidth=10) |
42 | 48 | ax.hlines(0.5, 0, 0.5, color=color, linewidth=8)
|
43 | 49 | ax.set_xlim(0, 1)
|
44 | 50 | ax.set_ylim(0, 1)
|
|
0 commit comments