From 99e8fb0f50b2cef2f70b1de95136e58ac8313c9e Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 24 Sep 2018 16:34:10 +0100 Subject: [PATCH] Backport PR #12209: Doc: Sort named colors example by palette --- examples/color/named_colors.py | 91 ++++++++++++++++++++++------------ tutorials/colors/colors.py | 2 + 2 files changed, 60 insertions(+), 33 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index 5a16c2d813f1..56a93d7ef0ae 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -3,7 +3,9 @@ Visualizing named colors ======================== -Simple plot example with the named colors and its visual representation. +This plots a list of the named colors supported in matplotlib. Note that +:ref:`xkcd colors ` are supported as well, but are not listed here +for brevity. For more information on colors in matplotlib see @@ -13,52 +15,75 @@ """ import matplotlib.pyplot as plt -from matplotlib import colors as mcolors +import matplotlib.colors as mcolors -colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) +def plot_colortable(colors, title, sort_colors=True, emptycols=0): -# Sort colors by hue, saturation, value and name. -by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) - for name, color in colors.items()) -sorted_names = [name for hsv, name in by_hsv] + cell_width = 212 + cell_height = 22 + swatch_width = 48 + margin = 12 + topmargin = 40 -n = len(sorted_names) -ncols = 4 -nrows = n // ncols + # Sort colors by hue, saturation, value and name. + by_hsv = ((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) + for name, color in colors.items()) + if sort_colors is True: + by_hsv = sorted(by_hsv) + names = [name for hsv, name in by_hsv] -fig, ax = plt.subplots(figsize=(9, 8)) + n = len(names) + ncols = 4 - emptycols + nrows = n // ncols + int(n % ncols > 0) -# Get height and width -X, Y = fig.get_dpi() * fig.get_size_inches() -h = Y / (nrows + 1) -w = X / ncols + width = cell_width * 4 + 2 * margin + height = cell_height * nrows + margin + topmargin + dpi = 72 -for i, name in enumerate(sorted_names): - row = i % nrows - col = i // nrows - y = Y - (row * h) - h + fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) + fig.subplots_adjust(margin/width, margin/height, + (width-margin)/width, (height-topmargin)/height) + ax.set_xlim(0, cell_width * 4) + ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) + ax.yaxis.set_visible(False) + ax.xaxis.set_visible(False) + ax.set_axis_off() + ax.set_title(title, fontsize=24, loc="left", pad=10) - xi_line = w * (col + 0.05) - xf_line = w * (col + 0.25) - xi_text = w * (col + 0.3) + for i, name in enumerate(names): + row = i % nrows + col = i // nrows + y = row * cell_height - ax.text(xi_text, y, name, fontsize=(h * 0.5), - horizontalalignment='left', - verticalalignment='center') + swatch_start_x = cell_width * col + swatch_end_x = cell_width * col + swatch_width + text_pos_x = cell_width * col + swatch_width + 7 - ax.hlines(y + h * 0.1, xi_line, xf_line, - color=colors[name], linewidth=(h * 0.6)) + ax.text(text_pos_x, y, name, fontsize=14, + horizontalalignment='left', + verticalalignment='center') -ax.set_xlim(0, X) -ax.set_ylim(0, Y) -ax.set_axis_off() + ax.hlines(y, swatch_start_x, swatch_end_x, + color=colors[name], linewidth=18) + + return fig + +plot_colortable(mcolors.BASE_COLORS, "Base Colors", + sort_colors=False, emptycols=1) +plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette", + sort_colors=False, emptycols=2) + +#sphinx_gallery_thumbnail_number = 3 +plot_colortable(mcolors.CSS4_COLORS, "CSS Colors") + +# Optionally plot the XKCD colors (Caution: will produce large figure) +#xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors") +#xkcd_fig.savefig("XKCD_Colors.png") -fig.subplots_adjust(left=0, right=1, - top=1, bottom=0, - hspace=0, wspace=0) plt.show() + ############################################################################# # # ------------ diff --git a/tutorials/colors/colors.py b/tutorials/colors/colors.py index 5721cb6b0b5a..75a80081b43a 100644 --- a/tutorials/colors/colors.py +++ b/tutorials/colors/colors.py @@ -77,6 +77,8 @@ def demo(sty): # and third colors of each style's ``mpl.rcParams['axes.prop_cycle']``. # # +# .. _xkcd-colors: +# # xkcd v X11/CSS4 # --------------- #