Skip to content

Gaussian kde contour

daspi.plotlib.plotter.GaussianKDEContour(source, target, feature, fill=True, fade_outers=True, n_points=DEFAULT.KD_SEQUENCE_LEN, margin=0.2, target_on_y=True, color=None, ax=None, visible_spines=None, hide_axis=None, **kwds)

Bases: Plotter

Class for creating contour plotters. Use this class for bivariate plots.

Performs a 2 dimensional kernel density estimation using Gaussian Kernels. The estimation is then shown as contour lines. The x- and y-axes remain as feature and target axes.

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

fill

Flag indicating whether to fill between the contour lines, by default True

TYPE: bool DEFAULT: True

fade_outers

Flag indicating whether the outer lines of the contour plot should be faded. This has no effect if fill is True, by default True.

TYPE: bool DEFAULT: True

n_points

Number of points the estimate and the sequence should have. Note that the calculated points are equal to the square of the given number (because the contour is two-dimensional). by default KD_SEQUENCE_LEN (defined in constants.py)

TYPE: int DEFAULT: KD_SEQUENCE_LEN

margin

Margin for the sequence as factor of data range, by default 0.2.

TYPE: float DEFAULT: 0.2

target_on_y

Flag indicating whether the target variable is plotted on the y-axis. If False, all contour lines have the same color. 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

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 matplotlib.pyplot as plt
from daspi import GaussianKDEContour, load_dataset

fig, ax = plt.subplots()
colors = ('#1f77b4', '#ff7f0e', '#2ca02c')
df = load_dataset('iris')
for color, (name, group) in zip(colors, df.groupby('species')):
    kde = GaussianKDEContour(
        source=group, target='length', feature='width', color=color,
        fill=False, fade_outers=False, margin=0.3, ax=ax)
    kde()

Apply using the plot method of a DaSPi Chart object:

import daspi as dsp

df = load_dataset('iris')
chart = dsp.SingleChart(
        source=df,
        target='length',
        feature='width',
        hue='leaf',
    ).plot(
        dsp.GaussianKDEContour,
        fill=False,
        fade_outers=False,
        margin=0.3
    ).label() # neded to add legend

shape = (n_points, n_points) instance-attribute

Shape used to reshape data before plotting the contours.

fill = fill instance-attribute

Flag indicating whether to fill between the contour lines.

cmap = LinearSegmentedColormap.from_list('', colors) instance-attribute

The colormap to be used for the contour plot.

kw_default property

Return the default keyword arguments for the plot.