1717on some common color categories.
1818"""
1919
20+ import math
21+
2022from matplotlib .patches import Rectangle
2123import matplotlib .pyplot as plt
2224import matplotlib .colors as mcolors
2325
2426
25- def plot_colortable (colors , sort_colors = True , emptycols = 0 ):
27+ def plot_colortable (colors , * , ncols = 4 , sort_colors = True ):
2628
2729 cell_width = 212
2830 cell_height = 22
@@ -31,16 +33,13 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
3133
3234 # Sort colors by hue, saturation, value and name.
3335 if sort_colors is True :
34- by_hsv = sorted ((tuple (mcolors .rgb_to_hsv (mcolors .to_rgb (color ))),
35- name )
36- for name , color in colors .items ())
37- names = [name for hsv , name in by_hsv ]
36+ names = sorted (
37+ colors , key = lambda c : tuple (mcolors .rgb_to_hsv (mcolors .to_rgb (c ))))
3838 else :
3939 names = list (colors )
4040
4141 n = len (names )
42- ncols = 4 - emptycols
43- nrows = n // ncols + int (n % ncols > 0 )
42+ nrows = math .ceil (n / ncols )
4443
4544 width = cell_width * 4 + 2 * margin
4645 height = cell_height * nrows + 2 * margin
@@ -79,14 +78,14 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
7978# Base colors
8079# -----------
8180
82- plot_colortable (mcolors .BASE_COLORS , sort_colors = False , emptycols = 1 )
81+ plot_colortable (mcolors .BASE_COLORS , ncols = 3 , sort_colors = False )
8382
8483#############################################################################
8584# ---------------
8685# Tableau Palette
8786# ---------------
8887
89- plot_colortable (mcolors .TABLEAU_COLORS , sort_colors = False , emptycols = 2 )
88+ plot_colortable (mcolors .TABLEAU_COLORS , ncols = 2 , sort_colors = False )
9089
9190#############################################################################
9291# ----------
@@ -104,7 +103,7 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
104103# XKCD colors are supported, but they produce a large figure, so we skip them
105104# for now. You can use the following code if desired::
106105#
107- # xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors" )
106+ # xkcd_fig = plot_colortable(mcolors.XKCD_COLORS)
108107# xkcd_fig.savefig("XKCD_Colors.png")
109108#
110109# .. admonition:: References
0 commit comments