Skip to content

Combined Stacked Barplot & Streamgraph

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

Stacked Barplot and Streamgraph can be combined to provide the most comprehensive view on pathway completeness grouped by functional categories across samples possible.

Step 1: Create Stacked Barplot

Detailed instructions can be found at Stacked Barplot API page

df = pd.read_csv("KEGGaNOG_multi_output/merged_pathways.tsv", sep="\t")

kgnstbar = kgn.stacked_barplot(
    df,
    figsize=(14, 7),
    cmap="tab20",
    xlabel="Samples",
    ylabel="Completeness",
    xticks_fontsize=12,
    # To create combined figure it is VERY important to use `None` background_color
    background_color=None
    )

# Save the figure using `transparent= False `
kgnstbar.savefig("kgnstbar.png", transparent=False)
kgnstbar.plotfig()

Sample output:

kgnstbar_OLD

Step 2: Create Streamgraph

Detailed instructions can be found at Streamgraph API page

df = pd.read_csv("KEGGaNOG_multi_output/merged_pathways.tsv", sep="\t")

kgnstream = kgn.streamgraph(
    df,
    figsize=(14, 7),
    cmap="tab20",
    # To create combined figure it is VERY important adjust fill_alpha
    fill_alpha=0.2,
    xlabel="Samples",
    ylabel="Completeness",
    edgecolor="black",
    xticks_fontsize=12,
    # To create combined figure it is VERY important to use `None` background_color
    background_color=None
    )

# Save the figure using `transparent=True`
kgnstream.savefig("kgnstream.png", transparent=True)
kgnstream.plotfig()

Sample output:

kgnstream_OLD

Step 3: Create combined figure

With transparent background

from PIL import Image

# Load both images as RGBA
bottom = Image.open("kgnstream.png").convert("RGBA")
top    = Image.open("kgnstbar.png").convert("RGBA")

# Paste the top image over the bottom using its own alpha channel
bottom.paste(top, (0, 0), top)

# Save &/or show
bottom.save("combined.png")
bottom.show()

Sample output:

combined_final_OLD

With white background

from PIL import Image

# Load both images as RGBA
bottom = Image.open("imgs/kgnstream.png").convert("RGBA")
top    = Image.open("imgs/kgnstbar.png").convert("RGBA")

# Paste the top image over the bottom using its own alpha channel
bottom.paste(top, (0, 0), top)

# Now flatten onto a white background
white_bg = Image.new("RGB", bottom.size, (255, 255, 255))
# Use alpha as mask
white_bg.paste(bottom, mask=bottom.split()[3])  

# Save &/or show
white_bg.save("combined_white.png")
white_bg.show()

Sample output:

combined_white_OLD

Clone this wiki locally