Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #24880 on branch v3.6.2-doc (Minor cleanups to named colors example.) #24882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions examples/color/named_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

#############################################################################
# ----------
Expand All @@ -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
Expand Down