Skip to content

Box

Box

Bases: TransformPlotter

A box plotter that uses matplotlib's native boxplot with whiskers.

PARAMETER DESCRIPTION
source

Pandas long format DataFrame containing the data source for the plot.

TYPE: pandas DataFrame

target

Column name of the target variable for the plot.

TYPE: str

feature

Column name of the feature variable for the plot, by default ''

TYPE: str DEFAULT: ''

width

The width of the boxes, by default CATEGORY.FEATURE_SPACE * 0.8.

TYPE: float DEFAULT: FEATURE_SPACE * 0.8

showfliers

Whether to show outliers beyond the whiskers, by default True.

TYPE: bool DEFAULT: True

whis

The whisker length. If a float, it defines the reach of the whiskers to the beyond the first and third quartiles (e.g., 1.5 corresponds to 1.5*IQR). If a tuple of two values, they define the percentiles (e.g., (5, 95) for 5th and 95th percentiles), by default 1.5.

TYPE: float or tuple DEFAULT: 1.5

fill

Whether to fill the boxes with color, by default False.

TYPE: bool DEFAULT: False

f_base

Value that serves as the base location (offset) of the feature values. Only taken into account if feature is not given, by default DEFAULT.FEATURE_BASE.

TYPE: int | float DEFAULT: FEATURE_BASE

skip_na

Flag indicating whether to skip missing values in the feature grouped data, by default None - None, no missing values are skipped - 'all', grouped data is skipped if all values are missing - 'any', grouped data is skipped if any value is missing

TYPE: Literal['none', 'all', 'any'] DEFAULT: None

target_on_y

Flag indicating whether the target variable is plotted on the y-axis, by default True

TYPE: bool DEFAULT: True

color

Color to be used to draw the box edges. If None, the first color is taken from the color cycle, by default None.

TYPE: str | None DEFAULT: None

ax

The axes object for the plot. If None, the current axes is fetched using plt.gca(). If no axes are available, a new one is created. Defaults to None.

TYPE: Axes | None DEFAULT: None

visible_spines

Specifies which spines are visible, the others are hidden. If 'none', no spines are visible. If None, the spines are drawn according to the stylesheet. Defaults to None.

TYPE: Literal['target', 'feature', 'none'] | None DEFAULT: None

hide_axis

Specifies which axes should be hidden. If None, both axes are displayed. Defaults to None.

TYPE: Literal['target', 'feature', 'both'] | None DEFAULT: None

**kwds

Those arguments have no effect. Only serves to catch further arguments that have no use here (occurs when this class is used within chart objects).

DEFAULT: {}

Examples:

Apply to an existing Axes object:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from daspi import Box

fig, ax = plt.subplots()
df = pd.DataFrame(dict(
    x = ['first'] * 50 + ['second'] * 50 + ['third'] * 50,
    y = (
        list(np.random.normal(loc=3, scale=1, size=50))
        + list(np.random.normal(loc=4, scale=1, size=50))
        + list(np.random.normal(loc=2, scale=1, size=50)))))
box = Box(source=df, target='y', feature='x', ax=ax)
box()
box.label_feature_ticks()

Apply using the plot method of a DaSPi Chart object:

import numpy as np
import daspi as dsp
import pandas as pd

df = pd.DataFrame(dict(
    x = ['first'] * 50 + ['second'] * 50 + ['third'] * 50,
    y = (
        list(np.random.normal(loc=3, scale=1, size=50))
        + list(np.random.normal(loc=4, scale=1, size=50))
        + list(np.random.normal(loc=2, scale=1, size=50)))))
chart = dsp.SingleChart(
        source=df,
        target='y',
        feature='x',
        categorical_feature=True,
    ).plot(
        dsp.Box,
    ).label()  # needed to label feature ticks

Customize boxplot appearance:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from daspi import Box

fig, ax = plt.subplots()
df = pd.DataFrame(dict(
    x = ['A'] * 30 + ['B'] * 30 + ['C'] * 30,
    y = (
        list(np.random.normal(loc=10, scale=2, size=30))
        + list(np.random.normal(loc=15, scale=3, size=30))
        + list(np.random.normal(loc=12, scale=1.5, size=30)))))
box = Box(source=df, target='y', feature='x', width=0.5, ax=ax)
box(patch_artist=True, boxprops=dict(facecolor='lightblue'))
box.label_feature_ticks()

width = width instance-attribute

The width of the boxes.

showfliers = showfliers instance-attribute

Whether to show outliers beyond the whiskers.

whis = whis instance-attribute

The whisker length definition.

fill = fill instance-attribute

Whether to fill the boxes with color.

kw_default property

Default keyword arguments for plotting (read-only)

transform

Store the grouped target data for boxplot and return minimal DataFrame for compatibility.

PARAMETER DESCRIPTION
feature_data

Base location (offset) of feature axis coming from `feature_grouped' generator.

TYPE: int | float

target_data

feature grouped target data used for transformation, coming from `feature_grouped' generator.

TYPE: pandas Series

RETURNS DESCRIPTION
data

A minimal DataFrame for compatibility with the base class.

TYPE: pandas DataFrame