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

Commitd39e083

Browse files
committed
DOC: selecting individual colors from a colormap [ci doc]
1 parent879bde7 commitd39e083

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

‎.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ 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
6667
galleries/examples/images_contours_and_fields/tricontour_demo.py: E201
6768
galleries/examples/images_contours_and_fields/tripcolor_demo.py: E201
6869
galleries/examples/images_contours_and_fields/triplot_demo.py: E201
Lines 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+
16+
importmatplotlib.pyplotasplt
17+
18+
importmatplotlibasmpl
19+
20+
n_lines=21
21+
22+
cmap=mpl.colormaps.get_cmap('plasma').resampled(n_lines)
23+
24+
fig,ax=plt.subplots(layout='constrained')
25+
26+
foriinrange(n_lines):
27+
ax.plot([0,i],color=cmap(i))
28+
29+
plt.show()
30+
31+
# %%
32+
# Alternatively, we may want to replace the default color cycle with a different
33+
# discrete set of colors. We can use a `~cycler.cycler` instance within `.rcParams` to
34+
# achieve that. See :ref:`color_cycle` for details.
35+
36+
37+
fromcyclerimportcycler
38+
39+
cmap=mpl.colormaps.get_cmap('Dark2')
40+
colors=cmap(range(cmap.N))
41+
42+
withmpl.rc_context({'axes.prop_cycle':cycler(color=colors)}):
43+
44+
fig,ax=plt.subplots(layout='constrained')
45+
46+
foriinrange(n_lines):
47+
ax.plot([0,i])
48+
49+
plt.show()
50+
51+
# %%
52+
#
53+
# .. admonition:: References
54+
#
55+
# The use of the following functions, methods, classes and modules is shown
56+
# in this example:
57+
#
58+
# - `matplotlib.colormaps.get_cmap`
59+
# - `matplotlib.colors.Colormap`
60+
# - `matplotlib.colors.Colormap.resampled`
61+
# - `maplotlib.rc_context`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp