-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.go
87 lines (72 loc) · 1.51 KB
/
plot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package whale
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
type Plot struct {
base []string
cmds []string
plots []string
data []struct {
x []float32
y []float32
}
}
func NewPlot() *Plot {
return &Plot{
cmds: []string{"set key noautotitle"},
}
}
func (p *Plot) addData(x, y []float32) {
var xs, ys []float32
for i := range x {
xs = append(xs, x[i])
ys = append(ys, y[i])
}
p.data = append(p.data, struct {
x []float32
y []float32
}{
x: xs,
y: ys,
})
}
func (p *Plot) Scatter(x, y []float32, color string) error {
if len(x) != len(y) {
return fmt.Errorf("scatter: x and y must be the same size")
}
p.plots = append(p.plots, fmt.Sprintf("'-' with points linecolor rgb '%s'", color))
p.addData(x, y)
return nil
}
func (p *Plot) Line(x, y []float32, color string) error {
if len(x) != len(y) {
return fmt.Errorf("line: x and y must be the same size")
}
p.plots = append(p.plots, fmt.Sprintf("'-' with lines linecolor rgb '%s'", color))
p.addData(x, y)
return nil
}
func (p *Plot) Exec() error {
cmd := exec.Command("gnuplot", "-persist")
gnuplot := ""
gnuplot += strings.Join(p.cmds, "; ")
gnuplot += "; plot " + strings.Join(p.plots, ", ")
gnuplot += "\n"
for _, d := range p.data {
for i := range d.x {
gnuplot += fmt.Sprintf("%.3f, %.3f\n", d.x[i], d.y[i])
}
gnuplot += "e\n"
}
var in bytes.Buffer
in.WriteString(gnuplot)
cmd.Stdin = &in
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("run gnuplot: %s", string(out))
}
return nil
}