-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplt.go
53 lines (45 loc) · 890 Bytes
/
plt.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
package go_ehlers_indicators
import (
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
type (
Values struct {
Xs []float64
Ys []float64
}
)
func (v *Values) Len() int {
return len(v.Xs)
}
func (v *Values) XY(index int) (float64, float64) {
return v.Xs[index], v.Ys[index]
}
// Plt will create a line plot with given values and filename
func Plt(vals []float64, filename string) error {
xs := make([]float64, len(vals))
ys := make([]float64, len(vals))
for i := 0; i < len(vals); i++ {
xs[i] = float64(i)
ys[i] = vals[i]
}
values := &Values{
Xs: xs,
Ys: ys,
}
p, err := plot.New()
if err != nil {
return err
}
p.Title.Text = filename
line, err := plotter.NewLine(values)
if err != nil {
return err
}
p.Add(line)
if err := p.Save(297*vg.Millimeter, 210*vg.Millimeter, filename); err != nil {
return err
}
return nil
}