Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit525349b

Browse files
authored
Merge pull request#27708 from rcomer/color-from-cmap-2
DOC: update colors from colormaps example
2 parents26832df +cca84fc commit525349b

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

‎.flake8

Lines changed: 0 additions & 1 deletion
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

‎galleries/examples/color/individual_colors_from_cmap.py

Lines 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
importmatplotlib.pyplotasplt
19+
importnumpyasnp
1920

2021
importmatplotlibasmpl
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-
foriinrange(n_lines):
29-
ax.plot([0,i],color=cmap(i))
31+
fori,colorinenumerate(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-
fromcyclerimportcycler
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-
withmpl.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-
foriinrange(n_lines):
49-
ax.plot([0,i])
47+
fori,colorinenumerate(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)

[8]ページ先頭

©2009-2025 Movatter.jp