-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathscatter_example_test.go
More file actions
81 lines (69 loc) · 1.86 KB
/
scatter_example_test.go
File metadata and controls
81 lines (69 loc) · 1.86 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plotter_test
import (
"image/color"
"log"
"math/rand/v2"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
// ExampleScatter draws some scatter points, a line,
// and a line with points.
func ExampleScatter() {
rnd := rand.New(rand.NewPCG(1, 1))
// randomPoints returns some random x, y points
// with some interesting kind of trend.
randomPoints := func(n int) plotter.XYs {
pts := make(plotter.XYs, n)
for i := range pts {
if i == 0 {
pts[i].X = rnd.Float64()
} else {
pts[i].X = pts[i-1].X + rnd.Float64()
}
pts[i].Y = pts[i].X + 10*rnd.Float64()
}
return pts
}
n := 15
scatterData := randomPoints(n)
lineData := randomPoints(n)
linePointsData := randomPoints(n)
p := plot.New()
p.Title.Text = "Points Example"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
p.Add(plotter.NewGrid())
s, err := plotter.NewScatter(scatterData)
if err != nil {
log.Panic(err)
}
s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}
s.GlyphStyle.Radius = vg.Points(3)
l, err := plotter.NewLine(lineData)
if err != nil {
log.Panic(err)
}
l.LineStyle.Width = vg.Points(1)
l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)}
l.LineStyle.Color = color.RGBA{B: 255, A: 255}
lpLine, lpPoints, err := plotter.NewLinePoints(linePointsData)
if err != nil {
log.Panic(err)
}
lpLine.Color = color.RGBA{G: 255, A: 255}
lpPoints.Shape = draw.CircleGlyph{}
lpPoints.Color = color.RGBA{R: 255, A: 255}
p.Add(s, l, lpLine, lpPoints)
p.Legend.Add("scatter", s)
p.Legend.Add("line", l)
p.Legend.Add("line points", lpLine, lpPoints)
err = p.Save(200, 200, "testdata/scatter.png")
if err != nil {
log.Panic(err)
}
}