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

Scatter Chart

Signature

sp.build_scatter_chart(
    title: str,
    x_values: list[float],
    y_values: list[float],
    *,
    color_hex: int = 0,
    show_text: bool = False,
    labels: list[str] | None = None,
    sizes: list[float] | None = None,
    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,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
    show_regression: bool = False,
    regression_type: str = "linear",
) -> Chart

Aliases: sp.scatter, sp.scatter_chart


Description

2D scatter plot with optional per-point sizing, grouping, labels, and regression line.


Parameters

ParameterTypeDefaultDescription
titlestrrequiredChart title
x_valueslist[float]requiredX coordinates
y_valueslist[float]requiredY coordinates
color_hexint0Uniform point color
show_textboolFalseShow point labels on chart
labelslist[str] | NoneNonePer-point label text
sizeslist[float] | NoneNonePer-point relative size (0.0–1.0)
color_groupslist[str] | NoneNonePer-point group name for color
show_regressionboolFalseOverlay regression line
regression_typestr"linear""linear" or "polynomial"
widthint900Width in pixels
heightint480Height in pixels
x_labelstr""X-axis label
y_labelstr""Y-axis label
gridlinesboolFalseShow gridlines
backgroundstr | NoneNoneBackground color

Returns

Chart


Examples

Basic scatter

Colored groups with regression

import seraplot as sp
import numpy as np
rng = np.random.default_rng(0)
x_a = rng.normal(0, 1, 200).tolist()
y_a = [xi * 1.5 + rng.normal() for xi in x_a]
x_b = rng.normal(3, 1, 200).tolist()
y_b = [xi * 0.5 + rng.normal() for xi in x_b]
chart = sp.build_scatter_chart(
    "Two Populations",
    x_values=x_a + x_b,
    y_values=y_a + y_b,
    color_groups=["Group A"] * 200 + ["Group B"] * 200,
    show_regression=True,
    regression_type="linear",
    x_label="X",
    y_label="Y",
)
const sp = require('seraplot');
function randn() {
  const u = 1 - Math.random(), v = Math.random();
  return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
}
function normal(mu, sigma, n) {
  return Array.from({ length: n }, () => mu + sigma * randn());
}
const xA = normal(0, 1, 200);
const yA = xA.map(x => x * 1.5 + randn());
const xB = normal(3, 1, 200);
const yB = xB.map(x => x * 0.5 + randn());
const chart = sp.build_scatter_chart("Two Populations", [...xA, ...xB], {
    y_values: [...yA, ...yB],
    color_groups: [...Array(200).fill("Group A"), ...Array(200).fill("Group B")],
    show_regression: true,
    regression_type: "linear",
    x_label: "X",
    y_label: "Y",
});
import * as sp from 'seraplot';
function randn(): number {
  const u = 1 - Math.random(), v = Math.random();
  return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
}
function normal(mu: number, sigma: number, n: number): number[] {
  return Array.from({ length: n }, () => mu + sigma * randn());
}
const xA: number[] = normal(0, 1, 200);
const yA: number[] = xA.map(x => x * 1.5 + randn());
const xB: number[] = normal(3, 1, 200);
const yB: number[] = xB.map(x => x * 0.5 + randn());
const chart = sp.build_scatter_chart("Two Populations", [...xA, ...xB], {
    y_values: [...yA, ...yB],
    color_groups: [...Array(200).fill("Group A"), ...Array(200).fill("Group B")],
    show_regression: true,
    regression_type: "linear",
    x_label: "X",
    y_label: "Y",
});
▶ Live Preview

See also

Signature

sp.build_scatter_chart(
    title: str,
    x_values: list[float],
    y_values: list[float],
    *,
    color_hex: int = 0,
    show_text: bool = False,
    labels: list[str] | None = None,
    sizes: list[float] | None = None,
    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,
    background: str | None = None,
    no_x_axis: bool = False,
    no_y_axis: bool = False,
    show_regression: bool = False,
    regression_type: str = "linear",
) -> Chart

Aliases: sp.scatter, sp.scatter_chart


Description

Nuage de points 2D avec taille par point, regroupement, étiquettes et droite de régression optionnels.


Paramètres

ParamètreTypeDéfautDescription
titlestrrequisTitre du graphique
x_valueslist[float]requisCoordonnées X
y_valueslist[float]requisCoordonnées Y
color_hexint0Couleur uniforme des points
show_textboolFalseAfficher les étiquettes de points sur le graphique
labelslist[str] | NoneNoneTexte d'étiquette par point
sizeslist[float] | NoneNoneTaille relative par point (0.0–1.0)
color_groupslist[str] | NoneNoneNom de groupe par point pour la couleur
show_regressionboolFalseSuperposer une droite de régression
regression_typestr"linear""linear" ou "polynomial"
widthint900Largeur en pixels
heightint480Hauteur en pixels
x_labelstr""Étiquette de l'axe X
y_labelstr""Étiquette de l'axe Y
gridlinesboolFalseLignes de grille
backgroundstr | NoneNoneCouleur de fond

Retourne

Chart


Exemples

import seraplot as sp
import numpy as np

rng = np.random.default_rng(0)
x_a = rng.normal(0, 1, 200).tolist()
y_a = [xi * 1.5 + rng.normal() for xi in x_a]
x_b = rng.normal(3, 1, 200).tolist()
y_b = [xi * 0.5 + rng.normal() for xi in x_b]

chart = sp.build_scatter_chart(
    "Deux populations",
    x_values=x_a + x_b,
    y_values=y_a + y_b,
    color_groups=["Groupe A"] * 200 + ["Groupe B"] * 200,
    show_regression=True,
    regression_type="linear",
    x_label="X",
    y_label="Y",
)

Voir aussi