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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_gallery_lines_bars_and_markers_bar_label_demo.py>`
        to download the full example code

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

.. _sphx_glr_gallery_lines_bars_and_markers_bar_label_demo.py:


==============
Bar Label Demo
==============

This example shows how to use the `~.Axes.bar_label` helper function
to create bar chart labels.

See also the :doc:`grouped bar
</gallery/lines_bars_and_markers/barchart>`,
:doc:`stacked bar
</gallery/lines_bars_and_markers/bar_stacked>` and
:doc:`horizontal bar chart
</gallery/lines_bars_and_markers/barh>` examples.

.. GENERATED FROM PYTHON SOURCE LINES 16-20

.. code-block:: default


    import matplotlib.pyplot as plt
    import numpy as np








.. GENERATED FROM PYTHON SOURCE LINES 21-22

Define the data

.. GENERATED FROM PYTHON SOURCE LINES 22-31

.. code-block:: default


    N = 5
    menMeans = (20, 35, 30, 35, -27)
    womenMeans = (25, 32, 34, 20, -25)
    menStd = (2, 3, 4, 1, 2)
    womenStd = (3, 5, 2, 3, 3)
    ind = np.arange(N)    # the x locations for the groups
    width = 0.35       # the width of the bars: can also be len(x) sequence








.. GENERATED FROM PYTHON SOURCE LINES 32-33

Stacked bar plot with error bars

.. GENERATED FROM PYTHON SOURCE LINES 33-53

.. code-block:: default


    fig, ax = plt.subplots()

    p1 = ax.bar(ind, menMeans, width, yerr=menStd, label='Men')
    p2 = ax.bar(ind, womenMeans, width,
                bottom=menMeans, yerr=womenStd, label='Women')

    ax.axhline(0, color='grey', linewidth=0.8)
    ax.set_ylabel('Scores')
    ax.set_title('Scores by group and gender')
    ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5'])
    ax.legend()

    # Label with label_type 'center' instead of the default 'edge'
    ax.bar_label(p1, label_type='center')
    ax.bar_label(p2, label_type='center')
    ax.bar_label(p2)

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_001.png
   :alt: Scores by group and gender
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_001.png, /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_001_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 54-55

Horizontal bar chart

.. GENERATED FROM PYTHON SOURCE LINES 55-79

.. code-block:: default


    # Fixing random state for reproducibility
    np.random.seed(19680801)

    # Example data
    people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
    y_pos = np.arange(len(people))
    performance = 3 + 10 * np.random.rand(len(people))
    error = np.random.rand(len(people))

    fig, ax = plt.subplots()

    hbars = ax.barh(y_pos, performance, xerr=error, align='center')
    ax.set_yticks(y_pos, labels=people)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('Performance')
    ax.set_title('How fast do you want to go today?')

    # Label with specially formatted floats
    ax.bar_label(hbars, fmt='%.2f')
    ax.set_xlim(right=15)  # adjust xlim to fit labels

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_002.png
   :alt: How fast do you want to go today?
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_002.png, /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_002_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 80-81

Some of the more advanced things that one can do with bar labels

.. GENERATED FROM PYTHON SOURCE LINES 81-97

.. code-block:: default


    fig, ax = plt.subplots()

    hbars = ax.barh(y_pos, performance, xerr=error, align='center')
    ax.set_yticks(y_pos, labels=people)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('Performance')
    ax.set_title('How fast do you want to go today?')

    # Label with given captions, custom padding and annotate options
    ax.bar_label(hbars, labels=['±%.2f' % e for e in error],
                 padding=8, color='b', fontsize=14)
    ax.set_xlim(right=16)

    plt.show()




.. image-sg:: /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_003.png
   :alt: How fast do you want to go today?
   :srcset: /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_003.png, /gallery/lines_bars_and_markers/images/sphx_glr_bar_label_demo_003_2_0x.png 2.0x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 98-106

.. admonition:: References

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

   - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar`
   - `matplotlib.axes.Axes.barh` / `matplotlib.pyplot.barh`
   - `matplotlib.axes.Axes.bar_label` / `matplotlib.pyplot.bar_label`


.. _sphx_glr_download_gallery_lines_bars_and_markers_bar_label_demo.py:


.. only :: html

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



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

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



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

     :download:`Download Jupyter notebook: bar_label_demo.ipynb <bar_label_demo.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>`_
