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

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

.. _sphx_glr_gallery_lines_bars_and_markers_categorical_variables.py:


==============================
Plotting categorical variables
==============================

You can pass categorical values (i.e. strings) directly as x- or y-values to
many plotting functions:

.. GENERATED FROM PYTHON SOURCE LINES 9-22

.. code-block:: Python

    import matplotlib.pyplot as plt

    data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20}
    names = list(data.keys())
    values = list(data.values())

    fig, axs = plt.subplots(1, 3, figsize=(9, 3), sharey=True)
    axs[0].bar(names, values)
    axs[1].scatter(names, values)
    axs[2].plot(names, values)
    fig.suptitle('Categorical Plotting')





.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_categorical_variables_001.png
   :alt: Categorical Plotting
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_categorical_variables_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_categorical_variables_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 23-27

Categorical values are a mapping from names to positions. This means that
values that occur multiple times are mapped to the same position. See the
``cat`` and ``dog`` values "happy" and "bored" on the y-axis in the following
example.

.. GENERATED FROM PYTHON SOURCE LINES 27-39

.. code-block:: Python


    cat = ["bored", "happy", "bored", "bored", "happy", "bored"]
    dog = ["happy", "happy", "happy", "happy", "bored", "bored"]
    activity = ["combing", "drinking", "feeding", "napping", "playing", "washing"]

    fig, ax = plt.subplots()
    ax.plot(activity, dog, label="dog")
    ax.plot(activity, cat, label="cat")
    ax.legend()

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_categorical_variables_002.png
   :alt: categorical variables
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_categorical_variables_002.png, /gallery/lines_bars_and_markers/images/sphx_glr_categorical_variables_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 40-44

.. tags::

   plot-type: specialty
   level: beginner


.. _sphx_glr_download_gallery_lines_bars_and_markers_categorical_variables.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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