go-hep.org/x/hep@v0.38.1/hplot/ratioplot_example_test.go (about) 1 // Copyright ©2020 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 13 "go-hep.org/x/hep/hbook" 14 "go-hep.org/x/hep/hplot" 15 "gonum.org/v1/gonum/stat/distuv" 16 "gonum.org/v1/plot/vg" 17 ) 18 19 func ExampleRatioPlot() { 20 21 const npoints = 10000 22 23 // Create a normal distribution. 24 dist := distuv.Normal{ 25 Mu: 0, 26 Sigma: 1, 27 Src: rand.New(rand.NewPCG(0, 0)), 28 } 29 30 hist1 := hbook.NewH1D(20, -4, +4) 31 hist2 := hbook.NewH1D(20, -4, +4) 32 33 for range npoints { 34 v1 := dist.Rand() - 0.5 35 v2 := dist.Rand() + 0.5 36 hist1.Fill(v1, 1) 37 hist2.Fill(v2, 1) 38 } 39 40 rp := hplot.NewRatioPlot() 41 rp.Ratio = 0.3 42 43 // Make a plot and set its title. 44 rp.Top.Title.Text = "Histos" 45 rp.Top.Y.Label.Text = "Y" 46 47 // Create a histogram of our values drawn 48 // from the standard normal. 49 h1 := hplot.NewH1D(hist1) 50 h1.FillColor = color.NRGBA{R: 255, A: 100} 51 rp.Top.Add(h1) 52 53 h2 := hplot.NewH1D(hist2) 54 h2.FillColor = color.NRGBA{B: 255, A: 100} 55 rp.Top.Add(h2) 56 57 rp.Top.Add(hplot.NewGrid()) 58 59 hist3 := hbook.NewH1D(20, -4, +4) 60 for i := range hist3.Len() { 61 v1 := hist1.Value(i) 62 v2 := hist2.Value(i) 63 x1, _ := hist1.XY(i) 64 hist3.Fill(x1, v1-v2) 65 } 66 67 hdiff := hplot.NewH1D(hist3) 68 69 rp.Bottom.X.Label.Text = "X" 70 rp.Bottom.Y.Label.Text = "Delta-Y" 71 rp.Bottom.Add(hdiff) 72 rp.Bottom.Add(hplot.NewGrid()) 73 74 const ( 75 width = 15 * vg.Centimeter 76 height = width / math.Phi 77 ) 78 79 err := hplot.Save(rp, width, height, "testdata/diff_plot.png") 80 if err != nil { 81 log.Fatalf("error: %v\n", err) 82 } 83 }