
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/color/named_colors.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_named_colors.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_color_named_colors.py:


====================
List of named colors
====================

This plots a list of the named colors supported by Matplotlib.
For more information on colors in matplotlib see

* the :ref:`colors_def` tutorial;
* the `matplotlib.colors` API;
* the :doc:`/gallery/color/color_demo`.

----------------------------
Helper Function for Plotting
----------------------------
First we define a helper function for making a table of colors, then we use it
on some common color categories.

.. GENERATED FROM PYTHON SOURCE LINES 19-77

.. code-block:: Python


    import math

    import matplotlib.pyplot as plt

    import matplotlib.colors as mcolors
    from matplotlib.patches import Rectangle


    def plot_colortable(colors, *, ncols=4, sort_colors=True):

        cell_width = 212
        cell_height = 22
        swatch_width = 48
        margin = 12

        # Sort colors by hue, saturation, value and name.
        if sort_colors is True:
            names = sorted(
                colors, key=lambda c: tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(c))))
        else:
            names = list(colors)

        n = len(names)
        nrows = math.ceil(n / ncols)

        width = cell_width * ncols + 2 * margin
        height = cell_height * nrows + 2 * margin
        dpi = 72

        fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
        fig.subplots_adjust(margin/width, margin/height,
                            (width-margin)/width, (height-margin)/height)
        ax.set_xlim(0, cell_width * ncols)
        ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
        ax.yaxis.set_visible(False)
        ax.xaxis.set_visible(False)
        ax.set_axis_off()

        for i, name in enumerate(names):
            row = i % nrows
            col = i // nrows
            y = row * cell_height

            swatch_start_x = cell_width * col
            text_pos_x = cell_width * col + swatch_width + 7

            ax.text(text_pos_x, y, name, fontsize=14,
                    horizontalalignment='left',
                    verticalalignment='center')

            ax.add_patch(
                Rectangle(xy=(swatch_start_x, y-9), width=swatch_width,
                          height=18, facecolor=colors[name], edgecolor='0.7')
            )

        return fig








.. GENERATED FROM PYTHON SOURCE LINES 78-81

-----------
Base colors
-----------

.. GENERATED FROM PYTHON SOURCE LINES 81-84

.. code-block:: Python


    plot_colortable(mcolors.BASE_COLORS, ncols=3, sort_colors=False)




.. image-sg:: /gallery/color/images/sphx_glr_named_colors_001.png
   :alt: named colors
   :srcset: /gallery/color/images/sphx_glr_named_colors_001.png, /gallery/color/images/sphx_glr_named_colors_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 85-88

---------------
Tableau Palette
---------------

.. GENERATED FROM PYTHON SOURCE LINES 88-91

.. code-block:: Python


    plot_colortable(mcolors.TABLEAU_COLORS, ncols=2, sort_colors=False)




.. image-sg:: /gallery/color/images/sphx_glr_named_colors_002.png
   :alt: named colors
   :srcset: /gallery/color/images/sphx_glr_named_colors_002.png, /gallery/color/images/sphx_glr_named_colors_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 92-95

----------
CSS Colors
----------

.. GENERATED FROM PYTHON SOURCE LINES 95-99

.. code-block:: Python


    plot_colortable(mcolors.CSS4_COLORS)
    plt.show()




.. image-sg:: /gallery/color/images/sphx_glr_named_colors_003.png
   :alt: named colors
   :srcset: /gallery/color/images/sphx_glr_named_colors_003.png, /gallery/color/images/sphx_glr_named_colors_003_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 101-129

-----------
XKCD Colors
-----------
Matplotlib supports colors from the
`xkcd color survey <https://xkcd.com/color/rgb/>`_, e.g. ``"xkcd:sky blue"``. Since
this contains almost 1000 colors, a figure of this would be very large and is thus
omitted here. You can use the following code to generate the overview yourself ::

    xkcd_fig = plot_colortable(mcolors.XKCD_COLORS)
    xkcd_fig.savefig("XKCD_Colors.png")

.. admonition:: References

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

   - `matplotlib.colors`
   - `matplotlib.colors.rgb_to_hsv`
   - `matplotlib.colors.to_rgba`
   - `matplotlib.figure.Figure.get_size_inches`
   - `matplotlib.figure.Figure.subplots_adjust`
   - `matplotlib.axes.Axes.text`
   - `matplotlib.patches.Rectangle`

.. tags::

   styling: color
   purpose: reference


.. _sphx_glr_download_gallery_color_named_colors.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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