go-hep.org/x/hep@v0.38.1/hplot/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 package hplot_test 6 7 import ( 8 "image/color" 9 "log" 10 "math" 11 "math/rand/v2" 12 "os" 13 14 "go-hep.org/x/hep/hbook" 15 "go-hep.org/x/hep/hplot" 16 "gonum.org/v1/gonum/stat/distuv" 17 "gonum.org/v1/plot/vg" 18 "gonum.org/v1/plot/vg/draw" 19 "gonum.org/v1/plot/vg/vgimg" 20 ) 21 22 // An example of a plot + sub-plot 23 func Example_subplot() { 24 const npoints = 10000 25 26 // Create a normal distribution. 27 dist := distuv.Normal{ 28 Mu: 0, 29 Sigma: 1, 30 Src: rand.New(rand.NewPCG(0, 0)), 31 } 32 33 // Draw some random values from the standard 34 // normal distribution. 35 hist := hbook.NewH1D(20, -4, +4) 36 for range npoints { 37 v := dist.Rand() 38 hist.Fill(v, 1) 39 } 40 41 // normalize histo 42 area := 0.0 43 for _, bin := range hist.Binning.Bins { 44 area += bin.SumW() * bin.XWidth() 45 } 46 hist.Scale(1 / area) 47 48 // Make a plot and set its title. 49 p1 := hplot.New() 50 p1.Title.Text = "Histogram" 51 p1.X.Label.Text = "X" 52 p1.Y.Label.Text = "Y" 53 54 // Create a histogram of our values drawn 55 // from the standard normal. 56 h := hplot.NewH1D(hist) 57 p1.Add(h) 58 59 // The normal distribution function 60 norm := hplot.NewFunction(dist.Prob) 61 norm.Color = color.RGBA{R: 255, A: 255} 62 norm.Width = vg.Points(2) 63 p1.Add(norm) 64 65 // draw a grid 66 p1.Add(hplot.NewGrid()) 67 68 // make a second plot which will be diplayed in the upper-right 69 // of the previous one 70 p2 := hplot.New() 71 p2.Title.Text = "Sub plot" 72 p2.Add(h) 73 p2.Add(hplot.NewGrid()) 74 75 const ( 76 width = 15 * vg.Centimeter 77 height = width / math.Phi 78 ) 79 80 c := vgimg.PngCanvas{Canvas: vgimg.New(width, height)} 81 dc := draw.New(c) 82 p1.Draw(dc) 83 sub := draw.Canvas{ 84 Canvas: dc, 85 Rectangle: vg.Rectangle{ 86 Min: vg.Point{X: 0.70 * width, Y: 0.50 * height}, 87 Max: vg.Point{X: 1.00 * width, Y: 1.00 * height}, 88 }, 89 } 90 p2.Draw(sub) 91 92 f, err := os.Create("testdata/sub_plot.png") 93 if err != nil { 94 log.Fatalf("error: %v\n", err) 95 } 96 defer f.Close() 97 _, err = c.WriteTo(f) 98 if err != nil { 99 log.Fatal(err) 100 } 101 err = f.Close() 102 if err != nil { 103 log.Fatal(err) 104 } 105 106 } 107 108 func Example_latexplot() { 109 110 const npoints = 10000 111 112 // Create a normal distribution. 113 dist := distuv.Normal{ 114 Mu: 0, 115 Sigma: 1, 116 Src: rand.New(rand.NewPCG(0, 0)), 117 } 118 119 hist := hbook.NewH1D(20, -4, +4) 120 for range npoints { 121 v := dist.Rand() 122 hist.Fill(v, 1) 123 } 124 125 // Make a plot and set its title. 126 p := hplot.New() 127 p.Title.Text = `Gaussian distribution: $f(x) = \frac{e^{-(x - \mu)^{2}/(2\sigma^{2}) }} {\sigma\sqrt{2\pi}}$` 128 p.Y.Label.Text = `$f(x)$` 129 p.X.Label.Text = `$x$` 130 131 // Create a histogram of our values drawn 132 // from the standard normal. 133 h := hplot.NewH1D(hist) 134 h.LineStyle.Color = color.RGBA{R: 255, A: 255} 135 h.FillColor = nil 136 h.Infos.Style = hplot.HInfoSummary 137 p.Add(h) 138 139 p.Add(hplot.NewGrid()) 140 141 const ( 142 width = 15 * vg.Centimeter 143 height = width / math.Phi 144 ) 145 146 fig := hplot.Figure(p, hplot.WithBorder(hplot.Border{ 147 Left: 5, 148 Right: 5, 149 Top: 5, 150 Bottom: 5, 151 })) 152 153 err := hplot.Save(fig, width, height, "testdata/latex_plot.tex") 154 if err != nil { 155 log.Fatalf("could not save LaTeX plot: %+v\n", err) 156 } 157 } 158 159 func ExampleSave() { 160 p := hplot.New() 161 p.Title.Text = "my title" 162 p.X.Label.Text = "x" 163 p.Y.Label.Text = "y" 164 165 const ( 166 width = -1 // automatically choose a nice plot width 167 height = -1 // automatically choose a nice plot height 168 ) 169 170 err := hplot.Save( 171 p, 172 width, height, 173 "testdata/plot_save.eps", 174 "testdata/plot_save.jpg", 175 "testdata/plot_save.pdf", 176 "testdata/plot_save.png", 177 "testdata/plot_save.svg", 178 "testdata/plot_save.tex", 179 "testdata/plot_save.tif", 180 ) 181 182 if err != nil { 183 log.Fatalf("could not save plot: %+v", err) 184 } 185 }