ExampleScatter_bubbles draws some scatter points. Each point is plotted with a different radius size depending on external criteria.
()
| 20 | // Each point is plotted with a different radius size depending on |
| 21 | // external criteria. |
| 22 | func ExampleScatter_bubbles() { |
| 23 | rnd := rand.New(rand.NewPCG(1, 1)) |
| 24 | |
| 25 | // randomTriples returns some random but correlated x, y, z triples |
| 26 | randomTriples := func(n int) plotter.XYZs { |
| 27 | data := make(plotter.XYZs, n) |
| 28 | for i := range data { |
| 29 | if i == 0 { |
| 30 | data[i].X = rnd.Float64() |
| 31 | } else { |
| 32 | data[i].X = data[i-1].X + 2*rnd.Float64() |
| 33 | } |
| 34 | data[i].Y = data[i].X + 10*rnd.Float64() |
| 35 | data[i].Z = data[i].X |
| 36 | } |
| 37 | return data |
| 38 | } |
| 39 | |
| 40 | n := 10 |
| 41 | scatterData := randomTriples(n) |
| 42 | |
| 43 | // Calculate the range of Z values. |
| 44 | minZ, maxZ := math.Inf(1), math.Inf(-1) |
| 45 | for _, xyz := range scatterData { |
| 46 | if xyz.Z > maxZ { |
| 47 | maxZ = xyz.Z |
| 48 | } |
| 49 | if xyz.Z < minZ { |
| 50 | minZ = xyz.Z |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | p := plot.New() |
| 55 | p.Title.Text = "Bubbles" |
| 56 | p.X.Label.Text = "X" |
| 57 | p.Y.Label.Text = "Y" |
| 58 | |
| 59 | sc, err := plotter.NewScatter(scatterData) |
| 60 | if err != nil { |
| 61 | log.Panic(err) |
| 62 | } |
| 63 | |
| 64 | // Specify style for individual points. |
| 65 | sc.GlyphStyleFunc = func(i int) draw.GlyphStyle { |
| 66 | c := color.RGBA{R: 196, B: 128, A: 255} |
| 67 | var minRadius, maxRadius = vg.Points(1), vg.Points(20) |
| 68 | rng := maxRadius - minRadius |
| 69 | _, _, z := scatterData.XYZ(i) |
| 70 | d := (z - minZ) / (maxZ - minZ) |
| 71 | r := vg.Length(d)*rng + minRadius |
| 72 | return draw.GlyphStyle{Color: c, Radius: r, Shape: draw.CircleGlyph{}} |
| 73 | } |
| 74 | p.Add(sc) |
| 75 | |
| 76 | err = p.Save(200, 200, "testdata/bubbles.png") |
| 77 | if err != nil { |
| 78 | log.Panic(err) |
| 79 | } |