Bar Plot#
VueCore is a Python package for creating interactive and static visualizations of multi-omics data. It is part of a broader ecosystem of tools—including ACore for data processing and VueGen for automated reporting—that together enable end-to-end workflows for omics analysis.
This notebook demonstrates how to generate bar plots using plotting functions from VueCore. We showcase basic and advanced plot configurations, highlighting key customization options such as grouping, color mapping, text annotations, and export to multiple file formats.
Notebook structure#
First, we will set up the work environment by installing the necessary packages and importing the required libraries. Next, we will create basic and advanced bar plots.
Credits and Contributors#
This notebook was created by Sebastián Ayala-Ruano under the supervision of Henry Webel and Alberto Santos, head of the Multiomics Network Analytics Group (MoNA) at the Novo Nordisk Foundation Center for Biosustainability (DTU Biosustain).
You can find more details about the project in this GitHub repository.
0. Work environment setup#
0.1. Installing libraries and creating global variables for platform and working directory#
To run this notebook locally, you should create a virtual environment with the required libraries. If you are running this notebook on Google Colab, everything should be set.
# VueCore library
%pip install vuecore
0.2. Importing libraries#
# Imports
import pandas as pd
import numpy as np
from pathlib import Path
from vuecore.plots.basic.bar import create_bar_plot
0.3. Create sample data#
We create a synthetic dataset representing the relative abundances of common bacterial genera across various environmental samples.
# Set a random seed for reproducibility of the synthetic data
np.random.seed(42)
# Sample types and bacterial genera
sample_types = ["Soil", "Freshwater", "Ocean", "Sediment", "Wastewater"]
genera = [
"Pseudomonas", "Bacillus", "Escherichia", "Streptococcus",
"Lactobacillus", "Bacteroides", "Clostridium", "Staphylococcus",
"Enterobacter", "Klebsiella", "Salmonella", "Shigella", "Vibrio"
]
def make_sample(sample: str, genera: list[str]) -> list[dict]:
"""
Generate synthetic microbial abundance data for a single sample.
Parameters
----------
sample : str
The sample type (e.g., 'Soil', 'Ocean', etc).
genera : list[str]
List of all possible bacterial genera.
Returns
-------
list[dict]
A list of dictionaries, each containing: Sample name, Genus,
Relative abundance, and Genera count.
"""
# Randomly pick a subset of genera present in this sample
selected = np.random.choice(genera, np.random.randint(5, len(genera) + 1), replace=False)
# Generate random raw abundances (shifted by +0.1 to avoid zeros)
raw = np.random.rand(len(selected)) + 0.1
# Normalize abundances so they sum to exactly 100%
abundances = (raw / raw.sum()) * 100
# Count how many genera are present
genera_count = len(selected)
# Store results into list of dicts
return [
{"Sample": sample,
"Genus": genus,
"Relative_Abundance": abund,
"Genera_Count": genera_count}
for genus, abund in zip(selected, abundances)
]
# Generate full dataset by combining all samples
abund_df = pd.DataFrame(
[row for sample in sample_types for row in make_sample(sample, genera)]
)
abund_df.head()
Sample | Genus | Relative_Abundance | Genera_Count | |
---|---|---|---|---|
0 | Soil | Klebsiella | 4.142384 | 11 |
1 | Soil | Shigella | 12.807301 | 11 |
2 | Soil | Pseudomonas | 2.667787 | 11 |
3 | Soil | Vibrio | 14.020172 | 11 |
4 | Soil | Bacteroides | 17.713758 | 11 |
1. Basic Bar Plot#
A basic bar plot can be created by simply providing the x
and y
columns from the DataFrame, along with style options like title
.
# Create a df with unique samples and their genera counts
bar_plot_basic_df = abund_df.drop_duplicates(subset="Sample")[["Sample", "Genera_Count"]]
# Define output path for the basic png plot
file_path_basic_png = Path(output_dir) / "bar_plot_basic.png"
# Generate the basic bar plot
bar_plot_basic = create_bar_plot(
data=bar_plot_basic_df,
x="Sample",
y="Genera_Count",
title="Genera Count by Sample Type",
file_path=file_path_basic_png,
)
bar_plot_basic.show()
[VueCore] Chrome not found. Attempting automatic install using `kaleido.get_chrome_sync()`...
[VueCore] Plot saved to outputs/bar_plot_basic.png
2. Advanced Bar Plot#
Here is an example of an advanced stacked bar plot
with more descriptive parameters, including color grouping
, text annotations
, hover tooltips
, and export to HTML
.
# Define the output file path for the HTML plot
file_path_adv_html = Path(output_dir) / "bar_plot_advanced.html"
# Generate the advanced stacked bar plot
bar_plot_adv = create_bar_plot(
data=abund_df,
x="Sample",
y="Relative_Abundance",
color="Genus",
barmode="stack",
title="Taxonomic Profile of Environmental Samples",
subtitle="Relative Abundance of Bacterial Genera",
labels={
"Sample": "Environmental Sample Type",
"Relative_Abundance": "Relative Abundance (%)",
"Genus": "Genus"
},
hover_name="Genus",
hover_data=["Relative_Abundance"],
opacity=0.9,
file_path=file_path_adv_html,
)
bar_plot_adv.show()
[VueCore] Plot saved to outputs/bar_plot_advanced.html