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

DOC: update colors from colormaps example#27708

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
story645 merged 1 commit intomatplotlib:mainfromrcomer:color-from-cmap-2
Feb 4, 2024
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@@ -63,7 +63,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
Copy link
MemberAuthor

@rcomerrcomerJan 26, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

#27678 started out using thecolors attribute but@jklymak said it could be confusing asLinearSegmentedColormap doesn't have it. Then@timhoffm suggested using it. I am hoping that using it after specifically stating that it's onListedColormaps is a reasonable compromise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

maybe turn that into a section heading to make it more obvious?

timhoffm reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Sectioning: The above should be something "Extracting values from a continuous colormap" (though technically most of the visually continous colormaps are still densely sampled ListedColormaps) and here "Extracting values from a discrete colormap".

I also suggest to not combine both theListedColormap.colors explanation and the therc_context into one plot. That's confusing. Either drop therc_context, or make it a separate plot.


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