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