Skip to content

Plotting Contexts

soundscapy.plotting.plot_context

Data and state management for plotting layers.

This module provides the PlotContext class that manages data and state for ISOPlot visualizations, enabling a more flexible architecture for plot generation with support for layered visualizations and subplot management.

CLASS DESCRIPTION
PlotContext

Manages data and state for a plot or subplot.

PlotContext

PlotContext(
    data: DataFrame | None = None,
    x: str = "ISOPleasant",
    y: str = "ISOEventful",
    hue: str | None = None,
    ax: Axes | None = None,
    title: str | None = None,
)

Manages data and state for a plot or subplot.

This class centralizes the management of data, coordinates, and other state needed for rendering plot layers, allowing for consistent data access patterns and simplified layer implementation.

ATTRIBUTE DESCRIPTION
data

The data associated with this context

x

The column name for x-axis data

y

The column name for y-axis data

hue

The column name for color encoding, if any

ax

The matplotlib Axes object this context is associated with

title

The title for this context's plot

layers

The visualization layers to be rendered on this context

TYPE: list[Layer]

Initialize a PlotContext.

PARAMETER DESCRIPTION
data

Data to be visualized

TYPE: DataFrame | None DEFAULT: None

x

Column name for x-axis data

TYPE: str DEFAULT: 'ISOPleasant'

y

Column name for y-axis data

TYPE: str DEFAULT: 'ISOEventful'

hue

Column name for color encoding

TYPE: str | None DEFAULT: None

ax

Matplotlib axis to render on

TYPE: Axes | None DEFAULT: None

title

Title for this plot context

TYPE: str | None DEFAULT: None

METHOD DESCRIPTION
create_child

Create a child context that inherits properties from this context.

Source code in src/soundscapy/plotting/plot_context.py
def __init__(
    self,
    data: pd.DataFrame | None = None,
    x: str = "ISOPleasant",
    y: str = "ISOEventful",
    hue: str | None = None,
    ax: Axes | None = None,
    title: str | None = None,
) -> None:
    """
    Initialize a PlotContext.

    Parameters
    ----------
    data
        Data to be visualized
    x
        Column name for x-axis data
    y
        Column name for y-axis data
    hue
        Column name for color encoding
    ax
        Matplotlib axis to render on
    title
        Title for this plot context

    """
    self.data = data
    self.x = x
    self.y = y
    self.hue = hue
    self.ax = ax
    self.title = title
    self.layers: list[Layer] = []
    self.parent: PlotContext | None = None

create_child

create_child(
    data: DataFrame | None = None,
    title: str | None = None,
    ax: Axes | None = None,
) -> PlotContext

Create a child context that inherits properties from this context.

PARAMETER DESCRIPTION
data

Data for the child context. If None, inherits from parent.

TYPE: DataFrame | None DEFAULT: None

title

Title for the child context

TYPE: str | None DEFAULT: None

ax

Matplotlib axis for the child context

TYPE: Axes | None DEFAULT: None

RETURNS DESCRIPTION
PlotContext

A new child context with inherited properties

Source code in src/soundscapy/plotting/plot_context.py
def create_child(
    self,
    data: pd.DataFrame | None = None,
    title: str | None = None,
    ax: Axes | None = None,
) -> PlotContext:
    """
    Create a child context that inherits properties from this context.

    Parameters
    ----------
    data
        Data for the child context. If None, inherits from parent.
    title
        Title for the child context
    ax
        Matplotlib axis for the child context

    Returns
    -------
    :
        A new child context with inherited properties

    """
    child = PlotContext(
        data=data if data is not None else self.data,
        x=self.x,
        y=self.y,
        hue=self.hue,
        ax=ax,
        title=title,
    )
    child.parent = self
    return child