go-hep.org/x/hep@v0.38.1/hplot/functions_example_test.go (about) 1 // Copyright ©2016 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copyright ©2015 The Gonum Authors. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the LICENSE file. 8 9 package hplot_test 10 11 import ( 12 "image/color" 13 "log" 14 "math" 15 16 "go-hep.org/x/hep/hplot" 17 "gonum.org/v1/plot" 18 "gonum.org/v1/plot/vg" 19 ) 20 21 // ExampleFunction draws some functions. 22 func ExampleFunction() { 23 quad := hplot.NewFunction(func(x float64) float64 { return x * x }) 24 quad.Color = color.RGBA{B: 255, A: 255} 25 26 exp := hplot.NewFunction(func(x float64) float64 { return math.Pow(2, x) }) 27 exp.Dashes = []vg.Length{vg.Points(2), vg.Points(2)} 28 exp.Width = vg.Points(2) 29 exp.Color = color.RGBA{G: 255, A: 255} 30 31 sin := hplot.NewFunction(func(x float64) float64 { return 10*math.Sin(x) + 50 }) 32 sin.Dashes = []vg.Length{vg.Points(4), vg.Points(5)} 33 sin.Width = vg.Points(4) 34 sin.Color = color.RGBA{R: 255, A: 255} 35 36 p := hplot.New() 37 p.Title.Text = "Functions" 38 p.X.Label.Text = "X" 39 p.Y.Label.Text = "Y" 40 41 p.Add(quad, exp, sin) 42 p.Legend.Add("x^2", quad) 43 p.Legend.Add("2^x", exp) 44 p.Legend.Add("10*sin(x)+50", sin) 45 p.Legend.ThumbnailWidth = 0.5 * vg.Inch 46 47 p.X.Min = 0 48 p.X.Max = 10 49 p.Y.Min = 0 50 p.Y.Max = 100 51 52 err := p.Save(200, 200, "testdata/functions.png") 53 if err != nil { 54 log.Panic(err) 55 } 56 } 57 58 // ExampleFunction_logY draws a function with a Log-Y axis. 59 func ExampleFunction_logY() { 60 quad := hplot.NewFunction(func(x float64) float64 { return x * x }) 61 quad.Color = color.RGBA{B: 255, A: 255} 62 63 fun := hplot.NewFunction(func(x float64) float64 { 64 switch { 65 case x < 6: 66 return 20 67 case 6 <= x && x < 7: 68 return 0 69 case 7 <= x && x < 7.5: 70 return 30 71 case 7.5 <= x && x < 9: 72 return 0 73 case 9 <= x: 74 return 40 75 } 76 return 50 77 }) 78 fun.LogY = true 79 fun.Color = color.RGBA{R: 255, A: 255} 80 81 p := hplot.New() 82 p.Title.Text = "Functions - Log-Y scale" 83 p.X.Label.Text = "X" 84 p.Y.Label.Text = "Y" 85 86 p.Y.Scale = plot.LogScale{} 87 p.Y.Tick.Marker = plot.LogTicks{} 88 89 p.Add(fun) 90 p.Add(quad) 91 p.Add(hplot.NewGrid()) 92 p.Legend.Add("x^2", quad) 93 p.Legend.Add("fct", fun) 94 p.Legend.ThumbnailWidth = 0.5 * vg.Inch 95 96 p.X.Min = 5 97 p.X.Max = 10 98 p.Y.Min = 10 99 p.Y.Max = 100 100 101 err := p.Save(200, 200, "testdata/functions_logy.png") 102 if err != nil { 103 log.Panic(err) 104 } 105 }