Skip to content

Center location

daspi.plotlib.plotter.CenterLocation(source, target, feature='', kind='mean', show_line=True, show_center=True, f_base=DEFAULT.FEATURE_BASE, skip_na=None, target_on_y=True, color=None, marker=None, ax=None, visible_spines=None, hide_axis=None, **kwds)

Bases: TransformPlotter

A center location (mean or median) plotter that extends the TransformPlotter class.

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: ''

kind

The type of center to plot ('mean' or 'median'), by default 'mean'.

TYPE: Literal['mean', 'median'] DEFAULT: 'mean'

show_line

Flag indicating whether to draw a line between the calculated mean or median points, by default True

TYPE: bool DEFAULT: True

show_center

Flag indicating whether to show the center points, by default True.

TYPE: bool DEFAULT: True

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 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 center points. Available markers see: https://matplotlib.org/stable/api/markers_api.html, 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 CenterLocation, Scatter

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)))))
scatter=Scatter(source=df, target='y', feature='x', ax=ax)
center = CenterLocation(
    source=df, target='y', feature='x', kind='median',
    show_center=True, show_line=True, ax= ax)
center(marker='_', markersize=10)
center.label_feature_ticks()
scatter(color='black')

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.CenterLocation,
        kind='median',
        show_line=True,
        show_center=True,
        kw_call=dict(marker='_', markersize=30)
    ).plot(
        dsp.Scatter,
    ).label() # neded to label feature ticks
Note

Be careful with the show_center and show_line flags. Either show_center or show_line must be True, otherwise this plot makes no sense. If both are False, nothing will be drawn.

show_line = show_line instance-attribute

Whether to draw a line between the calculated means or medians.

show_center = show_center instance-attribute

Whether to draw the center points.

kw_default property

Default keyword arguments for plotting (read-only)

kind property writable

Get and set the type of location ('mean' or 'median') to plot.

RAISES DESCRIPTION
AssertionError

If neither 'mean' or 'median' is given when setting kind.

marker property

Get the marker style for the center points if show_center is True, otherwise '' is returned (read-only).

linestyle property

Get rcParams line style if show_line is True, otherwise '' is returned (read-only).

transform(feature_data, target_data)

Calculate the mean or median of the target data and return the transformed data.

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

The transformed data source for the plot.

TYPE: pandas DataFrame