Skip to content

KDE Plots

KDEPlot

Bases: BasePlot

Create a kernel density estimate plot.

This plot shows the distribution of a single variable.

Source code in src/monet_plots/plots/kde.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class KDEPlot(BasePlot):
    """Create a kernel density estimate plot.

    This plot shows the distribution of a single variable.
    """

    def __init__(self, df, x, y, title=None, label=None, *args, **kwargs):
        """
        Initialize the plot with data and plot settings.

        Args:
            df (pd.DataFrame, np.ndarray, xr.Dataset, xr.DataArray): DataFrame with the data to plot.
            x (str): Column name for the x-axis.
            y (str): Column name for the y-axis.
            title (str, optional): Title for the plot.
            label (str, optional): Label for the plot.
        """
        super().__init__(*args, **kwargs)
        self.df = df
        self.x = x
        self.y = y
        self.title = title
        self.label = label

    def plot(self, **kwargs):
        """Generate the KDE plot."""
        with sns.axes_style("ticks"):
            self.ax = sns.kdeplot(
                data=self.df, x=self.x, y=self.y, ax=self.ax, label=self.label, **kwargs
            )
            if self.title:
                self.ax.set_title(self.title)
            sns.despine()
        return self.ax

__init__(df, x, y, title=None, label=None, *args, **kwargs)

Initialize the plot with data and plot settings.

Parameters:

Name Type Description Default
df (DataFrame, ndarray, Dataset, DataArray)

DataFrame with the data to plot.

required
x str

Column name for the x-axis.

required
y str

Column name for the y-axis.

required
title str

Title for the plot.

None
label str

Label for the plot.

None
Source code in src/monet_plots/plots/kde.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def __init__(self, df, x, y, title=None, label=None, *args, **kwargs):
    """
    Initialize the plot with data and plot settings.

    Args:
        df (pd.DataFrame, np.ndarray, xr.Dataset, xr.DataArray): DataFrame with the data to plot.
        x (str): Column name for the x-axis.
        y (str): Column name for the y-axis.
        title (str, optional): Title for the plot.
        label (str, optional): Label for the plot.
    """
    super().__init__(*args, **kwargs)
    self.df = df
    self.x = x
    self.y = y
    self.title = title
    self.label = label

plot(**kwargs)

Generate the KDE plot.

Source code in src/monet_plots/plots/kde.py
32
33
34
35
36
37
38
39
40
41
def plot(self, **kwargs):
    """Generate the KDE plot."""
    with sns.axes_style("ticks"):
        self.ax = sns.kdeplot(
            data=self.df, x=self.x, y=self.y, ax=self.ax, label=self.label, **kwargs
        )
        if self.title:
            self.ax.set_title(self.title)
        sns.despine()
    return self.ax