go-hep.org/x/hep@v0.38.1/hplot/tiledplot_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  	"fmt"
     9  	"image/color"
    10  	"log"
    11  	"math"
    12  	"math/rand/v2"
    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  )
    20  
    21  // An example of making a tile-plot
    22  func ExampleTiledPlot() {
    23  	tp := hplot.NewTiledPlot(draw.Tiles{Cols: 3, Rows: 2})
    24  
    25  	// Create a normal distribution.
    26  	dist := distuv.Normal{
    27  		Mu:    0,
    28  		Sigma: 1,
    29  		Src:   rand.New(rand.NewPCG(0, 0)),
    30  	}
    31  
    32  	newHist := func(p *hplot.Plot) {
    33  		const npoints = 10000
    34  		hist := hbook.NewH1D(20, -4, +4)
    35  		for range npoints {
    36  			v := dist.Rand()
    37  			hist.Fill(v, 1)
    38  		}
    39  
    40  		h := hplot.NewH1D(hist)
    41  		p.Add(h)
    42  	}
    43  
    44  	for i := range tp.Tiles.Rows {
    45  		for j := range tp.Tiles.Cols {
    46  			p := tp.Plot(j, i)
    47  			p.X.Min = -5
    48  			p.X.Max = +5
    49  			newHist(p)
    50  			p.Title.Text = fmt.Sprintf("hist - (%02d, %02d)", j, i)
    51  		}
    52  	}
    53  
    54  	// remove plot at (1,0)
    55  	tp.Plots[1] = nil
    56  
    57  	err := tp.Save(15*vg.Centimeter, -1, "testdata/tiled_plot_histogram.png")
    58  	if err != nil {
    59  		log.Fatalf("error: %+v\n", err)
    60  	}
    61  }
    62  
    63  // An example of making aligned tile-plots
    64  func ExampleTiledPlot_align() {
    65  	tp := hplot.NewTiledPlot(draw.Tiles{
    66  		Cols: 3, Rows: 3,
    67  		PadX: 20, PadY: 20,
    68  	})
    69  	tp.Align = true
    70  
    71  	points := func(i, j int) []hbook.Point2D {
    72  		n := i*tp.Tiles.Cols + j + 1
    73  		i += 1
    74  		j = int(math.Pow(10, float64(n)))
    75  
    76  		var pts []hbook.Point2D
    77  		for ii := range 10 {
    78  			pts = append(pts, hbook.Point2D{
    79  				X: float64(i + ii),
    80  				Y: float64(j + ii + 1),
    81  			})
    82  		}
    83  		return pts
    84  
    85  	}
    86  
    87  	for i := range tp.Tiles.Rows {
    88  		for j := range tp.Tiles.Cols {
    89  			p := tp.Plot(j, i)
    90  			p.X.Min = -5
    91  			p.X.Max = +5
    92  			s := hplot.NewS2D(hbook.NewS2D(points(i, j)...))
    93  			s.GlyphStyle.Color = color.RGBA{R: 255, A: 255}
    94  			s.GlyphStyle.Radius = vg.Points(4)
    95  			p.Add(s)
    96  
    97  			p.Title.Text = fmt.Sprintf("hist - (%02d, %02d)", j, i)
    98  		}
    99  	}
   100  
   101  	// remove plot at (1,1)
   102  	tp.Plots[4] = nil
   103  
   104  	err := tp.Save(15*vg.Centimeter, -1, "testdata/tiled_plot_aligned_histogram.png")
   105  	if err != nil {
   106  		log.Fatalf("error: %+v\n", err)
   107  	}
   108  }