MCPcopy Index your code
hub / github.com/gonum/plot / ExampleScatter

Function ExampleScatter

plotter/scatter_example_test.go:20–81  ·  view source on GitHub ↗

ExampleScatter draws some scatter points, a line, and a line with points.

()

Source from the content-addressed store, hash-verified

18// ExampleScatter draws some scatter points, a line,
19// and a line with points.
20func ExampleScatter() {
21 rnd := rand.New(rand.NewPCG(1, 1))
22
23 // randomPoints returns some random x, y points
24 // with some interesting kind of trend.
25 randomPoints := func(n int) plotter.XYs {
26 pts := make(plotter.XYs, n)
27 for i := range pts {
28 if i == 0 {
29 pts[i].X = rnd.Float64()
30 } else {
31 pts[i].X = pts[i-1].X + rnd.Float64()
32 }
33 pts[i].Y = pts[i].X + 10*rnd.Float64()
34 }
35 return pts
36 }
37
38 n := 15
39 scatterData := randomPoints(n)
40 lineData := randomPoints(n)
41 linePointsData := randomPoints(n)
42
43 p := plot.New()
44 p.Title.Text = "Points Example"
45 p.X.Label.Text = "X"
46 p.Y.Label.Text = "Y"
47 p.Add(plotter.NewGrid())
48
49 s, err := plotter.NewScatter(scatterData)
50 if err != nil {
51 log.Panic(err)
52 }
53 s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}
54 s.GlyphStyle.Radius = vg.Points(3)
55
56 l, err := plotter.NewLine(lineData)
57 if err != nil {
58 log.Panic(err)
59 }
60 l.LineStyle.Width = vg.Points(1)
61 l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)}
62 l.LineStyle.Color = color.RGBA{B: 255, A: 255}
63
64 lpLine, lpPoints, err := plotter.NewLinePoints(linePointsData)
65 if err != nil {
66 log.Panic(err)
67 }
68 lpLine.Color = color.RGBA{G: 255, A: 255}
69 lpPoints.Shape = draw.CircleGlyph{}
70 lpPoints.Color = color.RGBA{R: 255, A: 255}
71
72 p.Add(s, l, lpLine, lpPoints)
73 p.Legend.Add("scatter", s)
74 p.Legend.Add("line", l)
75 p.Legend.Add("line points", lpLine, lpPoints)
76
77 err = p.Save(200, 200, "testdata/scatter.png")

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
NewGridFunction · 0.92
NewScatterFunction · 0.92
PointsFunction · 0.92
NewLineFunction · 0.92
NewLinePointsFunction · 0.92
randomPointsFunction · 0.85
SaveMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected