.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plotting/_boxplot.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__boxplot.py: ============== 6.16 boxplot ============== .. currentmodule:: easy_mpl This file shows the usage of :func:`boxplot` function. .. GENERATED FROM PYTHON SOURCE LINES 10-23 .. code-block:: default # sphinx_gallery_thumbnail_number = -3 import pandas as pd import matplotlib.pyplot as plt from easy_mpl import boxplot, plot from easy_mpl.utils import _rescale 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 24-34 .. code-block:: default f = "https://raw.githubusercontent.com/AtrCheema/AI4Water/master/ai4water/datasets/arg_busan.csv" dataframe = pd.read_csv(f, index_col='index') cols = ['air_temp_c', 'wat_temp_c', 'sal_psu', 'tide_cm', 'rel_hum', 'pcp12_mm'] df = dataframe.copy() for col in df.columns: df[col] = _rescale(df[col].values) print(f"Our data has {len(df)} rows and {df.shape[1]} columns") .. rst-class:: sphx-glr-script-out .. code-block:: none Our data has 1446 rows and 25 columns .. GENERATED FROM PYTHON SOURCE LINES 35-36 To draw a boxplot we can provide a pandas DataFrame .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: default _ = boxplot(df[cols], fill_color='khaki') .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_001.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_001.png, /auto_examples/plotting/images/sphx_glr__boxplot_001_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 39-40 We can also provide multiple (numpy) array .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: default _ = boxplot(df[cols].values) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_002.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_002.png, /auto_examples/plotting/images/sphx_glr__boxplot_002_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 43-44 We can give the list of arrays .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: default data = df.iloc[:, 0:12] _ = boxplot([data[col].values for col in data.columns]) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_003.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_003.png, /auto_examples/plotting/images/sphx_glr__boxplot_003_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 49-50 The fill color can be specificed using any valid matplotlib cmap .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: default _ = boxplot(df[cols], fill_color="GnBu", patch_artist=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_004.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_004.png, /auto_examples/plotting/images/sphx_glr__boxplot_004_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: default _ = boxplot(df[cols], fill_color="thistle", line_width=1.5, patch_artist=True) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_005.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_005.png, /auto_examples/plotting/images/sphx_glr__boxplot_005_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 57-58 change color of median line .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: default _ = boxplot(df[cols], fill_color="thistle", patch_artist=True, medianprops={"color": "purple"}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_006.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_006.png, /auto_examples/plotting/images/sphx_glr__boxplot_006_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 63-64 different color for box boundary and filling the box .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: default _ = boxplot(df[cols], fill_color="Pastel2", patch_artist=True, meanline=True, showmeans=True, meanprops={"color": "black"}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_007.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_007.png, /auto_examples/plotting/images/sphx_glr__boxplot_007_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-70 show notches .. GENERATED FROM PYTHON SOURCE LINES 70-77 .. code-block:: default _ = boxplot(df[cols], fill_color="pink", notch=True, patch_artist=True, medianprops={"color": "black"}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_008.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_008.png, /auto_examples/plotting/images/sphx_glr__boxplot_008_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-79 don't show outliers .. GENERATED FROM PYTHON SOURCE LINES 79-83 .. code-block:: default _ = boxplot(df[cols], fill_color="bone", patch_artist=True, showfliers=False, medianprops={"color": "gold"}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_009.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_009.png, /auto_examples/plotting/images/sphx_glr__boxplot_009_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-85 change circle size of fliers .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: default _ = boxplot(df[cols], fill_color="gray", patch_artist=True,notch=True, flierprops={"ms": 1.0}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_010.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_010.png, /auto_examples/plotting/images/sphx_glr__boxplot_010_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-91 edit caps and whiskers properties .. GENERATED FROM PYTHON SOURCE LINES 91-101 .. code-block:: default _ = boxplot(df[cols], fill_color="Pastel2", patch_artist=True, flierprops={"ms": 4.0, "marker": 'o', "color": 'thistle', "alpha":0.8}, medianprops={"color": "black"}, capprops={'color':'#7570b3', "linewidth":2}, whiskerprops={'color':'#7570b3', "linewidth":2}) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_011.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_011.png, /auto_examples/plotting/images/sphx_glr__boxplot_011_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-103 don't show whiskers .. GENERATED FROM PYTHON SOURCE LINES 103-108 .. code-block:: default _ = boxplot(df[cols], fill_color="Pastel1", patch_artist=True, notch=True, showfliers=False, whis=0.0) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_012.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_012.png, /auto_examples/plotting/images/sphx_glr__boxplot_012_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 109-111 If we want to draw a separate boxplot on each axes, we can set the value of ``share_axes`` to False. .. GENERATED FROM PYTHON SOURCE LINES 111-116 .. code-block:: default _ = boxplot(dataframe[cols], flierprops={"ms": 1.0}, fill_color="ocean", patch_artist=True, share_axes=False, figsize=(5, 7)) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_013.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_013.png, /auto_examples/plotting/images/sphx_glr__boxplot_013_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-118 To draw the horizontal boxplots, we can set ``vert`` to False. .. GENERATED FROM PYTHON SOURCE LINES 118-126 .. code-block:: default _ = boxplot(dataframe[cols], flierprops={"ms": 1.0}, fill_color="Set2", patch_artist=True, notch=True, medianprops={"color": "black"}, share_axes=False, vert=False, widths=0.7, figsize=(8, 7) ) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_014.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_014.png, /auto_examples/plotting/images/sphx_glr__boxplot_014_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-129 The boxplot function returns a tuple. The first argument is the matplotlib axes and second value is a dictionary consisting of output from ``axes.boxplot``. .. GENERATED FROM PYTHON SOURCE LINES 129-151 .. code-block:: default ax, _ = boxplot(df[cols], fill_color="Pastel2", patch_artist=True, flierprops={"ms": 4.0, "marker": 's', "markerfacecolor": 'lightcoral', "alpha":0.8 }, medianprops={"color": "black"}, capprops={'color':'#7570b3', "linewidth":2}, whiskerprops={'color':'#7570b3', "linewidth":2}, show=False) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ax.set( axisbelow=True, # Hide the grid behind plot objects xlabel='Faetures', ylabel='Value', ) ax.set_facecolor('floralwhite') plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_015.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_015.png, /auto_examples/plotting/images/sphx_glr__boxplot_015_2_0x.png 2.0x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 152-154 In order to make grouped boxplots, we can drawo two boxplots on same axes. We can specify the position of boxes on the axes using ``positions`` argument. .. GENERATED FROM PYTHON SOURCE LINES 154-179 .. code-block:: default # Some fake data to plot A= [[1, 2, 5,], [7, 2]] B = [[5, 7, 2, 2, 5], [7, 2, 5]] ax, _ = boxplot(A, line_color='#D7191C', positions=[1, 2], sym='', widths = 0.6, show=False) _, _ = boxplot(B, line_color="#2C7BB6", positions=[4, 5], sym='', widths = 0.6, show=False) ax.yaxis.grid(True, linestyle='-', which='major', color='lightgrey', alpha=0.5) ax.set_xticks([1.5, 4.5]) ax.set_xticklabels(['Group1', 'Group2']) ax.set( axisbelow=True, # Hide the grid behind plot objects xlabel='Faetures', ylabel='Value', ) ax.set_facecolor('floralwhite') plt.show() .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_016.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_016.png, /auto_examples/plotting/images/sphx_glr__boxplot_016_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/envs/dev/lib/python3.9/site-packages/easy_mpl/_box.py:184: UserWarning: xticks (4) and xticklabels (2) dont match warnings.warn(f""" .. GENERATED FROM PYTHON SOURCE LINES 180-181 join mean of each box through a line .. GENERATED FROM PYTHON SOURCE LINES 181-195 .. code-block:: default plt.close('all') ax, _ = boxplot(df[cols], fill_color="thistle", patch_artist=True, notch=True, boxprops = {"linewidth":1.5, "color":'purple'}, showmeans=True, meanprops={"markerfacecolor": "black", "markeredgecolor":'black', "marker":"o"}, showfliers=False, whis=0.0, show=False) plot(ax.get_xticks(), df[cols].mean().values, color="darkorchid", ax=ax) .. image-sg:: /auto_examples/plotting/images/sphx_glr__boxplot_017.png :alt: boxplot :srcset: /auto_examples/plotting/images/sphx_glr__boxplot_017.png, /auto_examples/plotting/images/sphx_glr__boxplot_017_2_0x.png 2.0x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.387 seconds) .. _sphx_glr_download_auto_examples_plotting__boxplot.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/_boxplot.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _boxplot.py <_boxplot.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _boxplot.ipynb <_boxplot.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_