17
17
on some common color categories.
18
18
"""
19
19
20
+ import math
21
+
20
22
from matplotlib .patches import Rectangle
21
23
import matplotlib .pyplot as plt
22
24
import matplotlib .colors as mcolors
23
25
24
26
25
- def plot_colortable (colors , sort_colors = True , emptycols = 0 ):
27
+ def plot_colortable (colors , * , ncols = 4 , sort_colors = True ):
26
28
27
29
cell_width = 212
28
30
cell_height = 22
@@ -31,16 +33,13 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
31
33
32
34
# Sort colors by hue, saturation, value and name.
33
35
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 ))))
38
38
else :
39
39
names = list (colors )
40
40
41
41
n = len (names )
42
- ncols = 4 - emptycols
43
- nrows = n // ncols + int (n % ncols > 0 )
42
+ nrows = math .ceil (n / ncols )
44
43
45
44
width = cell_width * 4 + 2 * margin
46
45
height = cell_height * nrows + 2 * margin
@@ -79,14 +78,14 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
79
78
# Base colors
80
79
# -----------
81
80
82
- plot_colortable (mcolors .BASE_COLORS , sort_colors = False , emptycols = 1 )
81
+ plot_colortable (mcolors .BASE_COLORS , ncols = 3 , sort_colors = False )
83
82
84
83
#############################################################################
85
84
# ---------------
86
85
# Tableau Palette
87
86
# ---------------
88
87
89
- plot_colortable (mcolors .TABLEAU_COLORS , sort_colors = False , emptycols = 2 )
88
+ plot_colortable (mcolors .TABLEAU_COLORS , ncols = 2 , sort_colors = False )
90
89
91
90
#############################################################################
92
91
# ----------
@@ -104,7 +103,7 @@ def plot_colortable(colors, sort_colors=True, emptycols=0):
104
103
# XKCD colors are supported, but they produce a large figure, so we skip them
105
104
# for now. You can use the following code if desired::
106
105
#
107
- # xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors" )
106
+ # xkcd_fig = plot_colortable(mcolors.XKCD_COLORS)
108
107
# xkcd_fig.savefig("XKCD_Colors.png")
109
108
#
110
109
# .. admonition:: References
0 commit comments