Skip to content

Bar

daspi.plotlib.plotter.Bar(source, target, feature, stack=True, width=CATEGORY.FEATURE_SPACE, method=None, kw_method={}, f_base=DEFAULT.FEATURE_BASE, skip_na=None, target_on_y=True, color=None, ax=None, visible_spines=None, hide_axis=None, **kwds)

Bases: TransformPlotter

A bar 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.

TYPE: str

stack

Flag indicating whether to stack the bars. It is checked whether there are already bars at each feature position, if so the new ones are stacked on top of the existing ones, by default True

TYPE: bool DEFAULT: True

width

Width of the bars, by default CATEGORY.FEATURE_SPACE

TYPE: float DEFAULT: FEATURE_SPACE

kw_method

Additional keyword arguments to be passed to the method, by default {}

TYPE: dict DEFAULT: {}

method

A pandas Series method to use for aggregating target values within each feature level. Like 'sum', 'count' or similar that returns a scalar, by default None

TYPE: str DEFAULT: None

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

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:

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

fig, ax = plt.subplots()
df = pd.DataFrame(dict(
    x = list('abcdefghijklmno'),
    y = list(100/x for x in range(1, 16))))
bar = Bar(
    source=df, target='y', feature='x')
bar()
bar.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 = list('abcdefghijklmno'),
    y = list(100/x for x in range(1, 16))))
chart = dsp.SingleChart(
        source=df,
        target='y',
        feature='x',
        categorical_feature=True,
    ).plot(
        dsp.Bar,
    ).label() # neded to label feature ticks

You can also aggregate a function on the target column Apply to an existing Axes object:

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

limit = 3.5
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) > limit)
        + list(np.random.normal(loc=4, scale=1, size=50) > limit)
        + list(np.random.normal(loc=2, scale=1, size=50) > limit))))
bar = Bar(
    source=df, target='y', feature='x', method='sum', ax=ax)
bar()

Apply using the plot method of a DaSPi Chart object:

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

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

stack = stack instance-attribute

Whether to stack the bars.

width = width instance-attribute

Width of the bars.

method = method instance-attribute

The provided pandas Series method to use for aggregating target values.

kw_method = kw_method instance-attribute

The provided keyword arguments to be passed to the method.

kw_default property

Default keyword arguments for plotting (read-only)

bars property

Get a list of BarContainer objects representing the bars in the plot.

t_base property

Get the base values for the bars (target), contains zeros when not stacked.

The base values are calculated based on existing bars in the plot. If stacking is enabled, the base values are updated to reflect the top of the existing bars at each feature position. If stacking is disabled, the base values are simply zeros.

transform(feature_data, target_data)

Perform the given Series method on the target data if given and return the transformed data. If no method is given, the target data is adopted directly.

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

RAISES DESCRIPTION
AssertionError

When transformed target data (or original data if no method is specified) is non-scalar.