<jupyter_start><jupyter_text>Numerical variable description: DistributionNumerical variable description with a plot for one or several columns in a dataframe.<jupyter_code>%matplotlib inline
import seaborn as sns
sns.set_theme(style="whitegrid")
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math<jupyter_output><empty_output><jupyter_text>functions<jupyter_code>## plot distribution of a column
def dist(df:pd.DataFrame, column:str, ax:"matplotlib axis")->"matplotlib axis":
    # validation
    assert column in df.columns.tolist(), f"column '{column}' is not available."
    # plot
    ax = sns.stripplot(x = column, data = df, color = "red", alpha = .35, ax = ax)
    ax = sns.violinplot(x = column, data = df, scale="count", inner="quartile", scale_hue=False, bw=.2, ax = ax)
    ax = sns.boxplot(x = column, data = df, showfliers=True, showbox=False, ax = ax)
    # return axis
    return ax


# plot selected columns
def plot(df:pd.DataFrame, columns:"list or str", plot_function:"function", num_plots_per_row:int = 3):
    # if columns is only one string    
    if isinstance(columns, str):
        # figsize
        figsize = (10, 10)
        # number of plots in rows / columns
        nrs = ncs = 1
    # if is a list
    elif isinstance(columns, list):
        # number of columns to be ploted
        ncolumns = len(columns)
        # number of plots in rows / columns
        nrs = math.ceil(ncolumns / num_plots_per_row)
        ncs = num_plots_per_row if ncolumns >= num_plots_per_row else ncolumns 
        # figsize
        figsize = (ncs*5, nrs*5)    
    else:
        raise
    # create figure and axis    
    fig, ax = plt.subplots(ncols = ncs, nrows = nrs, figsize = figsize)
    # if axis is an array
    if isinstance(ax, np.ndarray):
        # reshape
        ax = ax.ravel()
        # loop of axis
        for ii, c in enumerate(columns):
            # plot in cells
            _ = plot_function(df, c, ax[ii])
    # if axis is only one
    else:
        # plots only one
        _ = plot_function(df, columns[0] if len(columns) == 1 else columns, ax)
    # display plot
    plt.show()<jupyter_output><empty_output><jupyter_text>data<jupyter_code>""" tips """

# load data
tips = sns.load_dataset("tips")
# get numerical columns
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
cols_num_tips = tips.select_dtypes(include=numerics).columns.tolist()


""" iris """

# load data

iris = sns.load_dataset("iris")
# get numerical columns
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
cols_num_iris = iris.select_dtypes(include=numerics).columns.tolist()<jupyter_output><empty_output><jupyter_text>plots<jupyter_code># plot only one column
fig, ax = plt.subplots(ncols = 1, nrows = 1, figsize = (5, 5))
ax = dist(tips, cols_num_tips[0], ax)
plt.show()
# plot only one column
fig, ax = plt.subplots(ncols = 1, nrows = 1, figsize = (5, 5))
ax = dist(iris, cols_num_iris[0], ax)
plt.show()
# plot several columns
columns = cols_num_tips[:]
df = tips.copy()
plot_function = dist
plot(df, columns, plot_function, num_plots_per_row = 3)
# plot several columns
columns = cols_num_iris[:]
df = iris.copy()
plot_function = dist
plot(df, columns, plot_function, num_plots_per_row = 3)<jupyter_output><empty_output>