go-hep.org/x/hep@v0.38.1/hplot/band_example_test.go (about)

     1  // Copyright ©2019 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/hplot"
    14  	"gonum.org/v1/gonum/stat/distuv"
    15  	"gonum.org/v1/plot/plotter"
    16  	"gonum.org/v1/plot/vg"
    17  	"gonum.org/v1/plot/vg/draw"
    18  )
    19  
    20  // An example of making a colored band plot
    21  func ExampleBand() {
    22  	const (
    23  		npoints = 100
    24  		xmax    = 10
    25  	)
    26  
    27  	// Create a normal distribution.
    28  	dist := distuv.Normal{
    29  		Mu:    0,
    30  		Sigma: 1,
    31  		Src:   rand.New(rand.NewPCG(0, 0)),
    32  	}
    33  
    34  	topData := make(plotter.XYs, npoints)
    35  	botData := make(plotter.XYs, npoints)
    36  
    37  	// Draw some random values from the standard
    38  	// normal distribution.
    39  	for i := range npoints {
    40  		x := float64(i+1) / xmax
    41  
    42  		v1 := dist.Rand()
    43  		v2 := dist.Rand()
    44  
    45  		topData[i].X = x
    46  		topData[i].Y = 1/x + v1 + 10
    47  
    48  		botData[i].X = x
    49  		botData[i].Y = math.Log(x) + v2
    50  	}
    51  
    52  	top, err := hplot.NewLine(topData)
    53  	if err != nil {
    54  		log.Fatalf("error: %+v", err)
    55  	}
    56  	top.LineStyle.Color = color.RGBA{R: 255, A: 255}
    57  
    58  	bot, err := hplot.NewLine(botData)
    59  	if err != nil {
    60  		log.Fatalf("error: %+v", err)
    61  	}
    62  	bot.LineStyle.Color = color.RGBA{B: 255, A: 255}
    63  
    64  	tp := hplot.NewTiledPlot(draw.Tiles{Cols: 1, Rows: 2})
    65  
    66  	tp.Plots[0].Title.Text = "Band"
    67  	tp.Plots[0].Add(
    68  		top,
    69  		bot,
    70  		hplot.NewBand(color.Gray{200}, topData, botData),
    71  	)
    72  
    73  	tp.Plots[1].Title.Text = "Band"
    74  	var (
    75  		blue = color.RGBA{B: 255, A: 255}
    76  		grey = color.Gray{200}
    77  		band = hplot.NewBand(grey, topData, botData)
    78  	)
    79  	band.LineStyle = plotter.DefaultLineStyle
    80  	band.LineStyle.Color = blue
    81  	tp.Plots[1].Add(band)
    82  
    83  	err = tp.Save(10*vg.Centimeter, -1, "testdata/band.png")
    84  	if err != nil {
    85  		log.Fatalf("error: %+v", err)
    86  	}
    87  }