Skip to content

deepbox/plot

Statistical & Structural Plots

Density, pair, joint, dendrogram, stem, strip, radar, polar, waterfall and quiver plots, plus 3-D surface, wireframe and scatter plots.

  • Statistical plots

Overview

  • Most of these functions draw on the current axes, like the core charts. jointplot and pairplot are different: they build and return a new Figure, which does not become the current figure. Render it with fig.renderSVG() or save it with saveFig(path, { figure }).
  • kdeplot takes a 1-D tensor and a bwMethod ("scott", "silverman" or a number). jointplot draws a scatter plot with a histogram of each variable on the margins. pairplot draws a grid of scatter plots with a histogram or density estimate on the diagonal.
  • plotDendrogram(linkage, nLeaves) takes the linkage as nested arrays whose rows are [clusterA, clusterB, distance, count].
  • surface, wireframe and scatter3d take plain nested arrays (arrays of numbers for scatter3d) and accept elevation and azimuth options.

Types

3

PlotTheme

interface

A theme configuration for consistent plot styling.

The active theme (see setTheme) is read when a figure or axes is created: Figure takes its default background from figureFacecolor, and Axes takes its facecolor, grid, text color and font sizes from the other fields (tick labels use fontSize - 2, axis labels and the legend fontSize, the title fontSize + 2). A figure that already exists keeps the look it was created with, so call setTheme before figure(). Options that are passed explicitly always win over the theme.

The high-level helpers (the ML plots such as plotRocCurve, kdeplot, pairplot, jointplot and plotDecisionBoundary) also take their default series colors from primaryColor and colorCycle. The theme does not change the per-chart default colors of plot, scatter, bar, barh and hist.

  • readonly figureFacecolor: string

    Background color for figure.

  • readonly axesFacecolor: string

    Background color for axes.

  • readonly primaryColor: string

    Default line/bar color.

  • readonly colorCycle: readonly string[]

    Color cycle for multiple series.

  • readonly fontSize: number

    Default font size: the size of axis labels and legends, in pixels.

  • readonly gridColor: string

    Grid color.

  • readonly gridVisible: boolean

    Whether grid is visible by default.

  • readonly textColor: string

    Color of the axes frame, ticks, tick labels, titles, axis labels and legend text.

ViolinplotOptions

type

Options of a violin plot: the common PlotOptions plus the layout and the density estimate.

type ViolinplotOptions = PlotOptions & {    readonly position?: number;    readonly width?: number;    readonly bandwidth?: "silverman" | "scott" | number;};

Plot3DOptions

type

Rendering options for 3D plot elements.

  • readonly label?: string

    Legend label.

  • readonly color?: Color

    Surface base color, wireframe line color or scatter marker color.

  • readonly linewidth?: number

    Wireframe line width in pixels (must be positive).

  • readonly size?: number

    Scatter marker radius in pixels (must be positive).

  • readonly elevation?: number

    Camera elevation above the xy plane in degrees (default 30).

  • readonly azimuth?: number

    Camera azimuth in degrees, measured from the +x axis toward +y (default -60).

  • readonly alpha?: number

    Opacity from 0 to 1.

Classes

3

Scatter3D

3D Scatter plot drawable.

Points are drawn far to near; points with a non-finite coordinate are skipped. The coordinates are copied when the drawable is created.

Members (6)
  • constructor(x: Float64Array, y: Float64Array, z: Float64Array, options?: Plot3DOptions)
  • readonly kind = "scatter3d"
  • getDataRange(): DataRange | null
  • drawSVG(ctx: SvgDrawContext): void
  • drawRaster(ctx: RasterDrawContext): void
  • getLegendEntries(): readonly LegendEntry[] | null

Surface3D

3D Surface plot drawable.

Draws shaded quadrilaterals far to near; cells with a non-finite corner are skipped. The grids are copied when the drawable is created, so later changes to the input arrays have no effect.

Members (6)
  • constructor(xGrid: Float64Array[], yGrid: Float64Array[], zGrid: Float64Array[], options?: Plot3DOptions)
  • readonly kind = "surface3d"
  • getDataRange(): DataRange | null
  • drawSVG(ctx: SvgDrawContext): void
  • drawRaster(ctx: RasterDrawContext): void
  • getLegendEntries(): readonly LegendEntry[] | null

Wireframe3D

3D Wireframe plot drawable.

Draws the grid lines along both grid directions; a line is broken at points with a non-finite coordinate. The grids are copied when the drawable is created.

Members (6)
  • constructor(xGrid: Float64Array[], yGrid: Float64Array[], zGrid: Float64Array[], options?: Plot3DOptions)
  • readonly kind = "wireframe3d"
  • getDataRange(): DataRange | null
  • drawSVG(ctx: SvgDrawContext): void
  • drawRaster(ctx: RasterDrawContext): void
  • getLegendEntries(): readonly LegendEntry[] | null

Functions

10

kdeplot

function kdeplot(data: Tensor, options?: PlotOptions & { bwMethod?: "scott" | "silverman" | number; bw_method?: "scott" | "silverman" | number; gridSize?: number; fill?: boolean; }): void;

Plot a kernel density estimate of a 1D dataset.

Uses a Gaussian KDE to estimate the probability density function, then plots it as a smooth line (or, with fill: true, as a shaded area under the curve). The grid extends three kernel bandwidths beyond the data on both sides, so the tails of the density are visible.

Non-finite values (NaN, Infinity) are ignored.

Parameters

data
Tensor
1D tensor of observations
optionsoptional
PlotOptions & { bwMethod?: "scott" | "silverman" | number; bw_method?: "scott" | "silverman" | number; gridSize?: number; fill?: boolean; }
Line styling options plus bwMethod ("scott", "silverman" or a positive bandwidth, default "scott"; bw_method is a deprecated spelling, and bwMethod wins when both are given), gridSize (number of evaluation points, an integer >= 2, default 200) and fill (shade the area between the curve and zero)

Throws

  • ShapeErrorIf data is not 1D.
  • InvalidParameterErrorIf there are no finite values or gridSize is invalid.

pairplot

function pairplot(data: Tensor, options?: { featureNames?: readonly string[]; color?: string; size?: number; diagKind?: "hist" | "kde"; bins?: number; }): Figure;

Create a pair plot (matrix of scatter plots) for all variable pairs.

Similar to Seaborn's pairplot. Creates an n x n grid of subplots where the diagonal shows a histogram (or a kernel density estimate) of each variable and the off-diagonal cells show scatter plots. Axis labels are drawn on the left column and bottom row only.

Parameters

data
Tensor
2D tensor [n_samples, n_features]
optionsoptional
{ featureNames?: readonly string[]; color?: string; size?: number; diagKind?: "hist" | "kde"; bins?: number; }
featureNames (one per column), color, scatter size, diagKind ("hist" or "kde", default "hist") and bins (histogram bins on the diagonal, default 20)

Returns

FigureThe figure holding the grid, row-major

Throws

  • ShapeErrorIf data is not 2D.
  • InvalidParameterErrorIf data is empty, featureNames has the wrong length, or diagKind is unknown.

stem

function stem(x: Tensor, y: Tensor, options?: PlotOptions & { baseline?: number; }): void;

Stem plot: vertical lines from baseline to data points with circle markers.

Parameters

x
Tensor
1D tensor of x positions
y
Tensor
1D tensor of values
optionsoptional
PlotOptions & { baseline?: number; }
Styling options plus baseline (default 0)

strip

function strip(groups: readonly Tensor[], options?: { colors?: readonly string[]; labels?: readonly string[]; size?: number; jitter?: number; }): void;

Strip plot: jittered categorical scatter plot (Seaborn-style).

Parameters

groups
readonly Tensor[]
One 1D tensor per category
optionsoptional
{ colors?: readonly string[]; labels?: readonly string[]; size?: number; jitter?: number; }
Colors and labels per group, marker size and jitter width

radar

function radar(series: readonly Tensor[], options?: { colors?: readonly string[]; labels?: readonly string[]; linewidth?: number; }): void;

Radar / spider chart: multi-dimensional data on radial axes.

Parameters

series
readonly Tensor[]
One 1D tensor per series; all series have one value per axis
optionsoptional
{ colors?: readonly string[]; labels?: readonly string[]; linewidth?: number; }
Colors and labels per series, and linewidth

waterfall

function waterfall(categories: readonly string[], values: Tensor, options?: { positiveColor?: string; negativeColor?: string; totalColor?: string; barWidth?: number; }): void;

Waterfall chart: cumulative positive/negative value changes.

Parameters

categories
readonly string[]
One label per step
values
Tensor
1D tensor of step values, same length as categories
optionsoptional
{ positiveColor?: string; negativeColor?: string; totalColor?: string; barWidth?: number; }
Bar colors and barWidth

quiver

function quiver(x: Tensor, y: Tensor, u: Tensor, v: Tensor, options?: PlotOptions & { scale?: number; }): void;

Quiver plot: vector field arrows.

Parameters

x
Tensor
1D tensor of arrow x positions
y
Tensor
1D tensor of arrow y positions
u
Tensor
1D tensor of arrow x components
v
Tensor
1D tensor of arrow y components
optionsoptional
PlotOptions & { scale?: number; }
Styling options plus an arrow length scale

polar

function polar(theta: Tensor, r: Tensor, options?: PlotOptions & { fill?: boolean; }): void;

Polar plot: data in polar coordinates (theta, r).

Parameters

theta
Tensor
1D tensor of angles in radians
r
Tensor
1D tensor of radii
optionsoptional
PlotOptions & { fill?: boolean; }
Styling options plus fill to shade the enclosed area

jointplot

function jointplot(x: Tensor, y: Tensor, options?: { color?: string; bins?: number; xlabel?: string; ylabel?: string; title?: string; }): Figure;

Joint plot: scatter plot with marginal histograms (Seaborn-style jointplot).

Creates a new figure with a central scatter plot, a histogram of x above it and a histogram of y to its right. The marginal axes use the same data range as the scatter axes, so the bins line up with the points. The histogram of y is drawn as horizontal bar outlines.

Parameters

x
Tensor
1D tensor of x values
y
Tensor
1D tensor of y values, same length as x
optionsoptional
{ color?: string; bins?: number; xlabel?: string; ylabel?: string; title?: string; }
color, number of bins (default 20), axis labels and title

Returns

FigureThe new figure (it is not made the current figure)

Throws

  • ShapeErrorIf x and y are not 1D tensors of the same length.
  • InvalidParameterErrorIf bins is not a positive integer.

plotDendrogram

function plotDendrogram(linkage: readonly (readonly [ number, number, number, number ])[], nLeaves: number, options?: PlotOptions): void;

Plot a dendrogram from a linkage matrix.

The linkage matrix has shape (n-1, 4) where each row is [clusterA, clusterB, distance, count].

Parameters

linkage
readonly (readonly [ number, number, number, number ])[]
Linkage matrix rows
nLeaves
number
Number of original observations
optionsoptional
PlotOptions
Styling options
plot-statistical.ts
import {  figure,  jointplot,  kdeplot,  plotDendrogram,  polar,  radar,  saveFig,  scatter3d,} from "deepbox/plot";import { tensor } from "deepbox/ndarray"; kdeplot(tensor([1, 2, 2, 3, 4, 5]), { color: "#0f766e", bwMethod: "silverman" }); // jointplot returns a new figure instead of drawing on the current one.const joint = jointplot(tensor([1, 2, 3, 4]), tensor([2, 2.5, 4, 3]), { title: "Joint plot" });await saveFig("joint.svg", { figure: joint }); figure();plotDendrogram(  [    [0, 1, 0.4, 2],    [2, 3, 0.8, 3],  ],  3); figure();polar(tensor([0, 1, 2]), tensor([1, 2, 1.5]), { fill: true }); figure();radar([tensor([3, 4, 5, 2])], { labels: ["A", "B", "C", "D"] }); figure();scatter3d([1, 2, 3], [4, 5, 6], [7, 8, 9], { color: "#0f766e", elevation: 30 });