From f80b03f22b10f6560fe01aebf6fac21bbb0a1280 Mon Sep 17 00:00:00 2001 From: hannah Date: Sat, 3 Feb 2024 19:13:40 -0500 Subject: [PATCH] Backport PR #27708: DOC: update colors from colormaps example --- .flake8 | 1 - .../color/individual_colors_from_cmap.py | 46 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/.flake8 b/.flake8 index f6d6b8d8c6df..ee739cdf4231 100644 --- a/.flake8 +++ b/.flake8 @@ -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 diff --git a/galleries/examples/color/individual_colors_from_cmap.py b/galleries/examples/color/individual_colors_from_cmap.py index 572fb33e6f11..1a14bd6b2ae1 100644 --- a/galleries/examples/color/individual_colors_from_cmap.py +++ b/galleries/examples/color/individual_colors_from_cmap.py @@ -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 - 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 #