/
CSVplot.go
65 lines (56 loc) · 1.13 KB
/
CSVplot.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
package main
import (
"encoding/csv"
"fmt"
"github.com/Arafatk/glot"
"os"
"strconv"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Need a data file!")
return
}
file := os.Args[1]
_, err := os.Stat(file)
if err != nil {
fmt.Println("Cannot stat", file)
return
}
f, err := os.Open(file)
if err != nil {
fmt.Println("Cannot open", file)
fmt.Println(err)
return
}
defer f.Close()
reader := csv.NewReader(f)
reader.FieldsPerRecord = -1
allRecords, err := reader.ReadAll()
if err != nil {
fmt.Println(err)
return
}
xP := []float64{}
yP := []float64{}
for _, rec := range allRecords {
x, _ := strconv.ParseFloat(rec[0], 64)
y, _ := strconv.ParseFloat(rec[1], 64)
xP = append(xP, x)
yP = append(yP, y)
}
points := [][]float64{}
points = append(points, xP)
points = append(points, yP)
fmt.Println(points)
dimensions := 2
persist := true
debug := false
plot, _ := glot.NewPlot(dimensions, persist, debug)
plot.SetTitle("Using Glot with CSV data")
plot.SetXLabel("X-Axis")
plot.SetYLabel("Y-Axis")
style := "circle"
plot.AddPointGroup("Circle:", style, points)
plot.SavePlot("output.png")
}