Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff 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 deletions galleries/examples/color/individual_colors_from_cmap.py
Original file line number Diff line number Diff 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 i in range(n_lines):
ax.plot([0, i], color=cmap(i))
for i, color in enumerate(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.

with mpl.rc_context({'axes.prop_cycle': cycler(color=colors)}):
colors = mpl.colormaps['Dark2'].colors
Copy link
Member Author

@rcomer rcomer Jan 26, 2024

Choose a reason for hiding this comment

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

#27678 started out using the colors attribute but @jklymak said it could be confusing as LinearSegmentedColormap doesn't have it. Then @timhoffm suggested using it. I am hoping that using it after specifically stating that it's on ListedColormaps 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?

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 the ListedColormap.colors explanation and the the rc_context into one plot. That's confusing. Either drop the rc_context, or make it a separate plot.


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

for i in range(n_lines):
ax.plot([0, i])
for i, color in enumerate(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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy