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

Grouped Bar Chart

Signature

sp.build_grouped_bar(
    title: str,
    category_labels: list[str],
    series_values: list[float],
    *,
    show_values: bool = False,
    series_names: 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,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
) -> Chart

Aliases: sp.grouped_bar


Description

Grouped bar chart for comparing multiple series across categories.

series_values must be a flat list of length n_categories × n_series, row-major (category-first).


Parameters

ParameterTypeDefaultDescription
titlestrrequiredChart title
category_labelslist[str]requiredCategory names on X axis
series_valueslist[float]requiredFlat values: [cat0_s0, cat0_s1, cat1_s0, cat1_s1, ...]
show_valuesboolFalseShow value labels
series_nameslist[str] | NoneNoneSeries names for legend
palettelist[int] | NoneNoneCustom color palette
widthint900Canvas width in pixels
heightint480Canvas height in pixels
legend_positionstr"right"Legend position
gridlinesboolFalseShow grid lines

Returns

Chart


Examples

import seraplot as sp
categories = ["Q1", "Q2", "Q3", "Q4"]
values = [
    120.0, 90.0, 150.0,
    130.0, 110.0, 140.0,
    100.0, 95.0,  160.0,
    140.0, 120.0, 175.0,
]
logo = "https://raw.githubusercontent.com/feur25/seraplot-documentation/main/logo.png"
hover = sp.build_hover_json(categories * 3, images=[logo] * len(categories * 3))
chart = (
    sp.build_grouped_bar(
        "Quarterly Sales by Product",
        category_labels=categories,
        series_values=values,
        series_names=["Product A", "Product B", "Product C"],
        show_values=True,
        gridlines=True,
        legend_position="bottom",
        hover_json=hover,
    )
    .set_bg(None)
)
const sp = require('seraplot');
const categories = ["Q1", "Q2", "Q3", "Q4"]
const values = [
    120.0, 90.0, 150.0,
    130.0, 110.0, 140.0,
    100.0, 95.0,  160.0,
    140.0, 120.0, 175.0,
]
const logo = "https://raw.githubusercontent.com/feur25/seraplot-documentation/main/logo.png"
const hover = sp.build_hover_json(categories * 3,
[logo])
const chart = (
    sp.build_grouped_bar("Quarterly Sales by Product",
categories,
{
    series_values: values,
    series_names: ["Product A", "Product B", "Product C"],
    show_values: true,
    gridlines: true,
    legend_position: "bottom",
    hover_json: hover
})
    .set_bg(null)
)
import * as sp from 'seraplot';
const categories: string[] = ["Q1", "Q2", "Q3", "Q4"]
const values: number[] = [
    120.0, 90.0, 150.0,
    130.0, 110.0, 140.0,
    100.0, 95.0,  160.0,
    140.0, 120.0, 175.0,
]
const logo: string = "https://raw.githubusercontent.com/feur25/seraplot-documentation/main/logo.png"
const hover = sp.build_hover_json(categories * 3,
[logo])
const chart = (
    sp.build_grouped_bar("Quarterly Sales by Product",
categories,
{
    series_values: values,
    series_names: ["Product A", "Product B", "Product C"],
    show_values: true,
    gridlines: true,
    legend_position: "bottom",
    hover_json: hover
})
    .set_bg(null)
)
▶ Live Preview

See also

Signature

sp.build_grouped_bar(
    title: str,
    category_labels: list[str],
    series_values: list[float],
    *,
    show_values: bool = False,
    series_names: 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,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
) -> Chart

Aliases: sp.grouped_bar


Description

Graphique en barres groupées pour comparer plusieurs séries sur les mêmes catégories.

series_values doit être une liste plate de longueur n_catégories × n_séries, en ordre catégorie-major.


Paramètres

ParamètreTypeDéfautDescription
titlestrrequisTitre du graphique
category_labelslist[str]requisNoms des catégories sur l'axe X
series_valueslist[float]requisValeurs plates : [cat0_s0, cat0_s1, cat1_s0, cat1_s1, ...]
show_valuesboolFalseAfficher les étiquettes de valeur
series_nameslist[str] | NoneNoneNoms des séries pour la légende
palettelist[int] | NoneNonePalette de couleurs personnalisée
widthint900Largeur du canvas
heightint480Hauteur du canvas
legend_positionstr"right"Position de la légende
gridlinesboolFalseLignes de grille

Retourne

Chart


Exemples

import seraplot as sp

categories = ["T1", "T2", "T3", "T4"]
valeurs = [
    120.0, 90.0, 150.0,
    130.0, 110.0, 140.0,
    100.0, 95.0,  160.0,
    140.0, 120.0, 175.0,
]

chart = (
    sp.build_grouped_bar(
        "Ventes trimestrielles par produit",
        category_labels=categories,
        series_values=valeurs,
        series_names=["Produit A", "Produit B", "Produit C"],
        show_values=True,
        gridlines=True,
        legend_position="bottom",
    )
    .set_bg(None)
)

Voir aussi