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