-
Notifications
You must be signed in to change notification settings - Fork 22
/
example1.go
64 lines (52 loc) · 1.29 KB
/
example1.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
package main
import (
"image/color"
"log"
"os"
"github.com/go-gota/gota/dataframe"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
)
func main() {
// Open the advertising dataset file.
f, err := os.Open("../data/advertising.csv")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Create a dataframe from the CSV file.
// The types of the columns will be inferred.
adDF := dataframe.ReadCSV(f)
// Extract the target column.
yVals := adDF.Col("sales").Float()
// Create a scatter plot for each of the features in the dataset.
for _, colName := range adDF.Names() {
// pts will hold the values for plotting
pts := make(plotter.XYs, adDF.Nrow())
// Fill pts with data.
for i, floatVal := range adDF.Col(colName).Float() {
pts[i].X = floatVal
pts[i].Y = yVals[i]
}
// Create the plot.
p, err := plot.New()
if err != nil {
log.Fatal(err)
}
p.X.Label.Text = colName
p.Y.Label.Text = "y"
p.Add(plotter.NewGrid())
s, err := plotter.NewScatter(pts)
if err != nil {
log.Fatal(err)
}
s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}
s.GlyphStyle.Radius = vg.Points(3)
// Save the plot to a PNG file.
p.Add(s)
if err := p.Save(4*vg.Inch, 4*vg.Inch, colName+"_scatter.png"); err != nil {
log.Fatal(err)
}
}
}