Skip to content

Commit 525349b

Browse files
authored
Merge pull request matplotlib#27708 from rcomer/color-from-cmap-2
DOC: update colors from colormaps example
2 parents 26832df + cca84fc commit 525349b

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

Diff for: .flake8

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ per-file-ignores =
6363
galleries/users_explain/text/text_props.py: E501
6464

6565
galleries/examples/animation/frame_grabbing_sgskip.py: E402
66-
galleries/examples/color/individual_colors_from_cmap.py: E402
6766
galleries/examples/images_contours_and_fields/tricontour_demo.py: E201
6867
galleries/examples/images_contours_and_fields/tripcolor_demo.py: E201
6968
galleries/examples/images_contours_and_fields/triplot_demo.py: E201

Diff for: galleries/examples/color/individual_colors_from_cmap.py

+25-21
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)