From 95dbab0e4b8dba8d4d450094b0f005eca354c4f6 Mon Sep 17 00:00:00 2001 From: Ruth Comer <10599679+rcomer@users.noreply.github.com> Date: Sat, 20 Jan 2024 13:50:21 +0000 Subject: [PATCH] DOC: selecting individual colors from a colormap [ci doc] --- .flake8 | 1 + .../color/individual_colors_from_cmap.py | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 galleries/examples/color/individual_colors_from_cmap.py diff --git a/.flake8 b/.flake8 index 40c07e0e324b..bfb5bb29b0e9 100644 --- a/.flake8 +++ b/.flake8 @@ -63,6 +63,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 diff --git a/galleries/examples/color/individual_colors_from_cmap.py b/galleries/examples/color/individual_colors_from_cmap.py new file mode 100644 index 000000000000..572fb33e6f11 --- /dev/null +++ b/galleries/examples/color/individual_colors_from_cmap.py @@ -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` 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