...
1
2
3
4
5 package plotter_test
6
7 import (
8 "image/color"
9 "log"
10 "math"
11
12 "gonum.org/v1/plot"
13 "gonum.org/v1/plot/plotter"
14 "gonum.org/v1/plot/vg"
15 )
16
17
18 func ExampleFunction() {
19 quad := plotter.NewFunction(func(x float64) float64 { return x * x })
20 quad.Color = color.RGBA{B: 255, A: 255}
21
22 exp := plotter.NewFunction(func(x float64) float64 { return math.Pow(2, x) })
23 exp.Dashes = []vg.Length{vg.Points(2), vg.Points(2)}
24 exp.Width = vg.Points(2)
25 exp.Color = color.RGBA{G: 255, A: 255}
26
27 sin := plotter.NewFunction(func(x float64) float64 { return 10*math.Sin(x) + 50 })
28 sin.Dashes = []vg.Length{vg.Points(4), vg.Points(5)}
29 sin.Width = vg.Points(4)
30 sin.Color = color.RGBA{R: 255, A: 255}
31
32 p := plot.New()
33 p.Title.Text = "Functions"
34 p.X.Label.Text = "X"
35 p.Y.Label.Text = "Y"
36
37 p.Add(quad, exp, sin)
38 p.Legend.Add("x^2", quad)
39 p.Legend.Add("2^x", exp)
40 p.Legend.Add("10*sin(x)+50", sin)
41 p.Legend.ThumbnailWidth = 0.5 * vg.Inch
42
43 p.X.Min = 0
44 p.X.Max = 10
45 p.Y.Min = 0
46 p.Y.Max = 100
47
48 err := p.Save(200, 200, "testdata/functions.png")
49 if err != nil {
50 log.Panic(err)
51 }
52 }
53
View as plain text