.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plotting/_bar.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plotting__bar.py: ================ 6.3 bar plot ================ .. GENERATED FROM PYTHON SOURCE LINES 7-18 .. code-block:: default # sphinx_gallery_thumbnail_number = 4 import numpy as np from easy_mpl import bar_chart, plot import matplotlib.pyplot as plt from easy_mpl.utils import version_info from easy_mpl.utils import despine_axes version_info() # print version information of all the packages being used .. rst-class:: sphx-glr-script-out .. code-block:: none {'easy_mpl': '0.21.4', 'matplotlib': '3.8.4', 'numpy': '1.26.4', 'pandas': '1.5.3', 'scipy': '1.13.1'} .. GENERATED FROM PYTHON SOURCE LINES 19-20 A basic chart requires just a list of values to represent as bars. .. GENERATED FROM PYTHON SOURCE LINES 20-22 .. code-block:: default _ = bar_chart([1,2,3,4,4,5,3,2,5]) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_001.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_001.png, /auto_examples/plotting/images/sphx_glr__bar_001_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 23-24 we can also provide a numpy array instead .. GENERATED FROM PYTHON SOURCE LINES 24-26 .. code-block:: default _ = bar_chart(np.array([1,2,3,4,4,5,3,2,5])) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_002.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_002.png, /auto_examples/plotting/images/sphx_glr__bar_002_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 27-28 setting the labels for axis .. GENERATED FROM PYTHON SOURCE LINES 28-31 .. code-block:: default _ = bar_chart([3,4,2,5,10], ['a', 'b', 'c', 'd', 'e']) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_003.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_003.png, /auto_examples/plotting/images/sphx_glr__bar_003_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 32-33 sorting the bars according to their values .. GENERATED FROM PYTHON SOURCE LINES 33-38 .. code-block:: default _ = bar_chart([1,2,3,4,4,5,3,2,5], ['a','b','c','d','e','f','g','h','i'], sort=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_004.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_004.png, /auto_examples/plotting/images/sphx_glr__bar_004_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-42 The default color of bars are chosen randomly. We can specify the color in many ways, e.g. a single color for all bars .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: default _ = bar_chart([1,2,3,4,4,5,3,2,5], color="salmon") .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_005.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_005.png, /auto_examples/plotting/images/sphx_glr__bar_005_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-46 adding bar labels .. GENERATED FROM PYTHON SOURCE LINES 46-53 .. code-block:: default _ = bar_chart( [1,2,3,4,4,5,3,2,5], ['a','b','c','d','e','f','g','h','i'], bar_labels=[11, 23, 12,43, 123, 12, 43, 234, 23], cmap="GnBu", sort=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_006.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_006.png, /auto_examples/plotting/images/sphx_glr__bar_006_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-55 putting bar labels outside the bar .. GENERATED FROM PYTHON SOURCE LINES 55-63 .. code-block:: default _ = bar_chart( [1,2,3,4,4,5,3,2,5], ['a','b','c','d','e','f','g','h','i'], bar_labels=[11, 23, 12,43, 123, 12, 43, 234, 23], bar_label_kws={'label_type':'edge'}, cmap="GnBu", sort=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_007.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_007.png, /auto_examples/plotting/images/sphx_glr__bar_007_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-65 vertical orientation .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: default _ = bar_chart([1,2,3,4,4,5,3,2,5], orient='v') .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_008.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_008.png, /auto_examples/plotting/images/sphx_glr__bar_008_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-69 define color for each bar individually .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: default _ = bar_chart([4,2,5,1,3], color=['#BD76B2', '#3BAAE2', '#2BB67B', '#9FA537', '#F5746F']) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_009.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_009.png, /auto_examples/plotting/images/sphx_glr__bar_009_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-73 error bars .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: default errors = [0.1, 0.2, 0.3, 0.24, 0.32, 0.11, 0.32, 0.12, 0.42] _ = bar_chart([1,2,3,4,4,5,3,2,5], errors=errors) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_010.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_010.png, /auto_examples/plotting/images/sphx_glr__bar_010_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-79 the function bar_chart returns matplotlib axes which can be used for further processing .. GENERATED FROM PYTHON SOURCE LINES 79-96 .. code-block:: default sv_bar = np.arange(20, 100, 10) names = [f"Feature {n}" for n in sv_bar] ax = bar_chart(sv_bar, names, bar_labels=sv_bar, bar_label_kws={'label_type':'edge'}, show=False, sort=True, cmap='summer_r') print(f"Type of ax is {type(ax)}") despine_axes(ax, keep=['bottom', 'left']) ax.set_xlabel(xlabel='mean(|SHAP value|)', fontsize=14, weight='bold') ax.set_xticklabels(ax.get_xticks().astype(int), size=12, weight='bold') ax.set_yticklabels(ax.get_yticklabels(), size=12, weight='bold') plt.tight_layout() plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_011.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_011.png, /auto_examples/plotting/images/sphx_glr__bar_011_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Type of ax is /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/plotting/_bar.py:91: UserWarning: set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator. ax.set_xticklabels(ax.get_xticks().astype(int), size=12, weight='bold') .. GENERATED FROM PYTHON SOURCE LINES 97-98 multipler bar charts .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: default data = np.random.randint(1, 10, (5, 2)) _ = bar_chart(data, color=['salmon', 'cadetblue']) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_012.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_012.png, /auto_examples/plotting/images/sphx_glr__bar_012_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-103 multipler bar charts on separate axes .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: default data = np.random.randint(0, 10, (5, 2)) _ = bar_chart(data, color=['salmon', 'cadetblue'], share_axes=False) .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_013.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_013.png, /auto_examples/plotting/images/sphx_glr__bar_013_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-108 specifying colors for group of bars .. GENERATED FROM PYTHON SOURCE LINES 108-141 .. code-block:: default colors = {'Asia': '#60AB7B', 'Europe': '#F9B234', 'Africa': '#E91B23'} continents = {'Pakistan': 'Asia', 'Iran': 'Asia', 'Syria': 'Asia', 'Iraq': 'Asia', 'Lebanon': 'Asia', 'Ireland': 'Europe', 'Germany': 'Europe', 'Norway': 'Europe', 'Ghana': 'Africa', 'Egypt': 'Africa', 'Moroco': 'Africa', 'Tunis': 'Africa'} data = [ 17.5, 21.6, 21.6, 47.7, 0.2, 15.2, 0.4 , 1.4, 60.6, 1.5, 11.8, 6.2] ax = bar_chart(data, list(continents.keys()), bar_labels=data, bar_label_kws={'label_type':'edge', 'fontsize': 10, 'weight': 'bold'}, show=False, sort=True, color=[colors[val] for val in continents.values()], ax_kws=dict(top_spine=False, right_spine=False)) ax.set_xticklabels(ax.get_xticks().astype(int), size=12, weight='bold') ax.set_yticklabels(ax.get_yticklabels(), size=12, weight='bold') labels = np.unique(list(continents.values())) handles = [plt.Rectangle((0,0),1,1, color=colors[l]) for l in labels] ax.xaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.7) ax.set(axisbelow=True) # Hide the grid behind plot objects ax.set_facecolor('floralwhite') plt.legend(handles, labels, loc='lower right') plt.tight_layout() plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_014.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_014.png, /auto_examples/plotting/images/sphx_glr__bar_014_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/python-seekho/checkouts/latest/scripts/plotting/_bar.py:129: UserWarning: set_ticklabels() should only be used with a fixed number of ticks, i.e. after set_ticks() or using a FixedLocator. ax.set_xticklabels(ax.get_xticks().astype(int), size=12, weight='bold') .. GENERATED FROM PYTHON SOURCE LINES 142-143 Stacked bar chart .. GENERATED FROM PYTHON SOURCE LINES 143-177 .. code-block:: default # Values of each group bars1 = [12, 28, 1, 8, 22] bars2 = [28, 7, 16, 4, 10] bars3 = [25, 3, 23, 25, 17] bars4 = [5, 11, 7, 3, 19] # Heights of bars1 + bars2 bars = np.add(bars1, np.add(bars2, bars3).tolist()).tolist() # Names of group and bar width names = ['A', 'B', 'C', 'D', 'E'] barWidth = 0.65 bar_chart(bars1, color='#006db6', edgecolor='white', width=barWidth, orient='v', show=False) bar_chart(bars2, bottom=bars1, color='#f8aa59', edgecolor='white', width=barWidth, orient='v', show=False) bar_chart(bars3, bottom=np.add(bars1, bars2).tolist(), color='#eb5c23', edgecolor='white', width=barWidth, orient='v', show=False) bar_chart(bars4, bottom=bars, color='#b72c10', edgecolor='white', width=barWidth, orient='v', show=False) # Custom X axis plt.xticks([0, 1, 2, 3, 4], names, fontweight='bold') plt.xlabel("group") plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_015.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_015.png, /auto_examples/plotting/images/sphx_glr__bar_015_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 178-179 negative values in the data .. GENERATED FROM PYTHON SOURCE LINES 179-207 .. code-block:: default names = ['JAN','FEB','MAR','APR','MAY','JUN', 'JUL','AUG','SEP','OCT','NOV','DEC'] temp_max = [-1,0,5,12,18,24,27,26,21,14,8,2] temp_min = [-7,-6,-2,4,10,15,18,17,13,7,2,-3] def listOfTuples(l1, l2): return list(map(lambda x, y:(x,y), l1, l2)) temp = listOfTuples(temp_min, temp_max) f, ax = plt.subplots(facecolor = "#EFE9E6") bar_chart(temp, color=['#c9807d', '#22a1bd'], orient='v', labels=names, ax=ax, show=False) ax.grid(axis='y', ls='dotted', color='lightgrey') for spine in ax.spines.values(): spine.set_edgecolor('lightgrey') spine.set_linestyle('dashed') plt.legend(['Minimum temperature', 'Maximum temperature']) plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_016.png :alt: bar :srcset: /auto_examples/plotting/images/sphx_glr__bar_016.png, /auto_examples/plotting/images/sphx_glr__bar_016_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 208-209 Displaying nagative values with a specific color .. GENERATED FROM PYTHON SOURCE LINES 209-237 .. code-block:: default data = [-0.4, -0.5, 0.1, -2, 0.6, 0.2, -0.5, -1, -1.2, -0.7, -0.6, -0.6, 0.2, -0.2, 0, 0.6, -2.3, -0.6, 0.2, -1.1, -0.3, -2.1, -0.8, 0.4, -1.5, 1.3, 0.2, -0.3, -1, 0.8, -0.5, 0, -0.2, -0.9, 0.2, 0.6, 0.8, 0, 2.1, 0.7, -0.2, -0.4, 0.9, 0.9, 0.2, 0.4, 0.1, 0.3, -0.2, -0.1, 0.4, 0, -0.2, -0.4, -0.5, -0.3, 0, 0.7, 1.4, 0.3, -0.3, 0.3, -0.2, 0.3, -0.6, 0.1, -0.7, 0.4, -0.1, -0.9, 0, -0.2, -0.6, -0.5, -0.5, -0.7, 0.2, -0.7, 0.5, -0.7, 0.5, -0.4, -0.6, -1.6, 0.5, 1.1, -0.6, 0.4, -0.6, -0.1, 0.7, 1.2, 0.7, 0.3, 1.1, 1.1, 0.9, -0.8, 0.3, 0.9, 0.1, 0, 1.6, 1.6, 2.7, 0.3, 1.9, 0.8, 1.1, 1.7, 2.2, 1.6, 0.6, 0.7, 0.6, 0.9,3.3, 1.1, -0.1, 1.8, 3.1, 2.8, 2, 0.6, 1.8, 2.1, 2.4, 1.5] colors = ['#063970' if e >= 0 else '#e28743' for e in data] ax = bar_chart(data, orient='v', color=colors, width=0.7, rotation=45, labels='', show=False) plot(np.zeros(128), ax=ax, show=False, color='black', lw=0.5) ax.grid(axis='y', ls='dotted', color='lightgrey') times = np.arange(np.datetime64('1894'), np.datetime64('2022'), np.timedelta64(1, 'Y')) ax.set_xticklabels(times) ax.xaxis.set_major_locator(plt.MaxNLocator(10)) ax.set_title('United States Anual Average Temperature Anomaly (°F)', fontdict={'fontsize': 14}) plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__bar_017.png :alt: United States Anual Average Temperature Anomaly (°F) :srcset: /auto_examples/plotting/images/sphx_glr__bar_017.png, /auto_examples/plotting/images/sphx_glr__bar_017_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.675 seconds) .. _sphx_glr_download_auto_examples_plotting__bar.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/plotting/_bar.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _bar.py <_bar.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _bar.ipynb <_bar.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_