.. 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 ` 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-24 This works on both axes: .. GENERATED FROM PYTHON SOURCE LINES 24-35 .. 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 .. _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 ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: categorical_variables.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_