.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plotting/_hist.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__hist.py: ==================== 6.9 histogram plot ==================== .. GENERATED FROM PYTHON SOURCE LINES 7-18 .. code-block:: default 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.4', 'matplotlib': '3.8.4', 'numpy': '1.26.4', 'pandas': '1.5.3', 'scipy': '1.13.1'} .. GENERATED FROM PYTHON SOURCE LINES 19-24 .. code-block:: default 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 25-28 .. code-block:: default data = np.random.randn(1000) .. GENERATED FROM PYTHON SOURCE LINES 29-30 let's start with a basic histogram .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: default _ = hist(data, bins = 100) .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_001.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_001.png, /auto_examples/plotting/images/sphx_glr__hist_001_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-37 adding KDE and specifying line properties .. GENERATED FROM PYTHON SOURCE LINES 37-44 .. code-block:: default _ = hist(data, add_kde=True, bins=50, color = 'lightpink', line_kws={'linestyle': '--', 'color': 'darkslategrey', 'linewidth': 2.0} ) .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_002.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_002.png, /auto_examples/plotting/images/sphx_glr__hist_002_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-46 setting grid to False .. GENERATED FROM PYTHON SOURCE LINES 46-53 .. code-block:: default _ = hist(data, bins = 100, grid = False, color = 'c', add_kde=True, line_kws={'linestyle': '--', 'color':'firebrick', 'linewidth': 2.0} ) .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_003.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_003.png, /auto_examples/plotting/images/sphx_glr__hist_003_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-55 manipulating kde calculation .. GENERATED FROM PYTHON SOURCE LINES 55-62 .. code-block:: default _ = 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/plotting/images/sphx_glr__hist_004.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_004.png, /auto_examples/plotting/images/sphx_glr__hist_004_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-65 Any argument for matplotlib.hist can be given to hist function for example ``color`` or ``edgecolor`` .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: default _ = hist(data, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False, color='khaki') .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_005.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_005.png, /auto_examples/plotting/images/sphx_glr__hist_005_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-72 ``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 72-75 .. code-block:: default _ = hist(data, bins = 100, edgecolor = "k", histtype='step') .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_006.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_006.png, /auto_examples/plotting/images/sphx_glr__hist_006_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-77 if data contains multiple columns, it will be plotted on same axis .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: default _ = hist(df[cols], alpha=0.7) .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_007.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_007.png, /auto_examples/plotting/images/sphx_glr__hist_007_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-83 ``share_axes`` can be set to False to plot multiple columns on different axis. .. GENERATED FROM PYTHON SOURCE LINES 83-87 .. code-block:: default _ = hist(df[cols], share_axes=False, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False) .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_008.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_008.png, /auto_examples/plotting/images/sphx_glr__hist_008_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-89 Arguments of subplots can be given to ``subplots_kws`` .. GENERATED FROM PYTHON SOURCE LINES 89-93 .. code-block:: default _ = hist(df[cols], share_axes=False, subplots_kws={"sharex": "all"}, bins = 20, linewidth = 0.5, edgecolor = "k", grid=False) .. image-sg:: /auto_examples/plotting/images/sphx_glr__hist_009.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_009.png, /auto_examples/plotting/images/sphx_glr__hist_009_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 94-96 ``return_axes`` can be set to True if we want to further work on current axis .. GENERATED FROM PYTHON SOURCE LINES 96-106 .. code-block:: default 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/plotting/images/sphx_glr__hist_010.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_010.png, /auto_examples/plotting/images/sphx_glr__hist_010_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 6 6 .. GENERATED FROM PYTHON SOURCE LINES 107-109 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 109-121 .. code-block:: default 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/plotting/images/sphx_glr__hist_011.png :alt: hist :srcset: /auto_examples/plotting/images/sphx_glr__hist_011.png, /auto_examples/plotting/images/sphx_glr__hist_011_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.919 seconds) .. _sphx_glr_download_auto_examples_plotting__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/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/plotting/_hist.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _hist.py <_hist.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _hist.ipynb <_hist.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_