ExampleScatter_color draws a colored scatter plot. Each point is plotted with a different color depending on external criteria.
()
| 23 | // Each point is plotted with a different color depending on |
| 24 | // external criteria. |
| 25 | func ExampleScatter_color() { |
| 26 | rnd := rand.New(rand.NewPCG(1, 1)) |
| 27 | |
| 28 | // randomTriples returns some random but correlated x, y, z triples |
| 29 | randomTriples := func(n int) plotter.XYZs { |
| 30 | data := make(plotter.XYZs, n) |
| 31 | for i := range data { |
| 32 | if i == 0 { |
| 33 | data[i].X = rnd.Float64() |
| 34 | } else { |
| 35 | data[i].X = data[i-1].X + 2*rnd.Float64() |
| 36 | } |
| 37 | data[i].Y = data[i].X + 10*rnd.Float64() |
| 38 | data[i].Z = data[i].X |
| 39 | } |
| 40 | return data |
| 41 | } |
| 42 | |
| 43 | n := 15 |
| 44 | scatterData := randomTriples(n) |
| 45 | |
| 46 | // Calculate the range of Z values. |
| 47 | minZ, maxZ := math.Inf(1), math.Inf(-1) |
| 48 | for _, xyz := range scatterData { |
| 49 | if xyz.Z > maxZ { |
| 50 | maxZ = xyz.Z |
| 51 | } |
| 52 | if xyz.Z < minZ { |
| 53 | minZ = xyz.Z |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | colors := moreland.Kindlmann() // Initialize a color map. |
| 58 | colors.SetMax(maxZ) |
| 59 | colors.SetMin(minZ) |
| 60 | |
| 61 | p := plot.New() |
| 62 | p.Title.Text = "Colored Points Example" |
| 63 | p.X.Label.Text = "X" |
| 64 | p.Y.Label.Text = "Y" |
| 65 | p.Add(plotter.NewGrid()) |
| 66 | |
| 67 | sc, err := plotter.NewScatter(scatterData) |
| 68 | if err != nil { |
| 69 | log.Panic(err) |
| 70 | } |
| 71 | |
| 72 | // Specify style and color for individual points. |
| 73 | sc.GlyphStyleFunc = func(i int) draw.GlyphStyle { |
| 74 | _, _, z := scatterData.XYZ(i) |
| 75 | d := (z - minZ) / (maxZ - minZ) |
| 76 | rng := maxZ - minZ |
| 77 | k := d*rng + minZ |
| 78 | // Clamp k to avoid potential overflow due to floating point error. |
| 79 | c, err := colors.At(min(k, colors.Max())) |
| 80 | if err != nil { |
| 81 | log.Panic(err) |
| 82 | } |
nothing calls this directly
no test coverage detected