Skip to content

Loess line

daspi.plotlib.plotter.LoessLine(source, target, feature, target_on_y=True, color=None, marker=None, show_scatter=False, show_fit_ci=False, confidence_level=0.95, fraction=0.2, order=3, kernel='gaussian', n_points=DEFAULT.LOWESS_SEQUENCE_LEN, kind='LOESS', ax=None, visible_spines=None, hide_axis=None, **kwds)

Bases: Plotter

A class for plotting a loess line.

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

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 artists. If None, the first color is taken from the color cycle, by default None.

TYPE: str | None DEFAULT: None

marker

The marker style for the scatter plot. Available markers see: https://matplotlib.org/stable/api/markers_api.html, by default None

TYPE: str | None DEFAULT: None

show_scatter

Flag indicating whether to show the individual points, by default False.

TYPE: bool DEFAULT: False

show_fit_ci

Flag indicating whether to show the confidence interval for the lowess line as filled area, by default False.

TYPE: bool DEFAULT: False

confidence_level

Calculate confidence bands for the lowess line at this level (0 to 1). Defaults to 0.95.

TYPE: float DEFAULT: 0.95

fraction

The fraction of the data used for each local regression. A good value to start with is 2/3 (default value of statsmodels). Reduce the value to avoid underfitting. A value below 0.2 usually leads to overfitting, except for gaussian weights. Defaults to 0.2 because of default gaussian kernel.

TYPE: float | None DEFAULT: 0.2

order

The order of the local regression to be fitted. This determines the degree of the polynomial used in the local regression:

0: No smoothing (interpolation) 1: Linear regression 2: Quadratic regression 3: Cubic regression Default is 3.

TYPE: Literal[0, 1, 2, 3] DEFAULT: 3

kernel

The kernel function used to calculate the weights. Available kernels are: 'tricube': Tricube kernel function 'gaussian': Gaussian kernel function 'epanechnikov': Epanechnikov kernel function. Default is 'gaussian', because it will not run in a Singular Matrix Error.

TYPE: Literal['tricube', 'gaussian', 'epanechnikov'] DEFAULT: 'gaussian'

n_points

Number of points the smoothed line and its sequence should have, by default LOWESS_SEQUENCE_LEN (defined in constants.py).

TYPE: int DEFAULT: LOWESS_SEQUENCE_LEN

kind

The type of lowess line to be plotted. Available options are: 'LOESS': Lowess line using the LOESS algorithm 'LOWESS': Lowess line using the LOWESS algorithm Default is 'LOESS' because it needs less computational power.

TYPE: Literal['LOESS', 'LOWESS'] DEFAULT: 'LOESS'

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 LoessLine

fig, ax = plt.subplots()
x = 5*np.random.random(100)
df = pd.DataFrame(dict(
    x = x,
    y = np.sin(x) * 3*np.exp(-x) + np.random.normal(0, 0.2, 100)))
loess_line = LoessLine(
    source=df, target='y', feature='x',
    show_scatter=True, show_fit_ci=True, ax=ax)
loess_line(
    kw_scatter=dict(color='black', s=10, alpha=0.5),
    kw_fit_ci=dict(color='skyblue'),
    color='deepskyblue')

Apply using the plot method of a DaSPi Chart object:

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

x = 5*np.random.random(100)
df = pd.DataFrame(dict(
    x = x,
    y = np.sin(x) * 3*np.exp(-x) + np.random.normal(0, 0.2, 100)))
chart = dsp.SingleChart(
        source=df,
        target='y',
        feature='x'
    ).plot(
        dsp.LoessLine,
        show_scatter=True,
        show_fit_ci=True,
        kw_call=dict(
            kw_scatter=dict(color='black', s=10, alpha=0.5),
            kw_fit_ci=dict(color='skyblue'),
            color='deepskyblue'))

show_scatter = show_scatter instance-attribute

Whether to show the individual points in the plot.

show_fit_ci = show_fit_ci instance-attribute

Whether to show the confidence interval for the fitted lowess line.

model = Model_(source=source, target=target, feature=feature) instance-attribute

The fitted results of the linear regression model.

kw_default property

Default keyword arguments for plotting (read-only)