From 4858b654e9cb6c430f57bd0b4c37a6fcc6de7592 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 3 Jan 2023 22:11:07 +0100 Subject: [PATCH] Minor cleanups to named colors example. --- examples/color/named_colors.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/examples/color/named_colors.py b/examples/color/named_colors.py index 59f15e307bd5..5a8e3667689a 100644 --- a/examples/color/named_colors.py +++ b/examples/color/named_colors.py @@ -17,12 +17,14 @@ on some common color categories. """ +import math + from matplotlib.patches import Rectangle import matplotlib.pyplot as plt import matplotlib.colors as mcolors -def plot_colortable(colors, sort_colors=True, emptycols=0): +def plot_colortable(colors, *, ncols=4, sort_colors=True): cell_width = 212 cell_height = 22 @@ -31,16 +33,13 @@ def plot_colortable(colors, sort_colors=True, emptycols=0): # Sort colors by hue, saturation, value and name. if sort_colors is True: - by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))), - name) - for name, color in colors.items()) - names = [name for hsv, name in by_hsv] + names = sorted( + colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c)))) else: names = list(colors) n = len(names) - ncols = 4 - emptycols - nrows = n // ncols + int(n % ncols > 0) + nrows = math.ceil(n / ncols) width = cell_width * 4 + 2 * margin height = cell_height * nrows + 2 * margin @@ -79,14 +78,14 @@ def plot_colortable(colors, sort_colors=True, emptycols=0): # Base colors # ----------- -plot_colortable(mcolors.BASE_COLORS, sort_colors=False, emptycols=1) +plot_colortable(mcolors.BASE_COLORS, ncols=3, sort_colors=False) ############################################################################# # --------------- # Tableau Palette # --------------- -plot_colortable(mcolors.TABLEAU_COLORS, sort_colors=False, emptycols=2) +plot_colortable(mcolors.TABLEAU_COLORS, ncols=2, sort_colors=False) ############################################################################# # ---------- @@ -104,7 +103,7 @@ def plot_colortable(colors, sort_colors=True, emptycols=0): # XKCD colors are supported, but they produce a large figure, so we skip them # for now. You can use the following code if desired:: # -# xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors") +# xkcd_fig = plot_colortable(mcolors.XKCD_COLORS) # xkcd_fig.savefig("XKCD_Colors.png") # # .. admonition:: References pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy