Skip to content

Jitter

daspi.plotlib.plotter.Jitter(source, target, feature='', width=CATEGORY.FEATURE_SPACE, skip_na=None, target_on_y=True, color=None, marker=None, ax=None, visible_spines=None, hide_axis=None, **kwds)

Bases: TransformPlotter

A class for creating jitter plotters.

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 jitter, by default CATEGORY.FEATURE_SPACE.

TYPE: float DEFAULT: FEATURE_SPACE

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 scatter plot. 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 Jitter

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)))))
jitter = Jitter(
    source=df, target='y', feature='x', ax=ax)
jitter()
jitter.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 # neded to label feature ticks
    ).plot(
        dsp.Jitter,
    ).label() # neded to label feature ticks

width = width instance-attribute

The width of the jitter.

kw_default property

Default keyword arguments for plotting (read-only)

jitter(loc, size)

Generates normally distributed jitter values. The standard deviation is selected so that +- 6 sigma corresponds to the permissible width. To ensure the width, values that lie outside this range are restricted to the limits.

PARAMETER DESCRIPTION
loc

Center position (feature axis) of the jitted values.

TYPE: float

size

Amount of valaues to generate

TYPE: int

RETURNS DESCRIPTION
jitter

Normally distributed values, but not wider than the given width

TYPE: 1D array

transform(feature_data, target_data)

Normally randomize the target data for each feature value in the feature axis direction.

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