forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example3.go
66 lines (56 loc) · 1.46 KB
/
example3.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go build
// ./example3
// Sample program to generate a box plot of example distributions.
package main
import (
"log"
"math/rand"
"github.com/gonum/plot"
"github.com/gonum/plot/plotter"
"github.com/gonum/plot/vg"
)
func main() {
// Get some random data from three different distributions
// to display in our plot.
rand.Seed(int64(0))
n := 100
uniform := make(plotter.Values, n)
normal := make(plotter.Values, n)
expon := make(plotter.Values, n)
for i := 0; i < n; i++ {
uniform[i] = rand.Float64()
normal[i] = rand.NormFloat64()
expon[i] = rand.ExpFloat64()
}
// Create the plot and set its title and axis label.
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = "Box plots"
p.Y.Label.Text = "Values"
// Make boxes for our data and add them to the plot.
w := vg.Points(50)
b0, err := plotter.NewBoxPlot(w, 0, uniform)
if err != nil {
log.Fatal(err)
}
b1, err := plotter.NewBoxPlot(w, 1, normal)
if err != nil {
log.Fatal(err)
}
b2, err := plotter.NewBoxPlot(w, 2, expon)
if err != nil {
log.Fatal(err)
}
p.Add(b0, b1, b2)
// Set the X axis of the plot to nominal with
// the given names for x=0, x=1 and x=2.
p.NominalX("Uniform\nDistribution", "Normal\nDistribution",
"Exponential\nDistribution")
if err := p.Save(6*vg.Inch, 8*vg.Inch, "boxplot.png"); err != nil {
log.Fatal(err)
}
}