Commits

Anonymous committed ba2c342

add support for splot and an example (t003_splot.go)

  • Participants
  • Parent commits 2330892
  • Tags release

Comments (0)

Files changed (2)

examples/t003_splot.go

+package main
+
+import "fmt"
+import "bitbucket.org/binet/go-gnuplot/pkg/gnuplot"
+
+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)
+		panic(err_string)
+	}
+	defer p.Close()
+
+	p.SetPlotCmd("splot")
+	p.SetStyle("pm3d")
+
+	p.PlotXY([]float64{0,1,2,3,4,5,6,7,8,9,10}, 
+		     []float64{0,1,2,3,4,5,6,7,8,9,10}, "some data - x/y")
+	p.SetXLabel("my x data")
+	p.SetYLabel("my y data")
+	p.CheckedCmd("set terminal pdf")
+	p.CheckedCmd("set output 'plot003.pdf'")
+	p.CheckedCmd("replot")
+
+
+	p.CheckedCmd("q")
+	return
+}
+
+/* EOF */

pkg/gnuplot/gnuplot.go

 var g_gnuplot_cmd string
 var g_gnuplot_prefix string = "go-gnuplot-"
 
-func min(a,b int) int {
-	if a<b {
+func min(a, b int) int {
+	if a < b {
 		return a
 	}
 	return b
 type gnuplot_error struct {
 	err string
 }
+
 func (e *gnuplot_error) String() string {
 	return e.err
 }
+
 type plotter_process struct {
-	handle  *exec.Cmd
-	stdin   io.WriteCloser
+	handle *exec.Cmd
+	stdin  io.WriteCloser
 }
 
 func new_plotter_proc(persist bool) (*plotter_process, os.Error) {
 type tmpfiles_db map[string]*os.File
 
 type Plotter struct {
-	proc *plotter_process
-	debug bool
-	plotcmd string
-	nplots int // number of currently active plots
-	style  string // current plotting style
+	proc     *plotter_process
+	debug    bool
+	plotcmd  string
+	nplots   int    // number of currently active plots
+	style    string // current plotting style
 	tmpfiles tmpfiles_db
 }
 
 func (self *Plotter) Cmd(format string, a ...interface{}) os.Error {
 	cmd := fmt.Sprintf(format, a...) + "\n"
-	n,err := io.WriteString(self.proc.stdin, cmd)
-	
+	n, err := io.WriteString(self.proc.stdin, cmd)
+
 	if self.debug {
 		//buf := new(bytes.Buffer)
 		//io.Copy(buf, self.proc.handle.Stdout)
 	ndims := len(data)
 
 	switch ndims {
-	case 1: return self.PlotX(data[0], title)
-	case 2: return self.PlotXY(data[0], data[1], title)
-	case 3: return self.PlotXYZ(data[0], data[1], data[2], title)
+	case 1:
+		return self.PlotX(data[0], title)
+	case 2:
+		return self.PlotXY(data[0], data[1], title)
+	case 3:
+		return self.PlotXYZ(data[0], data[1], data[2], title)
 	}
 
 	return &gnuplot_error{fmt.Sprintf("invalid number of dims '%v'", ndims)}
 	}
 	fname := f.Name()
 	self.tmpfiles[fname] = f
-	for _,d := range data {
+	for _, d := range data {
 		f.WriteString(fmt.Sprintf("%v\n", d))
 	}
 	f.Close()
-	cmd := "plot"
+	cmd := self.plotcmd
 	if self.nplots > 0 {
 		cmd = "replot"
 	}
-	
+
 	var line string
 	if title == "" {
 		line = fmt.Sprintf("%s \"%s\" with %s", cmd, fname, self.style)
 	return self.Cmd(line)
 }
 
-func (self *Plotter) PlotXY(x,y []float64, title string) os.Error {
+func (self *Plotter) PlotXY(x, y []float64, title string) os.Error {
 	npoints := min(len(x), len(y))
 
 	f, err := ioutil.TempFile(os.TempDir(), g_gnuplot_prefix)
 	fname := f.Name()
 	self.tmpfiles[fname] = f
 
-	for i:=0; i < npoints; i++ {
+	for i := 0; i < npoints; i++ {
 		f.WriteString(fmt.Sprintf("%v %v\n", x[i], y[i]))
 	}
 
 	f.Close()
-	cmd := "plot"
+	cmd := self.plotcmd
 	if self.nplots > 0 {
 		cmd = "replot"
 	}
-	
+
 	var line string
 	if title == "" {
 		line = fmt.Sprintf("%s \"%s\" with %s", cmd, fname, self.style)
 	return self.Cmd(line)
 }
 
-func (self *Plotter) PlotXYZ(x,y,z []float64, title string) os.Error {
+func (self *Plotter) PlotXYZ(x, y, z []float64, title string) os.Error {
 	npoints := min(len(x), len(y))
 	npoints = min(npoints, len(z))
 	f, err := ioutil.TempFile(os.TempDir(), g_gnuplot_prefix)
 	fname := f.Name()
 	self.tmpfiles[fname] = f
 
-	for i:=0; i < npoints; i++ {
+	for i := 0; i < npoints; i++ {
 		f.WriteString(fmt.Sprintf("%v %v %v\n", x[i], y[i], z[i]))
 	}
 
 	f.Close()
-	cmd := "splot"
+	cmd := "splot" // Force 3D plot
 	if self.nplots > 0 {
 		cmd = "replot"
 	}
-	
+
 	var line string
 	if title == "" {
 		line = fmt.Sprintf("%s \"%s\" with %s", cmd, fname, self.style)
 type Func func(x float64) float64
 
 func (self *Plotter) PlotFunc(data []float64, fct Func, title string) os.Error {
-	
+
 	f, err := ioutil.TempFile(os.TempDir(), g_gnuplot_prefix)
 	if err != nil {
 		return err
 	fname := f.Name()
 	self.tmpfiles[fname] = f
 
-	for _,x := range data {
+	for _, x := range data {
 		f.WriteString(fmt.Sprintf("%v %v\n", x, fct(x)))
 	}
 
 	f.Close()
-	cmd := "plot"
+	cmd := self.plotcmd
 	if self.nplots > 0 {
 		cmd = "replot"
 	}
-	
+
 	var line string
 	if title == "" {
 		line = fmt.Sprintf("%s \"%s\" with %s", cmd, fname, self.style)
 	return self.Cmd(line)
 }
 
+func (self *Plotter) SetPlotCmd(cmd string) (err os.Error) {
+	switch cmd {
+	case "plot", "splot":
+		self.plotcmd = cmd
+	default:
+		err = os.NewError("invalid plot cmd [" + cmd + "]")
+	}
+	return err
+}
+
 func (self *Plotter) SetStyle(style string) (err os.Error) {
 	allowed := []string{
 		"lines", "points", "linepoints",
 		"steps",
 		"errorbars",
 		"boxes",
-		"boxerrorbars"}
+		"boxerrorbars",
+		"pm3d"}
 
-	for _,s := range allowed {
+	for _, s := range allowed {
 		if s == style {
 			self.style = style
 			err = nil
 	}
 	var err os.Error = nil
 
-	for i,label := range labels {
+	for i, label := range labels {
 		switch i {
-		case 0: 
+		case 0:
 			ierr := self.SetXLabel(label)
 			if ierr != nil {
 				err = ierr
 }
 
 func NewPlotter(fname string, persist, debug bool) (*Plotter, os.Error) {
-	p := &Plotter{proc: nil, debug: debug, plotcmd: "plot", 
-	nplots:0, style:"points"}
+	p := &Plotter{proc: nil, debug: debug, plotcmd: "plot",
+		nplots: 0, style: "points"}
 	p.tmpfiles = make(tmpfiles_db)
 
 	if fname != "" {