Example 25
beginner
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: line plot, scatter plot, bar chart, histogram, and heatmap.

Deepbox Modules Used

deepbox/ndarraydeepbox/plot

What 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 { mkdirSync, writeFileSync } from "node:fs";2import { cos, linspace, sin, tensor } from "deepbox/ndarray";3import { Figure } from "deepbox/plot";45console.log("=== Data Visualization ===\n");67mkdirSync("docs/examples/25-plotting/output", { recursive: true });89// 1. Line Plot10console.log("1. Creating line plot...");11const x = linspace(0, 2 * Math.PI, 100);12const y1 = sin(x);13const y2 = cos(x);1415const fig1 = new Figure({ width: 640, height: 480 });16const ax1 = fig1.addAxes();17ax1.plot(x, y1, { color: "#1f77b4", linewidth: 2 });18ax1.plot(x, y2, { color: "#ff7f0e", linewidth: 2 });19ax1.setTitle("Sine and Cosine Functions");20ax1.setXLabel("x");21ax1.setYLabel("y");2223const svg1 = fig1.renderSVG();24writeFileSync("docs/examples/25-plotting/output/line-plot.svg", svg1.svg);25console.log("   ✓ Saved: output/line-plot.svg\n");2627// 2. Scatter Plot28console.log("2. Creating scatter plot...");29const x_scatter = tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);30const y_scatter = tensor([2.3, 4.1, 5.9, 7.8, 10.2, 11.8, 14.3, 16.1, 17.9, 20.1]);3132const fig2 = new Figure();33const ax2 = fig2.addAxes();34ax2.scatter(x_scatter, y_scatter, { color: "#2ca02c", size: 8 });35ax2.setTitle("Scatter Plot Example");36ax2.setXLabel("X values");37ax2.setYLabel("Y values");3839const svg2 = fig2.renderSVG();40writeFileSync("docs/examples/25-plotting/output/scatter-plot.svg", svg2.svg);41console.log("   ✓ Saved: output/scatter-plot.svg\n");4243// 3. Bar Chart44console.log("3. Creating bar chart...");45const categories = tensor([0, 1, 2, 3, 4]);46const values = tensor([23, 45, 56, 78, 32]);4748const fig3 = new Figure();49const ax3 = fig3.addAxes();50ax3.bar(categories, values, { color: "#d62728", edgecolor: "#000000" });51ax3.setTitle("Bar Chart Example");52ax3.setXLabel("Categories");53ax3.setYLabel("Values");5455const svg3 = fig3.renderSVG();56writeFileSync("docs/examples/25-plotting/output/bar-chart.svg", svg3.svg);57console.log("   ✓ Saved: output/bar-chart.svg\n");5859// 4. Histogram60console.log("4. Creating histogram...");61const data = tensor([1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9]);6263const fig4 = new Figure();64const ax4 = fig4.addAxes();65ax4.hist(data, 9, { color: "#9467bd" });66ax4.setTitle("Histogram Example");67ax4.setXLabel("Value");68ax4.setYLabel("Frequency");6970const svg4 = fig4.renderSVG();71writeFileSync("docs/examples/25-plotting/output/histogram.svg", svg4.svg);72console.log("   ✓ Saved: output/histogram.svg\n");7374// 5. Heatmap75console.log("5. Creating heatmap...");76const heatmap_data = tensor([77  [1, 2, 3, 4],78  [5, 6, 7, 8],79  [9, 10, 11, 12],80]);8182const fig5 = new Figure();83const ax5 = fig5.addAxes();84ax5.heatmap(heatmap_data, { vmin: 1, vmax: 12 });85ax5.setTitle("Heatmap Example");8687const svg5 = fig5.renderSVG();88writeFileSync("docs/examples/25-plotting/output/heatmap.svg", svg5.svg);89console.log("   ✓ Saved: output/heatmap.svg\n");9091console.log("Visualization Tips:");92console.log("• Use line plots for continuous data");93console.log("• Use scatter plots to show relationships");94console.log("• Use bar charts for categorical comparisons");95console.log("• Use histograms for distributions");96console.log("• Use heatmaps for matrix data");9798console.log("\n✓ Plotting complete!");

Console Output

$ npx tsx 25-plotting/index.ts
=== Data Visualization ===

1. Creating line plot...
   ✓ Saved: output/line-plot.svg

2. Creating scatter plot...
   ✓ Saved: output/scatter-plot.svg

3. Creating bar chart...
   ✓ Saved: output/bar-chart.svg

4. Creating histogram...
   ✓ Saved: output/histogram.svg

5. Creating heatmap...
   ✓ Saved: output/heatmap.svg

Visualization Tips:
• Use line plots for continuous data
• Use scatter plots to show relationships
• Use bar charts for categorical comparisons
• Use histograms for distributions
• Use heatmaps for matrix data

✓ Plotting complete!