25
Visualization
SVG
Plotting
Data Visualization
Deepbox's plot module provides a stateful API for creating publication-quality figures server-side, with no browser or DOM required. This example creates five types of visualizations: (1) a line plot of sine and cosine functions demonstrating multi-series plotting; (2) a scatter plot showing point data with custom colors and sizes; (3) a vertical bar chart comparing categorical values; (4) a histogram showing the frequency distribution of data; and (5) a heatmap visualizing a matrix with color intensity. Each plot uses the Figure/Axes pattern — create a Figure, add Axes, call plotting methods (plot, scatter, bar, hist, heatmap), set titles and labels, then render to SVG or save to file with saveFig().
Deepbox Modules Used
deepbox/ndarraydeepbox/plotWhat You Will Learn
- Create figures with new Figure() and add axes with fig.addAxes()
- Line plots: ax.plot(x, y) — for continuous functions and time series
- Scatter plots: ax.scatter(x, y) — for showing relationships between variables
- Bar charts: ax.bar(x, height) — for categorical comparisons
- Histograms: ax.hist(data, bins) — for showing distributions
- Heatmaps: ax.heatmap(matrix) — for correlation matrices and 2D data
- Render with fig.renderSVG() or save with saveFig('file.svg')
Source Code
25-plotting/index.ts
1import { cos, linspace, sin, tensor } from "deepbox/ndarray";2import { Figure } from "deepbox/plot";34console.log("=== Data Visualization ===\n");56// 1. Line Plot7const x = linspace(0, 2 * Math.PI, 100);8const fig1 = new Figure({ width: 640, height: 480 });9const ax1 = fig1.addAxes();10ax1.plot(x, sin(x), { color: "#1f77b4", linewidth: 2 });11ax1.plot(x, cos(x), { color: "#ff7f0e", linewidth: 2 });12ax1.setTitle("Sine and Cosine Functions");13ax1.setXLabel("x");14ax1.setYLabel("y");15const svg1 = fig1.renderSVG();16console.log("✓ Line plot:", svg1.svg.length, "bytes SVG");1718// 2. Scatter Plot19const fig2 = new Figure();20const ax2 = fig2.addAxes();21ax2.scatter(22 tensor([1,2,3,4,5,6,7,8,9,10]),23 tensor([2.3,4.1,5.9,7.8,10.2,11.8,14.3,16.1,17.9,20.1]),24 { color: "#2ca02c", size: 8 }25);26ax2.setTitle("Scatter Plot");27console.log("✓ Scatter plot generated");2829// 3. Bar Chart30const fig3 = new Figure();31const ax3 = fig3.addAxes();32ax3.bar(tensor([0,1,2,3,4]), tensor([23,45,56,78,32]), { color: "#d62728" });33ax3.setTitle("Bar Chart");34console.log("✓ Bar chart generated");3536// 4. Histogram37const fig4 = new Figure();38const ax4 = fig4.addAxes();39ax4.hist(tensor([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,7,7,8,9]), 9);40ax4.setTitle("Histogram");41console.log("✓ Histogram generated");4243// 5. Heatmap44const fig5 = new Figure();45const ax5 = fig5.addAxes();46ax5.heatmap(tensor([[1,2,3,4],[5,6,7,8],[9,10,11,12]]));47ax5.setTitle("Heatmap");48console.log("✓ Heatmap generated");Console Output
$ npx tsx 25-plotting/index.ts
=== Data Visualization ===
✓ Line plot: 42871 bytes SVG
✓ Scatter plot generated
✓ Bar chart generated
✓ Histogram generated
✓ Heatmap generated