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.
jointplotandpairplotare different: they build and return a newFigure, which does not become the current figure. Render it withfig.renderSVG()or save it withsaveFig(path, { figure }). kdeplottakes a 1-D tensor and abwMethod("scott","silverman"or a number).jointplotdraws a scatter plot with a histogram of each variable on the margins.pairplotdraws 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,wireframeandscatter3dtake plain nested arrays (arrays of numbers forscatter3d) and acceptelevationandazimuthoptions.
PlotTheme
interfaceA 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: stringBackground color for figure.
readonly axesFacecolor: stringBackground color for axes.
readonly primaryColor: stringDefault line/bar color.
readonly colorCycle: readonly string[]Color cycle for multiple series.
readonly fontSize: numberDefault font size: the size of axis labels and legends, in pixels.
readonly gridColor: stringGrid color.
readonly gridVisible: booleanWhether grid is visible by default.
readonly textColor: stringColor of the axes frame, ticks, tick labels, titles, axis labels and legend text.
ViolinplotOptions
typeOptions 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
typeRendering options for 3D plot elements.
readonly label?: stringLegend label.
readonly color?: ColorSurface base color, wireframe line color or scatter marker color.
readonly linewidth?: numberWireframe line width in pixels (must be positive).
readonly size?: numberScatter marker radius in pixels (must be positive).
readonly elevation?: numberCamera elevation above the xy plane in degrees (default 30).
readonly azimuth?: numberCamera azimuth in degrees, measured from the +x axis toward +y (default -60).
readonly alpha?: numberOpacity from 0 to 1.
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 | nulldrawSVG(ctx: SvgDrawContext): voiddrawRaster(ctx: RasterDrawContext): voidgetLegendEntries(): 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 | nulldrawSVG(ctx: SvgDrawContext): voiddrawRaster(ctx: RasterDrawContext): voidgetLegendEntries(): 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 | nulldrawSVG(ctx: SvgDrawContext): voiddrawRaster(ctx: RasterDrawContext): voidgetLegendEntries(): readonly LegendEntry[] | null
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
dataTensor- 1D tensor of observations
optionsoptionalPlotOptions & { 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_methodis a deprecated spelling, andbwMethodwins when both are given),gridSize(number of evaluation points, an integer >= 2, default 200) andfill(shade the area between the curve and zero)
Throws
ShapeErrorIfdatais not 1D.InvalidParameterErrorIf there are no finite values orgridSizeis 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
dataTensor- 2D tensor
[n_samples, n_features] optionsoptional{ featureNames?: readonly string[]; color?: string; size?: number; diagKind?: "hist" | "kde"; bins?: number; }featureNames(one per column),color, scattersize,diagKind("hist"or"kde", default"hist") andbins(histogram bins on the diagonal, default 20)
Returns
FigureThe figure holding the grid, row-major
Throws
ShapeErrorIfdatais not 2D.InvalidParameterErrorIfdatais empty,featureNameshas the wrong length, ordiagKindis 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
xTensor- 1D tensor of x positions
yTensor- 1D tensor of values
optionsoptionalPlotOptions & { 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
groupsreadonly Tensor[]- One 1D tensor per category
optionsoptional{ colors?: readonly string[]; labels?: readonly string[]; size?: number; jitter?: number; }- Colors and labels per group, marker
sizeandjitterwidth
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
seriesreadonly 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
categoriesreadonly string[]- One label per step
valuesTensor- 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
xTensor- 1D tensor of arrow x positions
yTensor- 1D tensor of arrow y positions
uTensor- 1D tensor of arrow x components
vTensor- 1D tensor of arrow y components
optionsoptionalPlotOptions & { 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
thetaTensor- 1D tensor of angles in radians
rTensor- 1D tensor of radii
optionsoptionalPlotOptions & { fill?: boolean; }- Styling options plus
fillto 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
xTensor- 1D tensor of x values
yTensor- 1D tensor of y values, same length as
x optionsoptional{ color?: string; bins?: number; xlabel?: string; ylabel?: string; title?: string; }color, number ofbins(default 20), axis labels and title
Returns
FigureThe new figure (it is not made the current figure)
Throws
ShapeErrorIfxandyare not 1D tensors of the same length.InvalidParameterErrorIfbinsis 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
linkagereadonly (readonly [ number, number, number, number ])[]- Linkage matrix rows
nLeavesnumber- Number of original observations
optionsoptionalPlotOptions- Styling options
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 });Found a mistake? Open an issue.