matplotlib for Go

github.com/malcolmston/matplotlib

Matplotlib-style plotting and charting in pure Go.

 GitHubports matplotlib/matplotlib

A small, dependency-free plotting library for Go, inspired by Python's matplotlib. It exposes a Figure/Axes drawing model with a pyplot-style convenience API on top, so you can build a chart either object-first (via NewFigure/AddAxes) or with package-level calls (Plot, Bar, Title, Save). Six chart types cover the common cases — line plots, scatter, vertical and horizontal bars, histograms with automatic binning, and pie — each returned as a typed value with fluent per-series styling for color, line width, marker and label. Axes handle the rest automatically: data-range to pixel mapping, axis lines, numeric ticks and labels, an optional legend and grid, and a tab10 color cycle. Every figure renders to two real formats from the standard library alone — PNG (a raster image with a built-in bitmap font) and vector SVG — and identical input always produces identical bytes. No cgo, no third-party modules; the import path and package are both matplotlib.

Install

shell
$ go get github.com/malcolmston/matplotlib

Quick start

main.go
import "github.com/malcolmston/matplotlib"

fig := matplotlib.NewFigure(640, 480)
ax := fig.AddAxes()
ax.Plot([]float64{0, 1, 2, 3}, []float64{0, 1, 4, 9}).
	SetLabel("x^2").SetMarker(matplotlib.MarkerCircle)
ax.Plot([]float64{0, 1, 2, 3}, []float64{0, 2, 4, 6}).SetLabel("2x")
ax.SetTitle("Demo").SetXLabel("x").SetYLabel("y").Legend().Grid(true)
_ = fig.SavePNG("demo.png")
_ = fig.SaveSVG("demo.svg")

Features

  • Figure + Axes model — a Figure owns size, DPI and the color cycle; Figure.AddAxes / Figure.Subplots add coordinate systems
  • Line plots via Axes.Plot returning a LinePlot — overlay multiple series with one call each
  • Scatter, bars and more — Axes.Scatter (ScatterPlot), Axes.Bar / Axes.BarH (BarChart), Axes.Hist (Histogram), Axes.Pie (PieChart)
  • Fluent per-series styling — SetColor, SetLabel, SetLineWidth, SetMarker and SetSize chain off each plot
  • Marker styles — MarkerCircle, MarkerSquare, MarkerTriangle, MarkerCross, MarkerPlus (and MarkerNone)
  • Automatic decorations — SetTitle, SetXLabel/SetYLabel, numeric ticks, Legend and Grid, with SetXLim/SetYLim overrides
  • tab10 color cycle — new series draw successive colors from DefaultColors; build your own with RGB, RGBA or Hex
  • pyplot-style convenience API — package-level FigSize, Plot, Bar, Title, Legend, Grid, Save over an implicit current figure (Gcf/Gca/Clf)
  • PNG output — SavePNG / WritePNG / PNGBytes render a raster *image.RGBA with Bresenham lines, filled shapes and a built-in bitmap font
  • Vector SVG output — SaveSVG / WriteSVG / RenderSVG emit clean <line>/<rect>/<polyline>/<polygon>/<text> markup
  • Format-by-extension Save plus RenderImage for direct pixel access
  • Deterministic — identical input always produces identical bytes
  • Zero dependencies — pure Go standard library (image, image/png, math), no cgo, nothing to audit but the toolchain
matplotlib