-
Notifications
You must be signed in to change notification settings - Fork 0
Stacked Barplot API
Ilia Popov edited this page May 30, 2025
·
2 revisions
This API provides a function to create a stacked bar plot from KrakenParser relative abundance tables.
| 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 groupsTYPE: str or List[str]DEFAULT: "tab20"
|
bar_width |
Width parameter for bars TYPE: floatDEFAULT: 0.6
|
edgecolor |
Color of the edges around each stacked area TYPE: strDEFAULT: "black"
|
edge_linewidth |
Line width of group borders in each stacked bar TYPE: floatDEFAULT: 0.3
|
title |
Plot title TYPE: str or NoneDEFAULT: None
|
title_fontsize |
Font size of the title TYPE: floatDEFAULT: 16.0
|
title_color |
Font color of the title TYPE: strDEFAULT: "black"
|
title_weight |
Font weight of the title TYPE: strDEFAULT: "normal"
|
title_style |
Font style of the title TYPE: strDEFAULT: "normal"
|
xlabel |
Label for the x-axis TYPE: strDEFAULT: "Samples"
|
xlabel_fontsize |
Font size for x-axis label TYPE: floatDEFAULT: 12.0
|
xlabel_color |
Font color of x-axis label TYPE: strDEFAULT: "black"
|
xlabel_weight |
Font weight of x-axis label TYPE: strDEFAULT: "normal"
|
xlabel_style |
Font style of x-axis label TYPE: strDEFAULT: "normal"
|
ylabel |
Label for the y-axis TYPE: strDEFAULT: "Total Completeness"
|
ylabel_fontsize |
Font size for y-axis label TYPE: floatDEFAULT: 12.0
|
ylabel_color |
Font color of y-axis label TYPE: strDEFAULT: "black"
|
ylabel_weight |
Font weight of y-axis label TYPE: strDEFAULT: "normal"
|
ylabel_style |
Font style of y-axis label TYPE: strDEFAULT: "normal"
|
xticks_rotation |
Rotation angle of x-axis tick labels TYPE: floatDEFAULT: 45.0
|
xticks_ha |
Horizontal alignment of x-axis tick labels TYPE: strDEFAULT: "center"
|
xticks_fontsize |
Font size of x-axis tick labels TYPE: floatDEFAULT: 12.0
|
xticks_color |
Font color of x-axis tick labels TYPE: strDEFAULT: "black"
|
xticks_weight |
Font weight of x-axis tick labels TYPE: strDEFAULT: "normal"
|
xticks_style |
Font style of x-axis tick labels TYPE: strDEFAULT: "normal"
|
grid |
Whether to show grid lines TYPE: boolDEFAULT: True
|
grid_linestyle |
Line style for the grid (e.g. "--")TYPE: strDEFAULT: "--"
|
grid_alpha |
Transparency of grid lines TYPE: floatDEFAULT: 0.7
|
legend_fontsize |
Font size of the legend TYPE: floatDEFAULT: 9.0
|
legend_fontstyle |
Font style of the legend TYPE: str DEFAULT: "italic"
|
legend_loc |
Location of the legend TYPE: strDEFAULT: "upper left"
|
legend_bbox |
Manual anchor for legend box TYPE: Tuple[float, float]DEFAULT: (1.05, 1)
|
show_legend |
Whether to display legend TYPE: boolDEFAULT: True
|
background_color |
Background color of the figure TYPE: str or NoneDEFAULT: None
|
Returns an object with:
-
fig: the Matplotlib figure -
ax: the Matplotlib axis
The object includes .plotfig() and .savefig() methods for convenience.
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:

To save the plot:
kpstbar.savefig("pic_name.png", dpi=600)