...
1
2
3
4
5
6
7 package plotter_test
8
9 import (
10 "image/color"
11
12 "gonum.org/v1/gonum/mat"
13 "gonum.org/v1/plot"
14 "gonum.org/v1/plot/palette"
15 "gonum.org/v1/plot/plotter"
16 "gonum.org/v1/plot/vg"
17 "gonum.org/v1/plot/vg/draw"
18 )
19
20 type deciGrid struct{ mat.Matrix }
21
22 func (g deciGrid) Dims() (c, r int) { r, c = g.Matrix.Dims(); return c, r }
23 func (g deciGrid) Z(c, r int) float64 { return g.Matrix.At(r, c) }
24 func (g deciGrid) X(c int) float64 {
25 _, n := g.Matrix.Dims()
26 if c < 0 || c >= n {
27 panic("index out of range")
28 }
29 return 10 * float64(c)
30 }
31 func (g deciGrid) Y(r int) float64 {
32 m, _ := g.Matrix.Dims()
33 if r < 0 || r >= m {
34 panic("index out of range")
35 }
36 return 10 * float64(r)
37 }
38
39 func Example_volcano() {
40 var levels []float64
41 for l := 100.5; l < mat.Max(volcano.Matrix.(*mat.Dense)); l += 5 {
42 levels = append(levels, l)
43 }
44 c := plotter.NewContour(volcano, levels, palette.Rainbow(len(levels), (palette.Yellow+palette.Red)/2, palette.Blue, 1, 1, 1))
45 quarterStyle := draw.LineStyle{
46 Color: color.Black,
47 Width: vg.Points(0.5),
48 Dashes: []vg.Length{0.2, 0.4},
49 }
50 halfStyle := draw.LineStyle{
51 Color: color.Black,
52 Width: vg.Points(0.5),
53 Dashes: []vg.Length{5, 2, 1, 2},
54 }
55 c.LineStyles = append(c.LineStyles, quarterStyle, halfStyle, quarterStyle)
56
57 h := plotter.NewHeatMap(volcano, palette.Heat(len(levels)*2, 1))
58
59 p := plot.New()
60 p.Title.Text = "Maunga Whau Volcano"
61
62 p.Add(h)
63 p.Add(c)
64
65 p.X.Padding = 0
66 p.Y.Padding = 0
67 _, p.X.Max, _, p.Y.Max = h.DataRange()
68
69 if err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/volcano.png"); err != nil {
70 panic(err)
71 }
72 }
73
View as plain text