Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bar Chart

Signature

sp.build_bar_chart(
    title: str,
    labels: list[str],
    values: list[float],
    *,
    color_hex: int = 0,
    orientation: str = "v",
    show_text: bool = False,
    color_groups: list[str] | None = None,
    width: int = 900,
    height: int = 480,
    x_label: str = "",
    y_label: str = "",
    gridlines: bool = False,
    sort_order: str = "none",
    hover_json: str = "",
    legend_position: str = "right",
    palette: list[int] | None = None,
    series_names: list[str] | None = None,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
) -> Chart

Aliases: sp.bar, sp.bar_chart, sp.bars


Description

Renders a vertical or horizontal bar chart.


Parameters

ParameterTypeDefaultDescription
titlestrrequiredChart title
labelslist[str]requiredCategory labels
valueslist[float]requiredBar values
color_hexint0Single bar color as hex int (e.g. 0xFF5733)
orientationstr"v""v" = vertical, "h" = horizontal
show_textboolFalseShow value labels on bars
color_groupslist[str] | NoneNonePer-bar group name for coloring
widthint900Canvas width in pixels
heightint480Canvas height in pixels
x_labelstr""X-axis label
y_labelstr""Y-axis label
gridlinesboolFalseShow gridlines
sort_orderstr"none""asc", "desc", or "none"
hover_jsonstr""Custom hover tooltip JSON
legend_positionstr"right""right", "left", "top", "bottom"
palettelist[int] | NoneNoneCustom color palette as list of hex ints
backgroundstr | NoneNoneBackground color (e.g. "#0f172a") or None = transparent
no_x_axisboolFalseHide X axis
no_y_axisboolFalseHide Y axis

Returns

Chart — object with .html property containing the full self-contained HTML.


Examples

Basic bar chart

import seraplot as sp
labels = ["Jan", "Feb", "Mar", "Apr", "May"]
values = [1200.0, 1850.0, 2100.0, 1750.0, 2400.0]
logo = "https://raw.githubusercontent.com/feur25/seraplot-documentation/main/logo.png"
hover = sp.build_hover_json(labels, images=[logo] * len(labels))
chart = (
    sp.build_bar_chart(
        "Monthly Revenue",
        labels=labels,
        values=values,
        x_label="Month",
        y_label="Revenue (€)",
        gridlines=True,
        hover_json=hover,
    )
    .set_bg(None)
    .show_labels(position="top")
)
const sp = require('seraplot');
const labels = ["Jan", "Feb", "Mar", "Apr", "May"]
const values = [1200.0, 1850.0, 2100.0, 1750.0, 2400.0]
const logo = "https://raw.githubusercontent.com/feur25/seraplot-documentation/main/logo.png"
const hover = sp.build_hover_json(labels,
[logo])
const chart = (
    sp.build_bar_chart("Monthly Revenue",
labels,
{
    values: values,
    x_label: "Month",
    y_label: "Revenue (€)",
    gridlines: true,
    hover_json: hover
})
    .set_bg(null)
    .show_labels(position="top")
)
import * as sp from 'seraplot';
const labels: string[] = ["Jan", "Feb", "Mar", "Apr", "May"]
const values: number[] = [1200.0, 1850.0, 2100.0, 1750.0, 2400.0]
const logo: string = "https://raw.githubusercontent.com/feur25/seraplot-documentation/main/logo.png"
const hover = sp.build_hover_json(labels,
[logo])
const chart = (
    sp.build_bar_chart("Monthly Revenue",
labels,
{
    values: values,
    x_label: "Month",
    y_label: "Revenue (€)",
    gridlines: true,
    hover_json: hover
})
    .set_bg(null)
    .show_labels(position="top")
)
▶ Live Preview

Colored groups

chart = sp.build_bar_chart(
    "Products by Category",
    labels=["A1", "A2", "B1", "B2", "C1"],
    values=[10.0, 15.0, 8.0, 12.0, 20.0],
    color_groups=["Cat A", "Cat A", "Cat B", "Cat B", "Cat C"],
    legend_position="bottom",
)

Dark background

chart = sp.build_bar_chart(
    "Dark Theme",
    labels=["Q1", "Q2", "Q3", "Q4"],
    values=[300.0, 450.0, 380.0, 520.0],
    background="#0f172a",
    width=800,
    height=400,
)

See also

Signature

sp.build_bar_chart(
    title: str,
    labels: list[str],
    values: list[float],
    *,
    color_hex: int = 0,
    orientation: str = "v",
    show_text: bool = False,
    color_groups: list[str] | None = None,
    width: int = 900,
    height: int = 480,
    x_label: str = "",
    y_label: str = "",
    gridlines: bool = False,
    sort_order: str = "none",
    hover_json: str = "",
    legend_position: str = "right",
    palette: list[int] | None = None,
    series_names: list[str] | None = None,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
) -> Chart

Aliases: sp.bar, sp.bar_chart, sp.bars


Description

Affiche un graphique en barres vertical ou horizontal.


Paramètres

ParamètreTypeDéfautDescription
titlestrrequisTitre du graphique
labelslist[str]requisÉtiquettes des catégories
valueslist[float]requisValeurs des barres
color_hexint0Couleur unique (ex. 0xFF5733)
orientationstr"v""v" = vertical, "h" = horizontal
show_textboolFalseAfficher les valeurs sur les barres
color_groupslist[str] | NoneNoneGroupe par barre pour la coloration
widthint900Largeur du canvas en pixels
heightint480Hauteur du canvas en pixels
x_labelstr""Étiquette de l'axe X
y_labelstr""Étiquette de l'axe Y
gridlinesboolFalseAfficher les lignes de grille
sort_orderstr"none""asc", "desc" ou "none"
hover_jsonstr""JSON d'infobulle personnalisée
legend_positionstr"right""right", "left", "top", "bottom"
palettelist[int] | NoneNonePalette de couleurs personnalisée
backgroundstr | NoneNoneCouleur de fond ou None = transparent
no_x_axisboolFalseMasquer l'axe X
no_y_axisboolFalseMasquer l'axe Y

Retourne

Chart — objet avec la propriété .html contenant le HTML autonome complet.


Exemples

Graphique en barres simple

import seraplot as sp

labels = ["Jan", "Fév", "Mar", "Avr", "Mai"]
values = [1200.0, 1850.0, 2100.0, 1750.0, 2400.0]

chart = (
    sp.build_bar_chart(
        "Chiffre d'affaires mensuel",
        labels=labels,
        values=values,
        x_label="Mois",
        y_label="Chiffre d'affaires (€)",
        gridlines=True,
    )
    .set_bg(None)
    .show_labels(position="top")
)

Groupes colorés

chart = sp.build_bar_chart(
    "Produits par catégorie",
    labels=["A1", "A2", "B1", "B2", "C1"],
    values=[10.0, 15.0, 8.0, 12.0, 20.0],
    color_groups=["Cat A", "Cat A", "Cat B", "Cat B", "Cat C"],
    legend_position="bottom",
)

Voir aussi