|
1 | 1 | """ |
2 | | -Visualization of named colors. |
| 2 | +======================== |
| 3 | +Visualizing named colors |
| 4 | +======================== |
3 | 5 |
|
4 | 6 | Simple plot example with the named colors and its visual representation. |
5 | 7 | """ |
| 8 | +from __future__ import division |
6 | 9 |
|
7 | | -from __future__ import (absolute_import, division, print_function, |
8 | | - unicode_literals) |
9 | | - |
10 | | -import six |
11 | | - |
12 | | -import numpy as np |
13 | 10 | import matplotlib.pyplot as plt |
14 | 11 | from matplotlib import colors as mcolors |
15 | 12 |
|
16 | 13 |
|
17 | 14 | colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) |
18 | 15 |
|
19 | | -# Sort by hue, saturation, value and name. |
| 16 | +# Sort colors by hue, saturation, value and name. |
20 | 17 | by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) |
21 | 18 | for name, color in colors.items()) |
22 | | - |
23 | | -# Get the sorted color names. |
24 | 19 | sorted_names = [name for hsv, name in by_hsv] |
25 | 20 |
|
26 | 21 | n = len(sorted_names) |
27 | 22 | ncols = 4 |
28 | | -nrows = int(np.ceil(1. * n / ncols)) |
| 23 | +nrows = n // ncols + 1 |
29 | 24 |
|
30 | 25 | fig, ax = plt.subplots(figsize=(8, 5)) |
31 | 26 |
|
| 27 | +# Get height and width |
32 | 28 | X, Y = fig.get_dpi() * fig.get_size_inches() |
33 | | - |
34 | | -# row height |
35 | 29 | h = Y / (nrows + 1) |
36 | | -# col width |
37 | 30 | w = X / ncols |
38 | 31 |
|
39 | 32 | for i, name in enumerate(sorted_names): |
40 | 33 | col = i % ncols |
41 | | - row = int(i / ncols) |
| 34 | + row = i // ncols |
42 | 35 | y = Y - (row * h) - h |
43 | 36 |
|
44 | 37 | xi_line = w * (col + 0.05) |
|
49 | 42 | horizontalalignment='left', |
50 | 43 | verticalalignment='center') |
51 | 44 |
|
52 | | - ax.hlines( |
53 | | - y + h * 0.1, xi_line, xf_line, color=colors[name], linewidth=(h * 0.6)) |
| 45 | + ax.hlines(y + h * 0.1, xi_line, xf_line, |
| 46 | + color=colors[name], linewidth=(h * 0.6)) |
54 | 47 |
|
55 | 48 | ax.set_xlim(0, X) |
56 | 49 | ax.set_ylim(0, Y) |
|
0 commit comments