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

Heatmap

Signature

sp.build_heatmap(
    title: str,
    labels: list[str],
    flat_matrix: list[float],
    *,
    show_values: bool = True,
    color_low: int = 0,
    color_mid: int = 0,
    color_high: int = 0,
    col_labels: list[str] | None = None,
    width: int = 900,
    height: int = 480,
    x_label: str = "",
    y_label: str = "",
    gridlines: bool = False,
    palette: list[int] | None = None,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
) -> Chart

Aliases: sp.heatmap


Description

Color-coded matrix heatmap. Values are automatically normalized for color mapping.

flat_matrix must contain n_rows × n_cols values in row-major order.

labels = row labels. col_labels = column labels (if different from rows).


Parameters

ParameterTypeDefaultDescription
titlestrrequiredChart title
labelslist[str]requiredRow labels
flat_matrixlist[float]requiredMatrix values, row-major
show_valuesboolTrueOverlay numeric values in cells
color_lowintautoLow value color (hex int)
color_midintautoMid value color
color_highintautoHigh value color
col_labelslist[str] | NoneNoneColumn labels (defaults to labels)

Returns

Chart


Examples

Correlation matrix

import seraplot as sp
import numpy as np
features = ["Age", "Income", "Score", "Visits"]
n = len(features)
matrix = np.corrcoef(np.random.randn(4, 100)).flatten().tolist()
chart = sp.build_heatmap(
    "Feature Correlation Matrix",
    labels=features,
    flat_matrix=matrix,
    color_low=0x3b82f6,
    color_mid=0xffffff,
    color_high=0xef4444,
    show_values=True,
)
const sp = require('seraplot');
const features = ["Age", "Income", "Score", "Visits"];
// Pre-computed 4×4 correlation matrix (row-major)
const matrix = [
   1.00,  0.72, -0.15,  0.43,
   0.72,  1.00,  0.08,  0.61,
  -0.15,  0.08,  1.00, -0.27,
   0.43,  0.61, -0.27,  1.00,
];
const chart = sp.build_heatmap("Feature Correlation Matrix", features, {
    flat_matrix: matrix,
    color_low: 0x3b82f6,
    color_mid: 0xffffff,
    color_high: 0xef4444,
    show_values: true,
});
import * as sp from 'seraplot';
const features: string[] = ["Age", "Income", "Score", "Visits"];
// Pre-computed 4×4 correlation matrix (row-major)
const matrix: number[] = [
   1.00,  0.72, -0.15,  0.43,
   0.72,  1.00,  0.08,  0.61,
  -0.15,  0.08,  1.00, -0.27,
   0.43,  0.61, -0.27,  1.00,
];
const chart = sp.build_heatmap("Feature Correlation Matrix", features, {
    flat_matrix: matrix,
    color_low: 0x3b82f6,
    color_mid: 0xffffff,
    color_high: 0xef4444,
    show_values: true,
});
▶ Live Preview

See also

Signature

sp.build_heatmap(
    title: str,
    labels: list[str],
    flat_matrix: list[float],
    *,
    show_values: bool = True,
    color_low: int = 0,
    color_mid: int = 0,
    color_high: int = 0,
    col_labels: list[str] | None = None,
    width: int = 900,
    height: int = 480,
    x_label: str = "",
    y_label: str = "",
    gridlines: bool = False,
    palette: list[int] | None = None,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
) -> Chart

Aliases: sp.heatmap


Description

Matrice colorée (heatmap). Les valeurs sont normalisées automatiquement pour le mappage de couleurs.

flat_matrix doit contenir n_lignes × n_colonnes valeurs en ordre ligne-major. labels = étiquettes de lignes. col_labels = étiquettes de colonnes.


Paramètres

ParamètreTypeDéfautDescription
titlestrrequisTitre du graphique
labelslist[str]requisÉtiquettes des lignes
flat_matrixlist[float]requisValeurs de la matrice, en ligne-major
show_valuesboolTrueAfficher les valeurs numériques dans les cellules
color_lowintautoCouleur pour les valeurs basses (hex int)
color_midintautoCouleur pour les valeurs médianes
color_highintautoCouleur pour les valeurs hautes
col_labelslist[str] | NoneNoneÉtiquettes de colonnes (par défaut = labels)

Retourne

Chart


Exemples

Matrice de corrélation

import seraplot as sp
import numpy as np

features = ["Age", "Revenu", "Score", "Visites"]
matrice = np.corrcoef(np.random.randn(4, 100)).flatten().tolist()

chart = sp.build_heatmap(
    "Matrice de corrélation des variables",
    labels=features,
    flat_matrix=matrice,
    color_low=0x3b82f6,
    color_mid=0xffffff,
    color_high=0xef4444,
    show_values=True,
)

Voir aussi