8000 Merge pull request #22912 from larsoner/colors · matplotlib/matplotlib@9c590de · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c590de

Browse files
authored
Merge pull request #22912 from larsoner/colors
DOC: Better doc of colors
2 parents e73ce5a + ab990e4 commit 9c590de

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

examples/color/named_colors.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
List of named colors
44
====================
55
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.
9-
6+
This plots a list of the named colors supported in matplotlib.
107
For more information on colors in matplotlib see
118
129
* the :doc:`/tutorials/colors/colors` tutorial;
1310
* the `matplotlib.colors` API;
1411
* the :doc:`/gallery/color/color_demo`.
12+
13+
----------------------------
14+
Helper Function for Plotting
15+
----------------------------
16+
First we define a helper function for making a table of colors, then we use it
17+
on some common color categories.
1518
"""
1619

1720
from matplotlib.patches import Rectangle
1821
import matplotlib.pyplot as plt
1922
import matplotlib.colors as mcolors
2023

2124

22-
def plot_colortable(colors, title, sort_colors=True, emptycols=0):
25+
def plot_colortable(colors, sort_colors=True, emptycols=0):
2326

2427
cell_width = 212
2528
cell_height = 22
2629
swatch_width = 48
2730
margin = 12
28-
topmargin = 40
2931

3032
# Sort colors by hue, saturation, value and name.
3133
if sort_colors is True:
@@ -41,18 +43,17 @@ def plot_colortable(colors, title, sort_colors=True, emptycols=0):
4143
nrows = n // ncols + int(n % ncols > 0)
4244

4345
width = cell_width * 4 + 2 * margin
44-
height = cell_height * nrows + margin + topmargin
46+
height = cell_height * nrows + 2 * margin
4547
dpi = 72
4648

4749
fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
4850
fig.subplots_adjust(margin/width, margin/height,
49-
(width-margin)/width, (height-topmargin)/height)
51+
(width-margin)/width, (height-margin)/height)
5052
ax.set_xlim(0, cell_width * 4)
5153
ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
5254
ax.yaxis.set_visible(False)
5355
ax.xaxis.set_visible(False)
5456
ax.set_axis_off()
55-
ax.set_title(title, fontsize=24, loc="left", pad=10)
5657

5758
for i, name in enumerate(names):
5859
row = i % nrows
@@ -73,22 +74,38 @@ def plot_colortable(colors, title, sort_colors=True, emptycols=0):
7374

7475
return fig
7576

76-
plot_colortable(mcolors.BASE_COLORS, "Base Colors",
77-
sort_colors=False, emptycols=1)
78-
plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette",
79-
sort_colors=False, emptycols=2)
77+
#############################################################################
78+
# -----------
79+
# Base colors
80+
# -----------
8081

81-
# sphinx_gallery_thumbnail_number = 3
82-
plot_colortable(mcolors.CSS4_COLORS, "CSS Colors")
82+
plot_colortable(mcolors.BASE_COLORS, sort_colors=False, emptycols=1)
8383

84-
# Optionally plot the XKCD colors (Caution: will produce large figure)
85-
# xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors")
86-
# xkcd_fig.savefig("XKCD_Colors.png")
84+
#############################################################################
85+
# ---------------
86+
# Tableau Palette
87+
# ---------------
8788

88-
plt.show()
89+
plot_colortable(mcolors.TABLEAU_COLORS, sort_colors=False, emptycols=2)
8990

91+
#############################################################################
92+
# ----------
93+
# CSS Colors
94+
# ----------
95+
96+
# sphinx_gallery_thumbnail_number = 3
97+
plot_colortable(mcolors.CSS4_COLORS)
98+
plt.show()
9099

91100
#############################################################################
101+
# -----------
102+
# XKCD Colors
103+
# -----------
104+
# XKCD colors are supported, but they produce a large figure, so we skip them
105+
# for now. You can use the following code if desired::
106+
#
107+
# xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors")
108+
# xkcd_fig.savefig("XKCD_Colors.png")
92109
#
93110
# .. admonition:: References
94111
#

0 commit comments

Comments
 (0)
0