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 dd8dbca

Browse filesBrowse files
authored
Merge pull request matplotlib#29563 from rcomer/doc-color-sequences
DOC: add color sequences reference example
2 parents 46ea8f0 + c0cd916 commit dd8dbca
Copy full SHA for dd8dbca

File tree

Expand file treeCollapse file tree

3 files changed

+69
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

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

‎galleries/examples/color/individual_colors_from_cmap.py

Copy file name to clipboardExpand all lines: galleries/examples/color/individual_colors_from_cmap.py
+3-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.