8000 Backport PR #27678 on branch v3.8.2-doc (DOC: selecting individual colors from a colormap) by meeseeksmachine · Pull Request #27679 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backport PR #27678 on branch v3.8.2-doc (DOC: selecting individual colors from a colormap) #27679

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
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: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ 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
61 changes: 61 additions & 0 deletions galleries/examples/color/individual_colors_from_cmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
===========================================
Selecting individual colors from a colormap
===========================================

Sometimes we want to use more colors or a different set of colors than the default color
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.

For more details about manipulating colormaps, see :ref:`colormap-manipulation`.
"""

import matplotlib.pyplot as plt

import matplotlib as mpl

n_lines = 21

cmap = mpl.colormaps.get_cmap('plasma').resampled(n_lines)

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

for i in range(n_lines):
ax.plot([0, i], color=cmap(i))

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

with mpl.rc_context({'axes.prop_cycle': cycler(color=colors)}):

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

for i in range(n_lines):
ax.plot([0, i])

plt.show()

# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.colors.Colormap`
# - `matplotlib.colors.Colormap.resampled`
0