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

Horizontal Bar Chart

Signature

sp.build_hbar(
    title: str,
    labels: list[str],
    values: list[float],
    *,
    color_hex: int = 0,
    show_text: bool = True,
    color_groups: list[str] | None = None,
    width: int = 900,
    height: int = 500,
    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.hbar, sp.barh, sp.horizontal_bar


Description

Horizontal bar chart. Best for long category labels or ranking comparisons.


Parameters

ParameterTypeDefaultDescription
titlestrrequiredChart title
labelslist[str]requiredCategory labels
valueslist[float]requiredBar values
show_textboolTrueShow values on bars (default True for hbar)
sort_orderstr"none""asc", "desc", "none"
color_groupslist[str] | NoneNoneGroup names for color mapping
palettelist[int] | NoneNoneCustom hex color palette
backgroundstr | NoneNoneBackground CSS color
widthint900Width in pixels
heightint500Height in pixels

Returns

Chart


Examples

Top 10 ranking

import seraplot as sp
chart = sp.build_hbar(
    "Top Countries by GDP",
    labels=["USA", "China", "Germany", "Japan", "India", "UK", "France", "Brazil", "Canada", "Korea"],
    values=[25.0, 18.0, 4.1, 4.2, 3.7, 3.1, 2.9, 2.1, 2.0, 1.7],
    sort_order="desc",
    x_label="GDP (trillion USD)",
    show_text=True,
    width=900,
    height=460,
)
const sp = require('seraplot');
const chart = sp.build_hbar("Top Countries by GDP",
["USA", "China", "Germany", "Japan", "India", "UK", "France", "Brazil", "Canada", "Korea"],
{
    values: [25.0, 18.0, 4.1, 4.2, 3.7, 3.1, 2.9, 2.1, 2.0, 1.7],
    sort_order: "desc",
    x_label: "GDP (trillion USD)",
    show_text: true,
    width: 900,
    height: 460
})
import * as sp from 'seraplot';
const chart = sp.build_hbar("Top Countries by GDP",
["USA", "China", "Germany", "Japan", "India", "UK", "France", "Brazil", "Canada", "Korea"],
{
    values: [25.0, 18.0, 4.1, 4.2, 3.7, 3.1, 2.9, 2.1, 2.0, 1.7],
    sort_order: "desc",
    x_label: "GDP (trillion USD)",
    show_text: true,
    width: 900,
    height: 460
})
▶ Live Preview

See also

Signature

sp.build_hbar(
    title: str,
    labels: list[str],
    values: list[float],
    *,
    color_hex: int = 0,
    show_text: bool = True,
    color_groups: list[str] | None = None,
    width: int = 900,
    height: int = 500,
    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.hbar, sp.barh, sp.horizontal_bar


Description

Graphique en barres horizontales. Particulièrement adapté aux longues étiquettes de catégories ou aux classements.


Paramètres

ParamètreTypeDéfautDescription
titlestrrequisTitre du graphique
labelslist[str]requisÉtiquettes des catégories
valueslist[float]requisValeurs des barres
show_textboolTrueAfficher les valeurs sur les barres
sort_orderstr"none""asc", "desc" ou "none"
color_groupslist[str] | NoneNoneNoms de groupe pour la coloration
palettelist[int] | NoneNonePalette de couleurs hex
backgroundstr | NoneNoneCouleur de fond CSS
widthint900Largeur en pixels
heightint500Hauteur en pixels

Retourne

Chart


Exemples

Top 10 classement

import seraplot as sp

chart = sp.build_hbar(
    "Top pays par PIB",
    labels=["USA", "Chine", "Allemagne", "Japon", "Inde", "UK", "France", "Brésil", "Canada", "Corée"],
    values=[25.0, 18.0, 4.1, 4.2, 3.7, 3.1, 2.9, 2.1, 2.0, 1.7],
    sort_order="desc",
    x_label="PIB (billions USD)",
    show_text=True,
)

Voir aussi