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

Backport PR #27708 on branch v3.8.x (DOC: update colors from colormaps example)#27742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion.flake8
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -73,7 +73,6 @@ per-file-ignores =
galleries/users_explain/text/text_props.py: E501

galleries/examples/animation/frame_grabbing_sgskip.py: E402
galleries/examples/color/individual_colors_from_cmap.py: E402
galleries/examples/images_contours_and_fields/tricontour_demo.py: E201
galleries/examples/images_contours_and_fields/tripcolor_demo.py: E201
galleries/examples/images_contours_and_fields/triplot_demo.py: E201
Expand Down
46 changes: 25 additions & 21 deletionsgalleries/examples/color/individual_colors_from_cmap.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,50 +7,54 @@
cycle provides. Selecting individual colors from one of the provided colormaps can be a
convenient way to do this.

Once we have hold of a `.Colormap` instance, the individual colors can be accessed
by passing it an index. If we want a specific number of colors taken at regular
intervals from a continuous colormap, we can create a new colormap using the
`~.Colormap.resampled` method.
We can retrieve colors from any `.Colormap` by calling it with a float or a list of
floats in the range [0, 1]; e.g. ``cmap(0.5)`` will give the middle color. See also
`.Colormap.__call__`.

For more details about manipulating colormaps, see :ref:`colormap-manipulation`.
Extracting colors from a continuous colormap
--------------------------------------------
"""

import matplotlib.pyplot as plt
import numpy as np

import matplotlib as mpl

n_lines = 21
cmap = mpl.colormaps['plasma']

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

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

for iinrange(n_lines):
ax.plot([0, i], color=cmap(i))
for i, colorinenumerate(colors):
ax.plot([0, i], color=color)

plt.show()

# %%
# Instead of passing colors one by one to `~.Axes.plot`, we can replace the default
# color cycle with a different set of colors. Specifying a `~cycler.cycler` instance
# within `.rcParams` achieves that. See :ref:`color_cycle` for details.


from cycler import cycler

cmap = mpl.colormaps.get_cmap('Dark2')
colors = cmap(range(cmap.N)) # cmap.N is number of unique colors in the colormap
#
# Extracting colors from a discrete colormap
# ------------------------------------------
# The list of all colors in a `.ListedColormap` is available as the ``colors``
# attribute.

withmpl.rc_context({'axes.prop_cycle': cycler(color=colors)}):
colors =mpl.colormaps['Dark2'].colors

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

for iinrange(n_lines):
ax.plot([0, i])
for i, colorinenumerate(colors):
ax.plot([0, i], color=color)

plt.show()

# %%
# See Also
# --------
#
# For more details about manipulating colormaps, see :ref:`colormap-manipulation`. To
# change the default color cycle, see :ref:`color_cycle`.
#
# .. admonition:: References
#
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp