-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox_plots.go
66 lines (53 loc) · 1.44 KB
/
box_plots.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
package main
import (
"github.com/kniren/gota/dataframe"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"log"
"os"
)
func main() {
// Open the CSV file.
irisFile, err := os.Open("iris.csv")
if err != nil {
log.Fatal(err)
}
defer irisFile.Close()
// Create a dataframe from the CSV file.
irisDF := dataframe.ReadCSV(irisFile)
// Create the plot and set its title and axis label.
p, err := plot.New()
if err != nil {
log.Fatal(err)
}
p.Title.Text = "Box plots"
p.Y.Label.Text = "Values"
// Create the box for our data.
w := vg.Points(50)
// Create a box plot for each of the feature columns in the dataset.
for idx, colName := range irisDF.Names() {
// If the column is one of the feature columns, let's create
// a histogram of the values.
if colName != "species" {
// Create a plotter.Values value and fill it with the
// values from the respective column of the dataframe.
v := make(plotter.Values, irisDF.Nrow())
for i, floatVal := range irisDF.Col(colName).Float() {
v[i] = floatVal
}
// Add the data to the plot.
b, err := plotter.NewBoxPlot(w, float64(idx), v)
if err != nil {
log.Fatal(err)
}
p.Add(b)
}
}
// Set the X axis of the plot to nominal with
// the given names for x=0, x=1, etc.
p.NominalX("sepal_length", "sepal_width", "petal_length", "petal_width")
if err := p.Save(6*vg.Inch, 8*vg.Inch, "boxplots.png"); err != nil {
log.Fatal(err)
}
}