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 6755d50

Browse filesBrowse files
authored
Merge pull request #27680 from meeseeksmachine/auto-backport-of-pr-27678-on-v3.8.x
Backport PR #27678 on branch v3.8.x (DOC: selecting individual colors from a colormap)
2 parents 82c9087 + 47b5ba3 commit 6755d50
Copy full SHA for 6755d50

File tree

Expand file treeCollapse file tree

2 files changed

+62
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+62
-0
lines changed

‎.flake8

Copy file name to clipboardExpand all lines: .flake8
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ per-file-ignores =
7373
galleries/users_explain/text/text_props.py: E501
7474

7575
galleries/examples/animation/frame_grabbing_sgskip.py: E402
76+
galleries/examples/color/individual_colors_from_cmap.py: E402
7677
galleries/examples/images_contours_and_fields/tricontour_demo.py: E201
7778
galleries/examples/images_contours_and_fields/tripcolor_demo.py: E201
7879
galleries/examples/images_contours_and_fields/triplot_demo.py: E201
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
===========================================
3+
Selecting individual colors from a colormap
4+
===========================================
5+
6+
Sometimes we want to use more colors or a different set of colors than the default color
7+
cycle provides. Selecting individual colors from one of the provided colormaps can be a
8+
convenient way to do this.
9+
10+
Once we have hold of a `.Colormap` instance, the individual colors can be accessed
11+
by passing it an index. If we want a specific number of colors taken at regular
12+
intervals from a continuous colormap, we can create a new colormap using the
13+
`~.Colormap.resampled` method.
14+
15+
For more details about manipulating colormaps, see :ref:`colormap-manipulation`.
16+
"""
17+
18+
import matplotlib.pyplot as plt
19+
20+
import matplotlib as mpl
21+
22+
n_lines = 21
23+
24+
cmap = mpl.colormaps.get_cmap('plasma').resampled(n_lines)
25+
26+
fig, ax = plt.subplots(layout='constrained')
27+
28+
for i in range(n_lines):
29+
ax.plot([0, i], color=cmap(i))
30+
31+
plt.show()
32+
33+
# %%
34+
# Instead of passing colors one by one to `~.Axes.plot`, we can replace the default
35+
# color cycle with a different set of colors. Specifying a `~cycler.cycler` instance
36+
# within `.rcParams` achieves that. See :ref:`color_cycle` for details.
37+
38+
39+
from cycler import cycler
40+
41+
cmap = mpl.colormaps.get_cmap('Dark2')
42+
colors = cmap(range(cmap.N)) # cmap.N is number of unique colors in the colormap
43+
44+
with mpl.rc_context({'axes.prop_cycle': cycler(color=colors)}):
45+
46+
fig, ax = plt.subplots(layout='constrained')
47+
48+
for i in range(n_lines):
49+
ax.plot([0, i])
50+
51+
plt.show()
52+
53+
# %%
54+
#
55+
# .. admonition:: References
56+
#
57+
# The use of the following functions, methods, classes and modules is shown
58+
# in this example:
59+
#
60+
# - `matplotlib.colors.Colormap`
61+
# - `matplotlib.colors.Colormap.resampled`

0 commit comments

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