Skip to content

Commit 49aa465

Browse files
authored
Merge pull request #24881 from meeseeksmachine/auto-backport-of-pr-24880-on-v3.6.x
Backport PR #24880 on branch v3.6.x (Minor cleanups to named colors example.)
2 parents a886617 + 0d402af commit 49aa465

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

examples/color/named_colors.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
on some common color categories.
1818
"""
1919

20+
import math
21+
2022
from matplotlib.patches import Rectangle
2123
import matplotlib.pyplot as plt
2224
import 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

Comments
 (0)
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