FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Minor cleanups to named colors example. by anntzer · Pull Request #24880 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 9 additions & 10 deletions examples/color/named_colors.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL