-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathscatterColor_example_test.go
More file actions
128 lines (112 loc) · 2.88 KB
/
scatterColor_example_test.go
File metadata and controls
128 lines (112 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package plotter_test
import (
"fmt"
"log"
"math"
"math/rand/v2"
"os"
"gonum.org/v1/plot"
"gonum.org/v1/plot/palette/moreland"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgimg"
)
// ExampleScatter_color draws a colored scatter plot.
// Each point is plotted with a different color depending on
// external criteria.
func ExampleScatter_color() {
rnd := rand.New(rand.NewPCG(1, 1))
// randomTriples returns some random but correlated x, y, z triples
randomTriples := func(n int) plotter.XYZs {
data := make(plotter.XYZs, n)
for i := range data {
if i == 0 {
data[i].X = rnd.Float64()
} else {
data[i].X = data[i-1].X + 2*rnd.Float64()
}
data[i].Y = data[i].X + 10*rnd.Float64()
data[i].Z = data[i].X
}
return data
}
n := 15
scatterData := randomTriples(n)
// Calculate the range of Z values.
minZ, maxZ := math.Inf(1), math.Inf(-1)
for _, xyz := range scatterData {
if xyz.Z > maxZ {
maxZ = xyz.Z
}
if xyz.Z < minZ {
minZ = xyz.Z
}
}
colors := moreland.Kindlmann() // Initialize a color map.
colors.SetMax(maxZ)
colors.SetMin(minZ)
p := plot.New()
p.Title.Text = "Colored Points Example"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
p.Add(plotter.NewGrid())
sc, err := plotter.NewScatter(scatterData)
if err != nil {
log.Panic(err)
}
// Specify style and color for individual points.
sc.GlyphStyleFunc = func(i int) draw.GlyphStyle {
_, _, z := scatterData.XYZ(i)
d := (z - minZ) / (maxZ - minZ)
rng := maxZ - minZ
k := d*rng + minZ
// Clamp k to avoid potential overflow due to floating point error.
c, err := colors.At(min(k, colors.Max()))
if err != nil {
log.Panic(err)
}
return draw.GlyphStyle{Color: c, Radius: vg.Points(3), Shape: draw.CircleGlyph{}}
}
p.Add(sc)
//Create a legend
thumbs := plotter.PaletteThumbnailers(colors.Palette(n))
for i := len(thumbs) - 1; i >= 0; i-- {
t := thumbs[i]
if i != 0 && i != len(thumbs)-1 {
p.Legend.Add("", t)
continue
}
var val int
switch i {
case 0:
val = int(minZ)
case len(thumbs) - 1:
val = int(maxZ)
}
p.Legend.Add(fmt.Sprintf("%d", val), t)
}
// This is the width of the legend, experimentally determined.
const legendWidth = vg.Centimeter
// Slide the legend over so it doesn't overlap the ScatterPlot.
p.Legend.XOffs = legendWidth
img := vgimg.New(300, 230)
dc := draw.New(img)
dc = draw.Crop(dc, 0, -legendWidth, 0, 0) // Make space for the legend.
p.Draw(dc)
w, err := os.Create("testdata/scatterColor.png")
if err != nil {
log.Panic(err)
}
defer w.Close()
png := vgimg.PngCanvas{Canvas: img}
if _, err = png.WriteTo(w); err != nil {
log.Panic(err)
}
if err = w.Close(); err != nil {
log.Panic(err)
}
}