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

    Click :ref:`here <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:


========================
Visualizing named colors
========================

This plots a list of the named colors supported in matplotlib. Note that
:ref:`xkcd colors <xkcd-colors>` are supported as well, but are not listed here
for brevity.

For more information on colors in matplotlib see

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



.. code-block:: python


    import matplotlib.pyplot as plt
    import matplotlib.colors as mcolors


    def plot_colortable(colors, title, sort_colors=True, emptycols=0):

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

        # Sort colors by hue, saturation, value and name.
        by_hsv = ((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
                        for name, color in colors.items())
        if sort_colors is True:
            by_hsv = sorted(by_hsv)
        names = [name for hsv, name in by_hsv]

        n = len(names)
        ncols = 4 - emptycols
        nrows = n // ncols + int(n % ncols > 0)

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

        fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
        fig.subplots_adjust(margin/width, margin/height,
                            (width-margin)/width, (height-topmargin)/height)
        ax.set_xlim(0, cell_width * 4)
        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()
        ax.set_title(title, fontsize=24, loc="left", pad=10)

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

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

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

            ax.hlines(y, swatch_start_x, swatch_end_x,
                      color=colors[name], linewidth=18)

        return fig

    plot_colortable(mcolors.BASE_COLORS, "Base Colors",
                    sort_colors=False, emptycols=1)
    plot_colortable(mcolors.TABLEAU_COLORS, "Tableau Palette",
                    sort_colors=False, emptycols=2)

    #sphinx_gallery_thumbnail_number = 3
    plot_colortable(mcolors.CSS4_COLORS, "CSS Colors")

    # Optionally plot the XKCD colors (Caution: will produce large figure)
    #xkcd_fig = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors")
    #xkcd_fig.savefig("XKCD_Colors.png")

    plt.show()





.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /gallery/color/images/sphx_glr_named_colors_001.png
            :class: sphx-glr-multi-img

    *

      .. image:: /gallery/color/images/sphx_glr_named_colors_002.png
            :class: sphx-glr-multi-img

    *

      .. image:: /gallery/color/images/sphx_glr_named_colors_003.png
            :class: sphx-glr-multi-img




------------

References
""""""""""

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



.. code-block:: python


    import matplotlib
    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.axes.Axes.hlines







.. _sphx_glr_download_gallery_color_named_colors.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
