package module
Version: v0.0.0-...-9167d8e Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2013 License: BSD-2-Clause Imports: 6 Imported by: 5



Simple-minded functions to work with gnuplot. go-gnuplot runs gnuplot as a subprocess and pushes commands via the STDIN of that subprocess.

See for more informations on the exact semantics of these commands.


The go-gnuplot package is go get installable:

$ go get


package main

import ""
import "fmt"

func main() {
	fname := ""
	persist := false
	debug := true

	p,err := gnuplot.NewPlotter(fname, persist, debug)
	if err != nil {
		err_string := fmt.Sprintf("** err: %v\n", err)
	defer p.Close()

	p.PlotX([]float64{0,1,2,3,4,5,6,7,8,9,10}, "some data")
	p.CheckedCmd("set terminal pdf")
	p.CheckedCmd("set output 'plot002.pdf'")




API documentation can be found here:



gnuplot is a simple minded set of functions to manage a gnuplot subprocess in order to plot data. See the gnuplot documentation page for the exact semantics of the gnuplot commands.



This section is empty.


This section is empty.


This section is empty.


type Func

type Func func(x float64) float64

Func is a 1-d function which can be plotted with gnuplot

type Plotter

type Plotter struct {
	// contains filtered or unexported fields

Plotter is a handle to a gnuplot subprocess, forwarding commands via its stdin

func NewPlotter

func NewPlotter(fname string, persist, debug bool) (*Plotter, error)

NewPlotter creates a new Plotter instance.

- `fname` is the name of the file containing commands (should be empty for now)
- `persist` is a flag to run the gnuplot subprocess with '-persist' so the
  plot window isn't closed after sending a command
- `debug` is a flag to tell go-gnuplot to print out every command sent to
  the gnuplot subprocess.


p, err := gnuplot.NewPlotter("", false, false)
if err != nil { /* handle error */ }
defer p.Close()

func (*Plotter) CheckedCmd

func (self *Plotter) CheckedCmd(format string, a ...interface{})

CheckedCmd is a convenience wrapper around Cmd: it will panic if the error returned by Cmd isn't nil. ex:

fname := "foo.dat"
p.CheckedCmd("plot %s", fname)

func (*Plotter) Close

func (self *Plotter) Close() (err error)

Close makes sure all resources used by the gnuplot subprocess are reclaimed. This method is typically called when the Plotter instance is not needed anymore. That's usually done via a defer statement:

p, err := gnuplot.NewPlotter(...)
if err != nil { /* handle error */ }
defer p.Close()

func (*Plotter) Cmd

func (self *Plotter) Cmd(format string, a ...interface{}) error

Cmd sends a command to the gnuplot subprocess and returns an error if something bad happened in the gnuplot process. ex:

fname := "foo.dat"
err := p.Cmd("plot %s", fname)
if err != nil {

func (*Plotter) PlotFunc

func (self *Plotter) PlotFunc(data []float64, fct Func, title string) error

PlotFunc will create a 2-d plot using `data` as x-coordinates and `fct(x[i])` as the y-coordinates. Example:

fct := funct (x float64) float64 { return math.Exp(float64(x) + 2.) }
err = p.PlotFunc(
         "my title")

func (*Plotter) PlotNd

func (self *Plotter) PlotNd(title string, data ...[]float64) error

PlotNd will create an n-dimensional plot (up to 3) with a title `title` and using the data from the var-arg `data`. example:

err = p.PlotNd(
         "test Nd plot",
         []float64{0,1,2,3}, // x-data
         []float64{0,1,2,3}, // y-data
         []float64{0,1,2,3}) // z-data

func (*Plotter) PlotX

func (self *Plotter) PlotX(data []float64, title string) error

PlotX will create a 2-d plot using `data` as input and `title` as the plot title. The index of the element in the `data` slice will be used as the x-coordinate and its correspinding value as the y-coordinate. Example:

err = p.PlotX([]float64{10, 20, 30}, "my title")

func (*Plotter) PlotXY

func (self *Plotter) PlotXY(x, y []float64, title string) error

PlotXY will create a 2-d plot using `x` and `y` as input and `title` as the plot title. The values of the `x` slice will be used as x-coordinates and the matching values of `y` as y-coordinates (ie: for the same index). If the lengthes of the slices do not match, the range for the data will be the smallest size of the two slices. Example:

err = p.PlotXY(
         []float64{10, 20, 30},
         []float64{11, 22, 33, 44},
         "my title")

func (*Plotter) PlotXYZ

func (self *Plotter) PlotXYZ(x, y, z []float64, title string) error

PlotXYZ will create a 3-d plot using `x`, `y` and `z` as input and `title` as the plot title. The data points to be plotted are the triplets (x[i], y[i], z[i]) where `i` runs from 0 to the smallest length of the 3 slices. Example:

err = p.PlotXYZ(
         []float64{10, 20, 30},
         []float64{11, 22, 33, 44},
         []float64{111, 222, 333, 444, 555},
         "my title")

func (*Plotter) ResetPlot

func (self *Plotter) ResetPlot() (err error)

ResetPlot clears up all plots and sets the Plotter state anew.

func (*Plotter) SetLabels

func (self *Plotter) SetLabels(labels ...string) error

SetLabels changes the labels for the x-,y- and z-axis in one go, depending on the size of the `labels` var-arg. Example:

err = p.SetLabels("x", "y", "z")

func (*Plotter) SetPlotCmd

func (self *Plotter) SetPlotCmd(cmd string) (err error)

SetPlotCmd changes the command used for plotting by the gnuplot subprocess. Only valid plot commands are accepted (plot, splot)

func (*Plotter) SetStyle

func (self *Plotter) SetStyle(style string) (err error)

SetStyle changes the style used by the gnuplot subprocess. Only valid styles are accepted:


func (*Plotter) SetXLabel

func (self *Plotter) SetXLabel(label string) error

SetXLabel changes the label for the x-axis

func (*Plotter) SetYLabel

func (self *Plotter) SetYLabel(label string) error

SetYLabel changes the label for the y-axis

func (*Plotter) SetZLabel

func (self *Plotter) SetZLabel(label string) error

SetZLabel changes the label for the z-axis

Source Files


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL