q. violin

This file shows the usage of violin_plot() function.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from easy_mpl import violin_plot
from easy_mpl.utils import _rescale
from easy_mpl.utils import version_info

version_info()
{'easy_mpl': '0.21.4', 'matplotlib': '3.8.0', 'numpy': '1.26.1', 'pandas': '2.1.1', 'scipy': '1.11.3'}
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']
for col in df.columns:
    df[col] = _rescale(df[col].values)

The basic violin plot can be drawn by passing a pandas DataFrame with one or more columns.

_ = violin_plot(df[cols])
violin

The function :func:`violin_plot always returns a matplotlib axes object. If we set show to False then, the returned axes can be used for further manipulation.

axes = violin_plot(df[cols], show=False)
axes.set_facecolor("#fbf9f4")
plt.show()
violin

The value of cut determines the length of tails on both sides.

axes = violin_plot(df[cols], show=False, cut=0.1)
axes.set_facecolor("#fbf9f4")
plt.show()
violin

We can specify colors for each of the violin plot separately. It can be any valid matplotlib colors i.e., name of color as string or its RGB vlaue.

axes = violin_plot(
    df[cols], show=False, cut=0.1, fill=False,
    scatter_kws={"s": 12, 'alpha': 0.5, 'edgecolors': None, 'linewidths': 0.2},
    datapoints_colors=['seagreen',
                       np.array([237, 187, 147]) / 255,
                      np.array([197, 194, 218]) / 255,
                      'darkcyan',
                      'skyblue',
                      "plum",
                       ]
                   )
axes.set_facecolor("#fbf9f4")
plt.show()
violin

Total running time of the script: (0 minutes 1.759 seconds)

Gallery generated by Sphinx-Gallery