Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 146b935

Browse filesBrowse files
authored
Merge pull request #12250 from meeseeksmachine/auto-backport-of-pr-12209-on-v3.0.x
Backport PR #12209 on branch v3.0.x (Doc: Sort named colors example by palette)
2 parents f7c7ebe + 7e5d18c commit 146b935
Copy full SHA for 146b935

File tree

Expand file treeCollapse file tree

2 files changed

+60
-33
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+60
-33
lines changed

‎examples/color/named_colors.py

Copy file name to clipboardExpand all lines: examples/color/named_colors.py
+58-33Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Visualizing named colors
44
========================
55
6-
Simple plot example with the named colors and its visual representation.
6+
This plots a list of the named colors supported in matplotlib. Note that
7+
:ref:`xkcd colors <xkcd-colors>` are supported as well, but are not listed here
8+
for brevity.
79
810
For more information on colors in matplotlib see
911
@@ -13,52 +15,75 @@
1315
"""
1416

1517
import matplotlib.pyplot as plt
16-
from matplotlib import colors as mcolors
18+
import matplotlib.colors as mcolors
1719

1820

19-
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
21+
def plot_colortable(colors, title, sort_colors=True, emptycols=0):
2022

21-
# Sort colors by hue, saturation, value and name.
22-
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
23-
for name, color in colors.items())
24-
sorted_names = [name for hsv, name in by_hsv]
23+
cell_width = 212
24+
cell_height = 22
25+
swatch_width = 48
26+
margin = 12
27+
topmargin = 40
2528

26-
n = len(sorted_names)
27-
ncols = 4
28-
nrows = n // ncols
29+
# Sort colors by hue, saturation, value and name.
30+
by_hsv = ((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
31+
for name, color in colors.items())
32+
if sort_colors is True:
33+
by_hsv = sorted(by_hsv)
34+
names = [name for hsv, name in by_hsv]
2935

30-
fig, ax = plt.subplots(figsize=(9, 8))
36+
n = len(names)
37+
ncols = 4 - emptycols
38+
nrows = n // ncols + int(n % ncols > 0)
3139

32-
# Get height and width
33-
X, Y = fig.get_dpi() * fig.get_size_inches()
34-
h = Y / (nrows + 1)
35-
w = X / ncols
40+
width = cell_width * 4 + 2 * margin
41+
height = cell_height * nrows + margin + topmargin
42+
dpi = 72
3643

37-
for i, name in enumerate(sorted_names):
38-
row = i % nrows
39-
col = i // nrows
40-
y = Y - (row * h) - h
44+
fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
45+
fig.subplots_adjust(margin/width, margin/height,
46+
(width-margin)/width, (height-topmargin)/height)
47+
ax.set_xlim(0, cell_width * 4)
48+
ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
49+
ax.yaxis.set_visible(False)
50+
ax.xaxis.set_visible(False)
51+
ax.set_axis_off()
52+
ax.set_title(title, fontsize=24, loc="left", pad=10)
4153

42-
xi_line = w * (col + 0.05)
43-
xf_line = w * (col + 0.25)
44-
xi_text = w * (col + 0.3)
54+
for i, name in enumerate(names):
55+
row = i % nrows
56+
col = i // nrows
57+
y = row * cell_height
4558

46-
ax.text(xi_text, y, name, fontsize=(h * 0.5),
47-
horizontalalignment='left',
48-
verticalalignment='center')
59+
swatch_start_x = cell_width * col
60+
swatch_end_x = cell_width * col + swatch_width
61+
text_pos_x = cell_width * col + swatch_width + 7
4962

50-
ax.hlines(y + h * 0.1, xi_line, xf_line,
51-
color=colors[name], linewidth=(h * 0.6))
63+
ax.text(text_pos_x, y, name, fontsize=14,
64+
horizontalalignment='left',
65+
verticalalignment='center')
5266

53-
ax.set_xlim(0, X)
54-
ax.set_ylim(0, Y)
55-
ax.set_axis_off()
67+
ax.hlines(y, swatch_start_x, swatch_end_x,
68+
color=colors[name], linewidth=18)
69+
70+
return fig
71+
72+
plot_colortable(mcolors.BASE_COLORS, "Base Colors",
73+
sort_colors=False, emptycols=1)
74+
plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette",
75+
sort_colors=False, emptycols=2)
76+
77+
#sphinx_gallery_thumbnail_number = 3
78+
plot_colortable(mcolors.CSS4_COLORS, "CSS Colors")
79+
80+
# Optionally plot the XKCD colors (Caution: will produce large figure)
81+
#xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors")
82+
#xkcd_fig.savefig("XKCD_Colors.png")
5683

57-
fig.subplots_adjust(left=0, right=1,
58-
top=1, bottom=0,
59-
hspace=0, wspace=0)
6084
plt.show()
6185

86+
6287
#############################################################################
6388
#
6489
# ------------

‎tutorials/colors/colors.py

Copy file name to clipboardExpand all lines: tutorials/colors/colors.py
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ def demo(sty):
7777
# and third colors of each style's ``mpl.rcParams['axes.prop_cycle']``.
7878
#
7979
#
80+
# .. _xkcd-colors:
81+
#
8082
# xkcd v X11/CSS4
8183
# ---------------
8284
#

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.