
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/color/individual_colors_from_cmap.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_color_individual_colors_from_cmap.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_color_individual_colors_from_cmap.py:


===========================================
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.

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__`.

Extracting colors from a continuous colormap
--------------------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 17-36

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    import matplotlib as mpl

    n_lines = 21
    cmap = mpl.colormaps['plasma']

    # Take colors at regular intervals spanning the colormap.
    colors = cmap(np.linspace(0, 1, n_lines))

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

    for i, color in enumerate(colors):
        ax.plot([0, i], color=color)

    plt.show()




.. image-sg:: /gallery/color/images/sphx_glr_individual_colors_from_cmap_001.png
   :alt: individual colors from cmap
   :srcset: /gallery/color/images/sphx_glr_individual_colors_from_cmap_001.png, /gallery/color/images/sphx_glr_individual_colors_from_cmap_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 37-43

Extracting colors from a discrete colormap
------------------------------------------
The list of all colors in a `.ListedColormap` is available as the ``colors``
attribute.  Note that all the colors from Matplotlib's qualitative color maps
are also available as color sequences, so may be accessed more directly from
the color sequence registry.  See :doc:`/gallery/color/color_sequences`.

.. GENERATED FROM PYTHON SOURCE LINES 44-54

.. code-block:: Python


    colors = mpl.colormaps['Dark2'].colors

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

    for i, color in enumerate(colors):
        ax.plot([0, i], color=color)

    plt.show()




.. image-sg:: /gallery/color/images/sphx_glr_individual_colors_from_cmap_002.png
   :alt: individual colors from cmap
   :srcset: /gallery/color/images/sphx_glr_individual_colors_from_cmap_002.png, /gallery/color/images/sphx_glr_individual_colors_from_cmap_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 55-75

See Also
--------

For more details about manipulating colormaps, see :ref:`colormap-manipulation`.  To
change the default color cycle, see :ref:`color_cycle`.

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.colors.Colormap`
   - `matplotlib.colors.Colormap.resampled`

.. tags::

   component: colormap
   styling: color
   plot-type: line
   level: intermediate


.. _sphx_glr_download_gallery_color_individual_colors_from_cmap.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: individual_colors_from_cmap.ipynb <individual_colors_from_cmap.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: individual_colors_from_cmap.py <individual_colors_from_cmap.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: individual_colors_from_cmap.zip <individual_colors_from_cmap.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
