go-hep.org/x/hep@v0.38.1/hplot/binerrband_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/rand/v2"
    11  
    12  	"go-hep.org/x/hep/hbook"
    13  	"go-hep.org/x/hep/hplot"
    14  	"gonum.org/v1/gonum/stat/distuv"
    15  	"gonum.org/v1/plot/vg"
    16  )
    17  
    18  // An example of making a colored binned error band
    19  // from scratch.
    20  func ExampleBinnedErrBand() {
    21  
    22  	// Number bins
    23  	nBins := 10
    24  
    25  	// Creation of a slice of hbook.Count.
    26  	counts := make([]hbook.Count, nBins)
    27  	for i, xrange := range newBinning(nBins, 0, 10) {
    28  		y := float64(i + 1)
    29  		counts[i].XRange = xrange
    30  		counts[i].Val = y
    31  		counts[i].Err.Low = 0.1 * (y - 5) * (y - 5)
    32  		counts[i].Err.High = 0.1 * (y - 5) * (y - 5)
    33  	}
    34  
    35  	// Set error of the 5th bin to zero
    36  	counts[4].Err.Low, counts[4].Err.High = 0, 0
    37  
    38  	// Binned error band
    39  	b := &hplot.BinnedErrBand{Counts: counts}
    40  	b.FillColor = color.NRGBA{B: 180, A: 200}
    41  	b.LineStyle.Color = color.NRGBA{R: 180, A: 200}
    42  	b.LineStyle.Width = 2
    43  
    44  	// Create a new plot and add b
    45  	p := hplot.New()
    46  	p.Title.Text = "Binned Error Band"
    47  	p.X.Label.Text = "Binned X"
    48  	p.Y.Label.Text = "Y"
    49  	p.Add(b)
    50  
    51  	// Save the result
    52  	err := p.Save(10*vg.Centimeter, -1, "testdata/binnederrband.png")
    53  	if err != nil {
    54  		log.Fatalf("error: %+v", err)
    55  	}
    56  }
    57  
    58  func ExampleBinnedErrBand_fromH1D() {
    59  
    60  	// Histogram
    61  	h := hbook.NewH1D(20, -5, 5)
    62  	for range 1000 {
    63  		x := gauss.Rand()
    64  		if 0 < x && x < 0.5 {
    65  			continue
    66  		}
    67  		h.Fill(x, 1)
    68  	}
    69  
    70  	hp := hplot.NewH1D(h)
    71  	hp.LineStyle.Width = 0
    72  	hp.FillColor = color.NRGBA{R: 180, G: 180, B: 180, A: 200}
    73  
    74  	// Binned error band from the histo counts.
    75  	b := hplot.NewBinnedErrBand(h.Counts())
    76  	b.FillColor = color.NRGBA{B: 180, A: 100}
    77  	b.LineStyle.Color = color.NRGBA{B: 100, A: 200}
    78  	b.LineStyle.Width = 1
    79  
    80  	// Create a new plot and add the histo and the band.
    81  	p := hplot.New()
    82  	p.Title.Text = "Binned Error Band from H1D"
    83  	p.X.Label.Text = "Binned X"
    84  	p.Y.Label.Text = "Y"
    85  	p.Add(hp)
    86  	p.Add(b)
    87  
    88  	// Save the result
    89  	err := p.Save(10*vg.Centimeter, -1, "testdata/binnederrband_fromh1d.png")
    90  	if err != nil {
    91  		log.Fatalf("error: %+v", err)
    92  	}
    93  }
    94  
    95  // newBinning returns a slice of Range corresponding to
    96  // an equally spaced binning.
    97  func newBinning(n int, xmin, xmax float64) []hbook.Range {
    98  	res := make([]hbook.Range, n)
    99  	dx := (xmax - xmin) / float64(n)
   100  	for i := range n {
   101  		lo := xmin + float64(i)*dx
   102  		hi := lo + dx
   103  		res[i].Min = lo
   104  		res[i].Max = hi
   105  	}
   106  	return res
   107  }
   108  
   109  var (
   110  	gauss = distuv.Normal{
   111  		Mu:    0,
   112  		Sigma: 1,
   113  		Src:   rand.New(rand.NewPCG(0, 0)),
   114  	}
   115  )