1
2
3
4
5
6
7
8 package main
9
10 import (
11 "golang.org/x/exp/rand"
12
13 "gonum.org/v1/plot"
14 "gonum.org/v1/plot/plotter"
15 "gonum.org/v1/plot/plotutil"
16 "gonum.org/v1/plot/vg"
17 )
18
19 var examples = []struct {
20 name string
21 mkplot func() *plot.Plot
22 }{
23 {"example_errpoints", Example_errpoints},
24 {"example_stackedAreaChart", Example_stackedAreaChart},
25 }
26
27 func main() {
28 for _, ex := range examples {
29 drawEps(ex.name, ex.mkplot)
30 drawSvg(ex.name, ex.mkplot)
31 drawPng(ex.name, ex.mkplot)
32 drawTiff(ex.name, ex.mkplot)
33 drawJpg(ex.name, ex.mkplot)
34 drawPdf(ex.name, ex.mkplot)
35 }
36 }
37
38 func drawEps(name string, mkplot func() *plot.Plot) {
39 if err := mkplot().Save(10*vg.Centimeter, 10*vg.Centimeter, name+".eps"); err != nil {
40 panic(err)
41 }
42 }
43
44 func drawPdf(name string, mkplot func() *plot.Plot) {
45 if err := mkplot().Save(10*vg.Centimeter, 10*vg.Centimeter, name+".pdf"); err != nil {
46 panic(err)
47 }
48 }
49
50 func drawSvg(name string, mkplot func() *plot.Plot) {
51 if err := mkplot().Save(10*vg.Centimeter, 10*vg.Centimeter, name+".svg"); err != nil {
52 panic(err)
53 }
54 }
55
56 func drawPng(name string, mkplot func() *plot.Plot) {
57 if err := mkplot().Save(10*vg.Centimeter, 10*vg.Centimeter, name+".png"); err != nil {
58 panic(err)
59 }
60 }
61
62 func drawTiff(name string, mkplot func() *plot.Plot) {
63 if err := mkplot().Save(10*vg.Centimeter, 10*vg.Centimeter, name+".tiff"); err != nil {
64 panic(err)
65 }
66 }
67
68 func drawJpg(name string, mkplot func() *plot.Plot) {
69 if err := mkplot().Save(10*vg.Centimeter, 10*vg.Centimeter, name+".jpg"); err != nil {
70 panic(err)
71 }
72 }
73
74
75 func Example_errpoints() *plot.Plot {
76 rnd := rand.New(rand.NewSource(1))
77
78
79 n, m := 5, 10
80 pts := make([]plotter.XYer, n)
81 for i := range pts {
82 xys := make(plotter.XYs, m)
83 pts[i] = xys
84 center := float64(i)
85 for j := range xys {
86 xys[j].X = center + (rnd.Float64() - 0.5)
87 xys[j].Y = center + (rnd.Float64() - 0.5)
88 }
89 }
90
91 plt := plot.New()
92
93 mean95, err := plotutil.NewErrorPoints(plotutil.MeanAndConf95, pts...)
94 if err != nil {
95 panic(err)
96 }
97 medMinMax, err := plotutil.NewErrorPoints(plotutil.MedianAndMinMax, pts...)
98 if err != nil {
99 panic(err)
100 }
101 plotutil.AddLinePoints(plt,
102 "mean and 95% confidence", mean95,
103 "median and minimum and maximum", medMinMax)
104 if err := plotutil.AddErrorBars(plt, mean95, medMinMax); err != nil {
105 panic(err)
106 }
107 if err := plotutil.AddScatters(plt, pts[0], pts[1], pts[2], pts[3], pts[4]); err != nil {
108 panic(err)
109 }
110
111 return plt
112 }
113
114 type stackValues struct{ vs []plotter.Values }
115
116 func (n stackValues) Len() int { return n.vs[0].Len() }
117 func (n stackValues) Value(i int) float64 {
118 sum := 0.0
119 for _, v := range n.vs {
120 sum += v.Value(i)
121 }
122 return sum
123 }
124
125
126 func Example_stackedAreaChart() *plot.Plot {
127 p := plot.New()
128
129 p.Title.Text = "Example: Software Version Comparison"
130 p.X.Label.Text = "Date"
131 p.Y.Label.Text = "Users (in thousands)"
132
133 p.Legend.Top = true
134 p.Legend.Left = true
135
136 vals := []plotter.Values{
137 {0.02, 0.015, 0, 0, 0, 0, 0},
138 {0, 0.48, 0.36, 0.34, 0.32, 0.32, 0.28},
139 {0, 0, 0.87, 1.4, 0.64, 0.32, 0.28},
140 {0, 0, 0, 1.26, 0.34, 0.12, 0.09},
141 {0, 0, 0, 0, 2.48, 2.68, 2.13},
142 {0, 0, 0, 0, 0, 1.32, 0.54},
143 {0, 0, 0, 0, 0, 0.68, 5.67},
144 }
145
146 err := plotutil.AddStackedAreaPlots(p, plotter.Values{2007, 2008, 2009, 2010, 2011, 2012, 2013},
147 "Version 3.0",
148 stackValues{vs: vals[0:7]},
149 "Version 2.1",
150 stackValues{vs: vals[0:6]},
151 "Version 2.0.1",
152 stackValues{vs: vals[0:5]},
153 "Version 2.0",
154 stackValues{vs: vals[0:4]},
155 "Version 1.1",
156 stackValues{vs: vals[0:3]},
157 "Version 1.0",
158 stackValues{vs: vals[0:2]},
159 "Beta",
160 stackValues{vs: vals[0:1]},
161 )
162
163 if err != nil {
164 panic(err)
165 }
166
167 return p
168 }
169
View as plain text