-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateErrorPlot.go
59 lines (46 loc) · 1.16 KB
/
generateErrorPlot.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
package main
import (
"fmt"
"image/color"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
)
func generateErrorPlot(arr1 []float64, legend1, xlabel, ylabel string) (*plot.Plot, error) {
length := len(arr1)
samplesArray := make([]float64, length)
for i := 0; i < length; i++ {
samplesArray[i] = float64(i + 1)
}
var resultArray1 []struct {
X, Y float64
}
for i := 0; i < len(samplesArray); i++ {
resultArray1 = append(resultArray1, struct{ X, Y float64 }{samplesArray[i], arr1[i]})
}
// Creating new plot
p := plot.New()
// Adding data to plot
points := make(plotter.XYs, len(resultArray1))
for i, d := range resultArray1 {
points[i].X = d.X
points[i].Y = d.Y
}
line1, err := plotter.NewLine(points)
if err != nil {
return nil, fmt.Errorf("error occured while creating plot: %v", err)
}
line1.LineStyle.Color = color.RGBA{R: 255, G: 0, B: 0, A: 255}
p.Add(line1)
// Adding legend (position and value)
p.Legend.Top = true
p.Legend.Left = false
p.Legend.XOffs = 0
p.Legend.YOffs = -0.1
p.Legend.Add(legend1, line1)
// Adding labels
p.X.Label.Text = xlabel
p.Y.Label.Text = ylabel
// Adding grid
p.Add(plotter.NewGrid())
return p, nil
}