...
1
2
3
4
5 package plotter_test
6
7 import (
8 "image/color"
9 "log"
10 "math"
11
12 "golang.org/x/exp/rand"
13
14 "gonum.org/v1/plot"
15 "gonum.org/v1/plot/plotter"
16 "gonum.org/v1/plot/vg"
17 "gonum.org/v1/plot/vg/draw"
18 )
19
20
21
22
23 func ExampleScatter_bubbles() {
24 rnd := rand.New(rand.NewSource(1))
25
26
27 randomTriples := func(n int) plotter.XYZs {
28 data := make(plotter.XYZs, n)
29 for i := range data {
30 if i == 0 {
31 data[i].X = rnd.Float64()
32 } else {
33 data[i].X = data[i-1].X + 2*rnd.Float64()
34 }
35 data[i].Y = data[i].X + 10*rnd.Float64()
36 data[i].Z = data[i].X
37 }
38 return data
39 }
40
41 n := 10
42 scatterData := randomTriples(n)
43
44
45 minZ, maxZ := math.Inf(1), math.Inf(-1)
46 for _, xyz := range scatterData {
47 if xyz.Z > maxZ {
48 maxZ = xyz.Z
49 }
50 if xyz.Z < minZ {
51 minZ = xyz.Z
52 }
53 }
54
55 p := plot.New()
56 p.Title.Text = "Bubbles"
57 p.X.Label.Text = "X"
58 p.Y.Label.Text = "Y"
59
60 sc, err := plotter.NewScatter(scatterData)
61 if err != nil {
62 log.Panic(err)
63 }
64
65
66 sc.GlyphStyleFunc = func(i int) draw.GlyphStyle {
67 c := color.RGBA{R: 196, B: 128, A: 255}
68 var minRadius, maxRadius = vg.Points(1), vg.Points(20)
69 rng := maxRadius - minRadius
70 _, _, z := scatterData.XYZ(i)
71 d := (z - minZ) / (maxZ - minZ)
72 r := vg.Length(d)*rng + minRadius
73 return draw.GlyphStyle{Color: c, Radius: r, Shape: draw.CircleGlyph{}}
74 }
75 p.Add(sc)
76
77 err = p.Save(200, 200, "testdata/bubbles.png")
78 if err != nil {
79 log.Panic(err)
80 }
81 }
82
View as plain text