8000 DOC: add color sequences reference example · matplotlib/matplotlib@c2ae704 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2ae704

Browse files
committed
DOC: add color sequences reference example
1 parent 46ea8f0 commit c2ae704

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
=====================
3+
Named Color Sequences
4+
=====================
5+
6+
Matplotlib's `~matplotlib.colors.ColorSequenceRegistry` allows access to
7+
predefined lists of colors by name e.g.
8+
``colors = matplotlib.color_sequences['Set1']``. This example shows all of the
9+
built in color sequences.
10+
"""
11+
12+
import matplotlib.pyplot as plt
13+
import numpy as np
14+
15+
import matplotlib as mpl
16+
17+
18+
def plot_color_sequences(names, ax):
19+
# Display each named color sequence horizontally on the supplied axes.
20+
21+
for n, name in enumerate(names):
22+
colors = mpl.color_sequences[name]
23+
n_colors = len(colors)
24+
x = np.arange(n_colors)
25+
y = np.full_like(x, n)
26+
27+
ax.scatter(x, y, facecolor=colors, edgecolor='dimgray', s=200, zorder=2)
28+
29+
ax.set_yticks(range(len(names)), labels=names)
30+
ax.grid(visible=True, axis='y')
31+
ax.yaxis.set_inverted(True)
32+
ax.xaxis.set_visible(False)
33+
34+
35+
built_in_color_sequences = [
36+
'tab10', 'tab20', 'tab20b', 'tab20c', 'Pastel1', 'Pastel2', 'Paired',
37+
'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'petroff10']
38+
39+
40+
fig, ax = plt.subplots(figsize=(6.4, 9.6), layout='constrained')
41+
42+
plot_color_sequences(built_in_color_sequences, ax)
43+
ax.set_title('Built In Color Sequences')
44+
45+
plt.show()
46+
47+
48+
# %%
49+
#
50+
# .. admonition:: References
51+
#
52+
# The use of the following functions, methods, classes and modules is shown
53+
# in this example:
54+
#
55+
# - `matplotlib.colors.ColorSequenceRegistry`
56+
# - `matplotlib.axes.Axes.scatter`
57+
#
58+
# .. tags::
59+
#
60+
# styling: color
61+
# purpose: reference

galleries/examples/color/individual_colors_from_cmap.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
# Extracting colors from a discrete colormap
3939
# ------------------------------------------
4040
# The list of all colors in a `.ListedColormap` is available as the ``colors``
41-
# attribute.
41+
# attribute. Note that all the colors from Matplotlib's qualitative color maps
42+
# are also available as color sequences, so may be accessed more directly from
43+
# the color sequence registry. See :doc:`/gallery/color/color_sequences`.
4244

4345
colors = mpl.colormaps['Dark2'].colors
4446

lib/matplotlib/colors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class ColorSequenceRegistry(Mapping):
108108
import matplotlib as mpl
109109
colors = mpl.color_sequences['tab10']
110110
111+
For a list of built in color sequences, see :doc:`/gallery/color/color_sequences`.
111112
The returned lists are copies, so that their modification does not change
112113
the global definition of the color sequence.
113114

0 commit comments

Comments
 (0)
0