alphapepttools.pl.barplot

Contents

alphapepttools.pl.barplot#

alphapepttools.pl.barplot(ax, data, grouping_column=None, value_column=None, direct_columns=None, color=(np.float64(0.21299500192233756), np.float64(0.5114186851211072), np.float64(0.730795847750865), np.float64(1.0)), color_dict=None, subgroup_column=None, width=0.4, alpha=0.5, legend=None, legend_kwargs=None)#

Plot a bar chart from a DataFrame or AnnData object

Creates a bar plot showing means with error bars (standard deviation) for grouped data. Each bar represents the mean of values within a group, with error bars showing the standard deviation. Bars have semi-transparent fill with opaque black outlines.

Two modes of operation: 1. Grouping mode: Use grouping_column/value_column to group data by categories 2. Direct mode: Use direct_columns to compare multiple columns directly

Parameters:
  • ax (Axes) – Matplotlib axes object to plot on.

  • data (AnnData | DataFrame) – Data containing grouping and value columns or direct columns to plot.

  • grouping_column (list[str] | None (default: None)) – Column containing the groups to compare (categorical). Used with value_column for grouped comparisons. By default None.

  • value_column (list[str] | None (default: None)) – Column whose values should be plotted (numeric). Used with grouping_column for grouped comparisons. By default None.

  • direct_columns (list[str] | None (default: None)) – List of column names to compare directly. Each column becomes a separate bar. Overrides grouping_column and value_column. By default None.

  • color (tuple (default: (np.float64(0.21299500192233756), np.float64(0.5114186851211072), np.float64(0.730795847750865), np.float64(1.0)))) – Default color for all bars. By default BaseColors.get(“blue”).

  • color_dict (dict | None (default: None)) – Dictionary mapping group labels to specific colors. Overrides the color parameter for specified groups. By default None.

  • subgroup_column (str | None (default: None)) – Optional column for subgroups within each main group. Each subgroup will be plotted as a separate bar within the main group. By default None.

  • width (float (default: 0.4)) – Width of the bars. By default 0.4.

  • alpha (float (default: 0.5)) – Transparency of the bars (0-1). By default 0.5.

  • legend (str | Legend | None (default: None)) – Legend to add to the plot, by default None. If “auto”, a legend is created from the color_dict. By default None.

  • legend_kwargs (dict | None (default: None)) – Additional keyword arguments for the matplotlib legend function. By default None.

Return type:

None

Returns:

None

Examples

Grouped comparison (long format):

import pandas as pd
import anndata as ad
from alphapepttools.pl.figure import create_figure
import alphapepttools as apt

data = pd.DataFrame({"intensity": [1, 2, 3, 4, 5, 6, 7]})
obs = pd.DataFrame({"group": ["A", "A", "B", "B", "B", "C", "C"]})
adata = ad.AnnData(X=data.values, obs=obs, var=pd.DataFrame(index=data.columns))

fig, axm = create_figure(1, 1, figsize=(6, 4))
ax = axm.next()
apt.pl.barplot(
    ax=ax,
    data=adata,
    grouping_column="group",
    value_column="intensity",
    color_dict={"A": "red", "B": "green", "C": "blue"},
)

Direct column comparison (wide format):

import pandas as pd
from alphapepttools.pl.figure import create_figure
import alphapepttools as apt

data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})

fig, axm = create_figure(1, 1, figsize=(6, 4))
ax = axm.next()
apt.pl.barplot(
    ax=ax,
    data=data,
    direct_columns=["A", "B", "C"],
    color_dict={"A": "red", "B": "green", "C": "blue"},
)

Notes

  • Error bars show standard deviation of values within each group

  • Bars have 50% transparency with opaque black outlines

  • When using direct_columns, each column’s mean is calculated across all rows

  • The subgroup_column allows for further subdivision within each main group

  • Missing values (NaN) are excluded from mean and std calculations