|
13 | 13 | """
|
14 | 14 |
|
15 | 15 | import matplotlib.pyplot as plt
|
16 |
| -from matplotlib import colors as mcolors |
| 16 | +import matplotlib.colors as mcolors |
17 | 17 |
|
18 | 18 |
|
19 |
| -colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) |
| 19 | +def plot_colortable(colors, title, sort_colors=True, emptycols=0): |
20 | 20 |
|
21 |
| -# Sort colors by hue, saturation, value and name. |
22 |
| -by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) |
23 |
| - for name, color in colors.items()) |
24 |
| -sorted_names = [name for hsv, name in by_hsv] |
| 21 | + cell_width = 212 |
| 22 | + cell_height = 24 |
| 23 | + swatch_width = 48 |
| 24 | + margin = 12 |
| 25 | + topmargin = 40 |
25 | 26 |
|
26 |
| -n = len(sorted_names) |
27 |
| -ncols = 4 |
28 |
| -nrows = n // ncols |
| 27 | + # Sort colors by hue, saturation, value and name. |
| 28 | + by_hsv = ((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) |
| 29 | + for name, color in colors.items()) |
| 30 | + if sort_colors is True: |
| 31 | + by_hsv = sorted(by_hsv) |
| 32 | + names = [name for hsv, name in by_hsv] |
29 | 33 |
|
30 |
| -fig, ax = plt.subplots(figsize=(9, 8)) |
| 34 | + n = len(names) |
| 35 | + ncols = 4 - emptycols |
| 36 | + nrows = n // ncols + int(n % ncols > 0) |
31 | 37 |
|
32 |
| -# Get height and width |
33 |
| -X, Y = fig.get_dpi() * fig.get_size_inches() |
34 |
| -h = Y / (nrows + 1) |
35 |
| -w = X / ncols |
| 38 | + width = cell_width * 4 + 2 * margin |
| 39 | + height = cell_height * nrows + margin + topmargin |
| 40 | + dpi = 72 |
36 | 41 |
|
37 |
| -for i, name in enumerate(sorted_names): |
38 |
| - row = i % nrows |
39 |
| - col = i // nrows |
40 |
| - y = Y - (row * h) - h |
| 42 | + fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) |
| 43 | + fig.subplots_adjust(margin/width, margin/height, |
| 44 | + (width-margin)/width, (height-topmargin)/height) |
| 45 | + ax.set_xlim(0, cell_width * 4) |
| 46 | + ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) |
| 47 | + ax.yaxis.set_visible(False) |
| 48 | + ax.xaxis.set_visible(False) |
| 49 | + ax.set_axis_off() |
| 50 | + ax.set_title(title, fontsize=24, loc="left") |
41 | 51 |
|
42 |
| - xi_line = w * (col + 0.05) |
43 |
| - xf_line = w * (col + 0.25) |
44 |
| - xi_text = w * (col + 0.3) |
| 52 | + for i, name in enumerate(names): |
| 53 | + row = i % nrows |
| 54 | + col = i // nrows |
| 55 | + y = row * cell_height |
45 | 56 |
|
46 |
| - ax.text(xi_text, y, name, fontsize=(h * 0.5), |
47 |
| - horizontalalignment='left', |
48 |
| - verticalalignment='center') |
| 57 | + swatch_start_x = cell_width * col |
| 58 | + swatch_end_x = cell_width * col + swatch_width |
| 59 | + text_pos_x = cell_width * col + swatch_width + 7 |
49 | 60 |
|
50 |
| - ax.hlines(y + h * 0.1, xi_line, xf_line, |
51 |
| - color=colors[name], linewidth=(h * 0.6)) |
| 61 | + ax.text(text_pos_x, y, name, fontsize=13, |
| 62 | + horizontalalignment='left', |
| 63 | + verticalalignment='center') |
52 | 64 |
|
53 |
| -ax.set_xlim(0, X) |
54 |
| -ax.set_ylim(0, Y) |
55 |
| -ax.set_axis_off() |
| 65 | + ax.hlines(y, swatch_start_x, swatch_end_x, |
| 66 | + color=colors[name], linewidth=20) |
| 67 | + |
| 68 | + return fig |
| 69 | + |
| 70 | +plot_colortable(mcolors.BASE_COLORS, "Base Colors", |
| 71 | + sort_colors=False, emptycols=2) |
| 72 | +plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette", |
| 73 | + sort_colors=False, emptycols=2) |
| 74 | + |
| 75 | +#sphinx_gallery_thumbnail_number = 4 |
| 76 | +plot_colortable(mcolors.CSS4_COLORS, "CSS Colors") |
| 77 | + |
| 78 | +# Optionally plot the XKCD colors (Caution: will produce large figure) |
| 79 | +#xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors") |
| 80 | +#xkcd_fig.savefig("XKCD_Colors.png") |
56 | 81 |
|
57 |
| -fig.subplots_adjust(left=0, right=1, |
58 |
| - top=1, bottom=0, |
59 |
| - hspace=0, wspace=0) |
60 | 82 | plt.show()
|
61 | 83 |
|
| 84 | + |
62 | 85 | #############################################################################
|
63 | 86 | #
|
64 | 87 | # ------------
|
|
0 commit comments