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 de210ea

Browse filesBrowse files
authored
Merge pull request #27743 from meeseeksmachine/auto-backport-of-pr-27708-on-v3.8.2-doc
Backport PR #27708 on branch v3.8.2-doc (DOC: update colors from colormaps example)
2 parents ad01a81 + 5b26b11 commit de210ea
Copy full SHA for de210ea

File tree

Expand file treeCollapse file tree

2 files changed

+25
-22
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+25
-22
lines changed

‎.flake8

Copy file name to clipboardExpand all lines: .flake8
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ 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
7776
galleries/examples/images_contours_and_fields/tricontour_demo.py: E201
7877
galleries/examples/images_contours_and_fields/tripcolor_demo.py: E201
7978
galleries/examples/images_contours_and_fields/triplot_demo.py: E201

‎galleries/examples/color/individual_colors_from_cmap.py

Copy file name to clipboardExpand all lines: galleries/examples/color/individual_colors_from_cmap.py
+25-21Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,54 @@
77
cycle provides. Selecting individual colors from one of the provided colormaps can be a
88
convenient way to do this.
99
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.
10+
We can retrieve colors from any `.Colormap` by calling it with a float or a list of
11+
floats in the range [0, 1]; e.g. ``cmap(0.5)`` will give the middle color. See also
12+
`.Colormap.__call__`.
1413
15-
For more details about manipulating colormaps, see :ref:`colormap-manipulation`.
14+
Extracting colors from a continuous colormap
15+
--------------------------------------------
1616
"""
1717

1818
import matplotlib.pyplot as plt
19+
import numpy as np
1920

2021
import matplotlib as mpl
2122

2223
n_lines = 21
24+
cmap = mpl.colormaps['plasma']
2325

24-
cmap = mpl.colormaps.get_cmap('plasma').resampled(n_lines)
26+
# Take colors at regular intervals spanning the colormap.
27+
colors = cmap(np.linspace(0, 1, n_lines))
2528

2629
fig, ax = plt.subplots(layout='constrained')
2730

28-
for i in range(n_lines):
29-
ax.plot([0, i], color=cmap(i))
31+
for i, color in enumerate(colors):
32+
ax.plot([0, i], color=color)
3033

3134
plt.show()
3235

3336
# %%
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
37+
#
38+
# Extracting colors from a discrete colormap
39+
# ------------------------------------------
40+
# The list of all colors in a `.ListedColormap` is available as the ``colors``
41+
# attribute.
4342

44-
with mpl.rc_context({'axes.prop_cycle': cycler(color=colors)}):
43+
colors = mpl.colormaps['Dark2'].colors
4544

46-
fig, ax = plt.subplots(layout='constrained')
45+
fig, ax = plt.subplots(layout='constrained')
4746

48-
for i in range(n_lines):
49-
ax.plot([0, i])
47+
for i, color in enumerate(colors):
48+
ax.plot([0, i], color=color)
5049

5150
plt.show()
5251

5352
# %%
53+
# See Also
54+
# --------
55+
#
56+
# For more details about manipulating colormaps, see :ref:`colormap-manipulation`. To
57+
# change the default color cycle, see :ref:`color_cycle`.
5458
#
5559
# .. admonition:: References
5660
#

0 commit comments

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