Skip to content

bs_seaborn module

Some Seaborn plotting utilities:

  • bs_sns_get_legend: get the Legend object of a Seaborn plot
  • bs_sns_bar_x_byf: make a bar plot of x by f
  • bs_sns_bar_x_byfg: make a bar plot of x by f and g
  • bs_sns_plot_density: basic density plot
  • bs_sns_density_estimates: plots the densities of estimates of several coefficients with several methods, superposed by methods and faceted by coefficients.

bs_regplot(data, x, y, title=None, line_color='red', save=None)

Draw a seaborn regplot with title and legend.

Parameters:

Name Type Description Default
data DataFrame

dataframe, should contain columns x and y

required
x str

column name of x

required
y str

column name of y

required
title str | None

title of plot

None
line_color str

color of the regression line. Red by default

'red'
save str | None

where to save it, if requested

None

Returns:

Type Description
Axes

the plot

Source code in bs_python_utils/bs_seaborn.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def bs_regplot(
    data: pd.DataFrame,
    x: str,
    y: str,
    title: str | None = None,
    line_color: str = "red",
    save: str | None = None,
) -> Axes:
    """Draw a seaborn regplot with title and legend.

    Args:
        data: dataframe, should contain columns `x` and `y`
        x: column name of x
        y: column name of y
        title: title of plot
        line_color: color of the regression line. Red by default
        save: where to save it, if requested

    Returns:
        the plot
    """
    sns.set_style("whitegrid")
    g = sns.regplot(
        data=data,
        x=x,
        y=y,
        label="Data",
        line_kws={"label": "Linear Fit (95% CI)", "color": line_color},
    )
    if title:
        plt.title(title)
    plt.legend(loc="best")
    if save:
        plt.savefig(f"{save}.png", dpi=400)
    return cast(Axes, g)

bs_sns_bar_x_byf(df, xstr, fstr, statistic=np.mean, label_x=None, label_f=None, title=None)

Make a bar plot of x by f.

Parameters:

Name Type Description Default
df DataFrame

dataframe, should contain columns xstr and fstr

required
xstr str

column name of x

required
fstr str

column name of f

required
statistic Callable

statistic to plot (by default, the mean)

mean
label_x str | None

label of x

None
label_f str | None

label of f

None
title str | None

title of plot

None

Returns:

Type Description
Axes

the plot.

Source code in bs_python_utils/bs_seaborn.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def bs_sns_bar_x_byf(
    df: pd.DataFrame,
    xstr: str,
    fstr: str,
    statistic: Callable = np.mean,
    label_x: str | None = None,
    label_f: str | None = None,
    title: str | None = None,
) -> Axes:
    """Make a bar plot of `x` by `f`.

    Args:
        df: dataframe, should contain columns `xstr` and `fstr`
        xstr: column name of x
        fstr: column name of f
        statistic: statistic to plot (by default, the mean)
        label_x: label of x
        label_f: label of f
        title: title of plot

    Returns:
        the plot.
    """
    fig, ax = plt.subplots()
    gbar = sns.barplot(
        x=fstr,
        y=xstr,
        data=df,
        estimator=statistic,
        errcolor="r",
        errwidth=0.75,
        capsize=0.2,
        ax=ax,
    )
    xlab = fstr if label_f is None else label_f
    ylab = xstr if label_x is None else label_x
    ax.set_xlabel(xlab)
    ax.set_ylabel(ylab)
    if title is not None:
        ax.set_title(title)
    return cast(Axes, gbar)

bs_sns_bar_x_byfg(df, xstr, fstr, gstr, statistic=np.mean, label_x=None, label_f=None, label_g=None, title=None)

Make a bar plot of x by f and g

Parameters:

Name Type Description Default
df DataFrame

dataframe, should contain columns xstr, fstr, and gstr

required
xstr str

column name of x

required
fstr str

column name of f

required
gstr str

column name of g

required
statistic Callable

statistic to plot (by default, the mean)

mean
label_x str | None

label of x

None
label_f str | None

label of f

None
label_g str | None

label of g in legend

None
title str | None

title of plot

None

Returns:

Type Description
Axes

the plot.

Source code in bs_python_utils/bs_seaborn.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def bs_sns_bar_x_byfg(
    df: pd.DataFrame,
    xstr: str,
    fstr: str,
    gstr: str,
    statistic: Callable = np.mean,
    label_x: str | None = None,
    label_f: str | None = None,
    label_g: str | None = None,
    title: str | None = None,
) -> Axes:
    """Make a bar plot of x by f and g

    Args:
        df: dataframe, should contain columns  `xstr`, `fstr`,  and `gstr`
        xstr: column name of x
        fstr: column name of f
        gstr: column name of g
        statistic: statistic to plot (by default, the mean)
        label_x: label of x
        label_f: label of f
        label_g: label of g in legend
        title: title of plot

    Returns:
        the plot.
    """
    _, ax = plt.subplots()
    gbar = sns.barplot(
        x=fstr,
        y=xstr,
        data=df,
        hue=gstr,
        estimator=statistic,
        errcolor="r",
        errwidth=0.75,
        capsize=0.2,
        ax=ax,
    )
    xlab = fstr if label_f is None else label_f
    ylab = xstr if label_x is None else label_x
    ax.set_xlabel(xlab)
    ax.set_ylabel(ylab)
    if label_g is not None:
        plt.gca().legend(title=label_g)
    if title is not None:
        ax.set_title(title)
    return cast(Axes, gbar)

bs_sns_density_estimates(df, true_values, method_string='Estimator', coeff_string='Parameter', estimate_string='Estimate', max_cols=3)

Plots the densities of estimates of several coefficients with several methods, superposed by methods and faceted by coefficients.

Parameters:

Name Type Description Default
df DataFrame

contains columns method_string, coeff_name, estimate_value

required
true_values ndarray

the true values of the coefficients

required
method_string str | None

the name of the column that indicates the method

'Estimator'
coeff_string str | None

the name of the column that indicates the coefficient

'Parameter'
estimate_string str | None

the name of the column that gives the value of the estimate

'Estimate'
max_cols int

we wrap after that

3

Returns:

Type Description
FacetGrid

the FacetGrid plot.

Source code in bs_python_utils/bs_seaborn.py
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def bs_sns_density_estimates(
    df: pd.DataFrame,
    true_values: np.ndarray,
    method_string: str | None = "Estimator",
    coeff_string: str | None = "Parameter",
    estimate_string: str | None = "Estimate",
    max_cols: int = 3,
) -> sns.FacetGrid:
    """
    Plots the densities of estimates of several coefficients with several methods,
    superposed by methods and faceted by coefficients.

    Args:
        df: contains columns `method_string`, `coeff_name`, `estimate_value`
        true_values: the true values of the coefficients
        method_string: the name of the column that indicates the method
        coeff_string: the name of the column that indicates the coefficient
        estimate_string: the name of the column that gives the value of the estimate
        max_cols: we wrap after that

    Returns:
        the `FacetGrid` plot.

    """
    g = sns.FacetGrid(
        data=df,
        sharex=False,
        sharey=False,
        hue=method_string,
        col=coeff_string,
        col_wrap=max_cols,
    )
    g.map(sns.kdeplot, estimate_string)
    g.set_titles("{col_name}")
    for true_val, ax in zip(true_values, g.axes.ravel(), strict=True):
        ax.vlines(true_val, *ax.get_ylim(), color="k", linestyles="dashed")
    g.add_legend()

    return g

bs_sns_plot_density(df, var_name, save_to=None)

plots the density of a variable

Parameters:

Name Type Description Default
df DataFrame

dataframe, should contain column var_name

required
var_name str

the name of a continuous variable

required
save_to str | None

(maybe) where we save the plot, with .png extension.

None
Source code in bs_python_utils/bs_seaborn.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
def bs_sns_plot_density(
    df: pd.DataFrame, var_name: str, save_to: str | None = None
) -> None:
    """plots the density of a variable

    Args:
        df: dataframe, should contain column `var_name`
        var_name:  the name of a continuous variable
        save_to: (maybe) where we save the plot, with `.png` extension.
    """
    var_y = df[var_name].values
    var_fig = sns.kdeplot(var_y)
    var_fig.axvline(x=0, c="k", ls="dashed")
    var_fig.set_title(f"Density of the {var_name}")
    var_fig.set_xlabel(f"Value of {var_name}")
    var_fig.set_ylabel("Value of the density")
    if save_to:
        plt.savefig(f"{save_to}.png")