ExampleScatter draws some scatter points, a line, and a line with points.
()
| 18 | // ExampleScatter draws some scatter points, a line, |
| 19 | // and a line with points. |
| 20 | func 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") |
nothing calls this directly
no test coverage detected