.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/hist.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. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_hist.py: ========= e. hist ========= .. currentmodule:: easy_mpl This file shows the usage of :func:`hist` function. .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: Python from easy_mpl import hist import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib import colors from easy_mpl.utils import version_info version_info() # print version information of all the packages being used .. rst-class:: sphx-glr-script-out .. code-block:: none {'easy_mpl': '0.21.5', 'matplotlib': '3.9.4', 'numpy': '2.0.2', 'pandas': '2.2.3', 'scipy': '1.13.1'} .. GENERATED FROM PYTHON SOURCE LINES 21-26 .. code-block:: Python f = "https://raw.githubusercontent.com/AtrCheema/AI4Water/master/ai4water/datasets/arg_busan.csv" df = pd.read_csv(f, index_col='index') cols = ['air_temp_c', 'wat_temp_c', 'sal_psu', 'tide_cm', 'rel_hum', 'pcp12_mm'] .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: Python data = np.random.randn(1000) .. GENERATED FROM PYTHON SOURCE LINES 31-32 let's start with a basic histogram .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python _ = hist(data, bins = 100) .. image-sg:: /auto_examples/images/sphx_glr_hist_001.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_001.png, /auto_examples/images/sphx_glr_hist_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 38-39 adding KDE and specifying line properties .. GENERATED FROM PYTHON SOURCE LINES 39-46 .. code-block:: Python _ = hist(data, add_kde=True, bins=50, color = 'lightpink', line_kws={'linestyle': '--', 'color': 'darkslategrey', 'linewidth': 2.0} ) .. image-sg:: /auto_examples/images/sphx_glr_hist_002.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_002.png, /auto_examples/images/sphx_glr_hist_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-48 setting grid to False .. GENERATED FROM PYTHON SOURCE LINES 48-55 .. code-block:: Python _ = hist(data, bins = 100, grid = False, color = 'c', add_kde=True, line_kws={'linestyle': '--', 'color':'firebrick', 'linewidth': 2.0} ) .. image-sg:: /auto_examples/images/sphx_glr_hist_003.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_003.png, /auto_examples/images/sphx_glr_hist_003_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-57 manipulating kde calculation .. GENERATED FROM PYTHON SOURCE LINES 57-64 .. code-block:: Python _ = hist(data, bins = 100, color = 'gold', add_kde=True, kde_kws=dict(cut=0.2), line_kws={'linestyle': '--', 'color':'firebrick', 'linewidth': 2.0}) .. image-sg:: /auto_examples/images/sphx_glr_hist_004.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_004.png, /auto_examples/images/sphx_glr_hist_004_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 65-67 Any argument for matplotlib.hist can be given to hist function for example ``color`` or ``edgecolor`` .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: Python _ = hist(data, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False, color='khaki') .. image-sg:: /auto_examples/images/sphx_glr_hist_005.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_005.png, /auto_examples/images/sphx_glr_hist_005_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-74 ``histtype`` defines the type of histogram to show. Here we are using ``step`` which generates a lineplot that is by default unfilled. .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: Python _ = hist(data, bins = 100, edgecolor = "k", histtype='step') .. image-sg:: /auto_examples/images/sphx_glr_hist_006.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_006.png, /auto_examples/images/sphx_glr_hist_006_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-79 if data contains multiple columns, it will be plotted on same axis .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: Python _ = hist(df[cols], alpha=0.7) .. image-sg:: /auto_examples/images/sphx_glr_hist_007.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_007.png, /auto_examples/images/sphx_glr_hist_007_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 83-85 ``share_axes`` can be set to False to plot multiple columns on different axis. .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: Python _ = hist(df[cols], share_axes=False, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False) .. image-sg:: /auto_examples/images/sphx_glr_hist_008.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_008.png, /auto_examples/images/sphx_glr_hist_008_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-91 Arguments of subplots can be given to ``subplots_kws`` .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: Python _ = hist(df[cols], share_axes=False, subplots_kws={"sharex": "all"}, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False) .. image-sg:: /auto_examples/images/sphx_glr_hist_009.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_009.png, /auto_examples/images/sphx_glr_hist_009_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-98 ``return_axes`` can be set to True if we want to further work on current axis .. GENERATED FROM PYTHON SOURCE LINES 98-108 .. code-block:: Python outs, axes = hist(df[cols], share_axes=False, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False, return_axes=True, show=False) print(f"{len(outs)} {len(axes)}") for idx, ax in enumerate(axes): ax.set_ylabel('Counts', fontsize=12) plt.subplots_adjust(wspace=0.35) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_hist_010.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_010.png, /auto_examples/images/sphx_glr_hist_010_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 6 6 .. GENERATED FROM PYTHON SOURCE LINES 109-111 we can further modify colors for better understanding and correct readings for example in this plot, the color represent the frequency of data. .. GENERATED FROM PYTHON SOURCE LINES 111-123 .. code-block:: Python n, bins, patches = hist(data, bins = 20, show=False) # Setting color f = ((n ** (1 / 2)) / n.max()) norm = colors.Normalize(f.min(), f.max()) for thisfrac, thispatch in zip(f, patches): color = plt.cm.viridis(norm(thisfrac)) thispatch.set_facecolor(color) plt.show() .. image-sg:: /auto_examples/images/sphx_glr_hist_011.png :alt: hist :srcset: /auto_examples/images/sphx_glr_hist_011.png, /auto_examples/images/sphx_glr_hist_011_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.354 seconds) .. _sphx_glr_download_auto_examples_hist.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/sphinx-gallery/sphinx-gallery.github.io/master?urlpath=lab/tree/notebooks/auto_examples/hist.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: hist.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: hist.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: hist.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_