Skip to content

Stacked Barplot API

Ilia Popov edited this page May 30, 2025 · 2 revisions

Stacked Barplot FUNCTION

This API provides a function to create a stacked bar plot from KrakenParser relative abundance tables.


PARAMETERS

PARAMETER DESCRIPTION
df Input data as a pandas DataFrame
TYPE: pd.DataFrame
metadata Optional DataFrame with sample metadata (must include 'Sample_id')
TYPE: pd.DataFrame
metadata_group Column in metadata to group samples by for aggregation
TYPE: str
sample_order The order of columns (samples)
TYPE: list
figsize Size of the figure (width, height)
TYPE: Tuple[int, int]
DEFAULT: (14, 7)
cmap Colormap name (e.g. "tab20") or list of HEX colors for pathway groups
TYPE: str or List[str]
DEFAULT: "tab20"
bar_width Width parameter for bars
TYPE: float
DEFAULT: 0.6
edgecolor Color of the edges around each stacked area
TYPE: str
DEFAULT: "black"
edge_linewidth Line width of group borders in each stacked bar
TYPE: float
DEFAULT: 0.3
title Plot title
TYPE: str or None
DEFAULT: None
title_fontsize Font size of the title
TYPE: float
DEFAULT: 16.0
title_color Font color of the title
TYPE: str
DEFAULT: "black"
title_weight Font weight of the title
TYPE: str
DEFAULT: "normal"
title_style Font style of the title
TYPE: str
DEFAULT: "normal"
xlabel Label for the x-axis
TYPE: str
DEFAULT: "Samples"
xlabel_fontsize Font size for x-axis label
TYPE: float
DEFAULT: 12.0
xlabel_color Font color of x-axis label
TYPE: str
DEFAULT: "black"
xlabel_weight Font weight of x-axis label
TYPE: str
DEFAULT: "normal"
xlabel_style Font style of x-axis label
TYPE: str
DEFAULT: "normal"
ylabel Label for the y-axis
TYPE: str
DEFAULT: "Total Completeness"
ylabel_fontsize Font size for y-axis label
TYPE: float
DEFAULT: 12.0
ylabel_color Font color of y-axis label
TYPE: str
DEFAULT: "black"
ylabel_weight Font weight of y-axis label
TYPE: str
DEFAULT: "normal"
ylabel_style Font style of y-axis label
TYPE: str
DEFAULT: "normal"
xticks_rotation Rotation angle of x-axis tick labels
TYPE: float
DEFAULT: 45.0
xticks_ha Horizontal alignment of x-axis tick labels
TYPE: str
DEFAULT: "center"
xticks_fontsize Font size of x-axis tick labels
TYPE: float
DEFAULT: 12.0
xticks_color Font color of x-axis tick labels
TYPE: str
DEFAULT: "black"
xticks_weight Font weight of x-axis tick labels
TYPE: str
DEFAULT: "normal"
xticks_style Font style of x-axis tick labels
TYPE: str
DEFAULT: "normal"
grid Whether to show grid lines
TYPE: bool
DEFAULT: True
grid_linestyle Line style for the grid (e.g. "--")
TYPE: str
DEFAULT: "--"
grid_alpha Transparency of grid lines
TYPE: float
DEFAULT: 0.7
legend_fontsize Font size of the legend
TYPE: float
DEFAULT: 9.0
legend_fontstyle Font style of the legend
TYPE: str
DEFAULT: "italic"
legend_loc Location of the legend
TYPE: str
DEFAULT: "upper left"
legend_bbox Manual anchor for legend box
TYPE: Tuple[float, float]
DEFAULT: (1.05, 1)
show_legend Whether to display legend
TYPE: bool
DEFAULT: True
background_color Background color of the figure
TYPE: str or None
DEFAULT: None

RETURNS

Returns an object with:

  • fig: the Matplotlib figure
  • ax: the Matplotlib axis

The object includes .plotfig() and .savefig() methods for convenience.


USAGE EXAMPLE

Where to get demo data? Here

import krakenparser as kp
import pandas as pd

df = pd.read_csv("demo_data/counts/csv_relabund/counts_species_2.csv")

kpstbar = kp.stacked_barplot(
    df,
    figsize=(12, 6),
    bar_width = 0.6,
    edge_linewidth = 0.6,
    cmap="tab20",
    xticks_fontsize=12,
    background_color=None
    )

# Show the figure
kpstbar.plotfig()

Sample output:

kpstbar

To save the plot:

kpstbar.savefig("pic_name.png", dpi=600)

Clone this wiki locally