.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/1d_hist/1d_str_category.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. 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_example_gallery_1d_hist_1d_str_category.py: String categories ================= Plot a 1D histogram with string categories. .. GENERATED FROM PYTHON SOURCE LINES 7-42 .. image-sg:: /example_gallery/1d_hist/images/sphx_glr_1d_str_category_001.svg :alt: 1d str category :srcset: /example_gallery/1d_hist/images/sphx_glr_1d_str_category_001.svg :class: sphx-glr-single-img .. code-block:: Python ### import boost_histogram as bh import matplotlib.pyplot as plt from plothist import plot_hist # 3 str categories str_categories = ["a", "b", "c"] # String category axis with 3 bins axis = bh.axis.StrCategory(categories=str_categories) # 6 data points, data = ["a", "a", "a", "b", "b", "c"] # Create and fill the histogram h = bh.Histogram(axis, storage=bh.storage.Weight()) h.fill(data) # Plot the histogram fig, ax = plt.subplots() plot_hist(h, ax=ax) # Set the x-ticks to the middle of the bins and label them ax.set_xticks([i + 0.5 for i in range(len(str_categories))]) ax.set_xticklabels(str_categories) ax.minorticks_off() ax.set_xlabel("String Category") ax.set_ylabel("Entries") ax.set_xlim(0, len(str_categories)) fig.savefig("1d_str_category.svg", bbox_inches="tight") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.173 seconds) .. _sphx_glr_download_example_gallery_1d_hist_1d_str_category.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 1d_str_category.ipynb <1d_str_category.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 1d_str_category.py <1d_str_category.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 1d_str_category.zip <1d_str_category.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_