A V wrapper for gnuplot: plot functions, data series, and 3D surfaces through a persistent gnuplot process driven over its stdin pipe.
- V 0.4+
- gnuplot on
PATH(or setVPLOT_GNUPLOTto the binary)
v install --git https://github.com/<you>/vplotor clone and use as a local module / symlink into ~/.vmodules.
module main
import vplot
fn main() {
mut p := vplot.new() or { panic(err) } // headless: no GUI window
defer { p.close() }
p.set_title('Demo') or { panic(err) }
p.set_grid(true) or { panic(err) }
// plot functions
p.plot_fns(['sin(x)', 'cos(x)'], samples: 500) or { panic(err) }
// or plot data series
x := [0.0, 1, 2, 3, 4]
y := [0.0, 1, 4, 9, 16]
p.plot_xy(x, y, label: 'x^2', style: .lines_points, color: '#d62728') or { panic(err) }
// export (terminal inferred from extension)
p.save('plot.png', 900, 600) or { panic(err) } // also: .svg .pdf .eps .txt
}new() is headless by default (it selects gnuplot's no-op unknown
terminal), so nothing ever pops up and save() still works — ideal for
scripts, CI, and SSH sessions. Pass vplot.new(interactive: true) if you
want live GUI plot windows, or call p.set_terminal('qt') at any time.
See examples/ for multi-series data plots (data.v) and 3D surfaces
(surface.v). Run one with:
v -path '@vlib|.' run examples/basic/basic.v| Function | Purpose |
|---|---|
new() ! |
spawn persistent gnuplot process (headless; new(interactive: true) for GUI windows) |
cmd(string) ! |
send any raw gnuplot command |
plot_fn(expr, cfg) / plot_fns(exprs, cfg) |
function plots |
plot_xy(x, y, cfg) / plot_series([]Series) |
data plots |
plot3_fn(expr, label) |
3D surface |
save(path, w, h) |
export via replot (.png/.svg/.pdf/.eps/.txt) |
set_title/xlabel/ylabel/xrange/yrange/grid/log_y |
styling |
set_terminal(term) |
switch gnuplot terminal at runtime |
close() |
quit gnuplot, clean up temp files |
Set p.debug = true to echo every gnuplot command to stderr.
v test .Tests skip automatically when gnuplot is not installed.