matplotlib for Go
Matplotlib-style plotting and charting in pure Go.
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
$ go get github.com/malcolmston/matplotlibQuick start
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
Figureowns size, DPI and the color cycle;Figure.AddAxes/Figure.Subplotsadd coordinate systems - Line plots via
Axes.Plotreturning aLinePlot— 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,SetMarkerandSetSizechain off each plot - Marker styles —
MarkerCircle,MarkerSquare,MarkerTriangle,MarkerCross,MarkerPlus(andMarkerNone) - Automatic decorations —
SetTitle,SetXLabel/SetYLabel, numeric ticks,LegendandGrid, withSetXLim/SetYLimoverrides - tab10 color cycle — new series draw successive colors from
DefaultColors; build your own withRGB,RGBAorHex - pyplot-style convenience API — package-level
FigSize,Plot,Bar,Title,Legend,Grid,Saveover an implicit current figure (Gcf/Gca/Clf) - PNG output —
SavePNG/WritePNG/PNGBytesrender a raster*image.RGBAwith Bresenham lines, filled shapes and a built-in bitmap font - Vector SVG output —
SaveSVG/WriteSVG/RenderSVGemit clean<line>/<rect>/<polyline>/<polygon>/<text>markup - Format-by-extension
SaveplusRenderImagefor 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