go-hep.org/x/hep@v0.38.1/hplot/h1d_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  	"math"
    10  	"math/rand/v2"
    11  	"os"
    12  	"runtime"
    13  	"testing"
    14  
    15  	"go-hep.org/x/hep/hbook"
    16  	"go-hep.org/x/hep/hplot"
    17  	"gonum.org/v1/gonum/stat/distuv"
    18  	"gonum.org/v1/plot/cmpimg"
    19  	"gonum.org/v1/plot/vg"
    20  	"gonum.org/v1/plot/vg/draw"
    21  	"gonum.org/v1/plot/vg/vgimg"
    22  )
    23  
    24  func TestH1D(t *testing.T) {
    25  	if runtime.GOOS == "darwin" {
    26  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    27  	}
    28  	checkPlot(cmpimg.CheckPlot)(ExampleH1D, t, "h1d_plot.png")
    29  }
    30  
    31  func TestH1DtoPDF(t *testing.T) {
    32  	if runtime.GOOS == "darwin" {
    33  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    34  	}
    35  
    36  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_toPDF, t, "h1d_plot.pdf")
    37  }
    38  
    39  func TestH1DLogScale(t *testing.T) {
    40  	if runtime.GOOS == "darwin" {
    41  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    42  	}
    43  
    44  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_logScaleY, t, "h1d_logy.png")
    45  }
    46  
    47  func TestH1DYErrs(t *testing.T) {
    48  	if runtime.GOOS == "darwin" {
    49  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    50  	}
    51  
    52  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_withYErrBars, t, "h1d_yerrs.png")
    53  }
    54  
    55  func TestH1DYErrsBand(t *testing.T) {
    56  	if runtime.GOOS == "darwin" {
    57  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    58  	}
    59  
    60  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_withYErrBars_withBand, t, "h1d_yerrs_band.png")
    61  }
    62  
    63  func TestH1DAsData(t *testing.T) {
    64  	if runtime.GOOS == "darwin" {
    65  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    66  	}
    67  
    68  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_withYErrBarsAndData, t, "h1d_glyphs.png")
    69  }
    70  
    71  func TestH1DLegendStyle(t *testing.T) {
    72  	if runtime.GOOS == "darwin" {
    73  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    74  	}
    75  
    76  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_legendStyle, t, "h1d_legend.png")
    77  }
    78  
    79  func TestH1DWithBorders(t *testing.T) {
    80  	if runtime.GOOS == "darwin" {
    81  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    82  	}
    83  
    84  	_ = os.Remove("testdata/h1d_borders.png")
    85  	checkPlot(cmpimg.CheckPlot)(ExampleH1D_withPlotBorders, t, "h1d_borders.png")
    86  
    87  	_ = os.Remove("testdata/h1d_borders.png")
    88  	// check that it works with a vg.Canvas-WriterTo.
    89  	checkPlot(cmpimg.CheckPlot)(func() {
    90  		const npoints = 10000
    91  
    92  		// Create a normal distribution.
    93  		dist := distuv.Normal{
    94  			Mu:    0,
    95  			Sigma: 1,
    96  			Src:   rand.New(rand.NewPCG(0, 0)),
    97  		}
    98  
    99  		// Draw some random values from the standard
   100  		// normal distribution.
   101  		hist := hbook.NewH1D(20, -4, +4)
   102  		for range npoints {
   103  			v := dist.Rand()
   104  			hist.Fill(v, 1)
   105  		}
   106  
   107  		// normalize histogram
   108  		area := 0.0
   109  		for _, bin := range hist.Binning.Bins {
   110  			area += bin.SumW() * bin.XWidth()
   111  		}
   112  		hist.Scale(1 / area)
   113  
   114  		// Make a plot and set its title.
   115  		p := hplot.New()
   116  		p.Title.Text = "Histogram"
   117  		p.X.Label.Text = "X"
   118  		p.Y.Label.Text = "Y"
   119  
   120  		// Create a histogram of our values drawn
   121  		// from the standard normal.
   122  		h := hplot.NewH1D(hist)
   123  		h.Infos.Style = hplot.HInfoSummary
   124  		p.Add(h)
   125  
   126  		// The normal distribution function
   127  		norm := hplot.NewFunction(dist.Prob)
   128  		norm.Color = color.RGBA{R: 255, A: 255}
   129  		norm.Width = vg.Points(2)
   130  		p.Add(norm)
   131  
   132  		// draw a grid
   133  		p.Add(hplot.NewGrid())
   134  
   135  		fig := hplot.Figure(p, hplot.WithBorder(hplot.Border{
   136  			Right:  25,
   137  			Left:   20,
   138  			Top:    25,
   139  			Bottom: 20,
   140  		}))
   141  
   142  		c := vgimg.NewWith(
   143  			vgimg.UseWH(6*vg.Inch, 6*vg.Inch/math.Phi),
   144  		)
   145  		dc := draw.New(c)
   146  		fig.Draw(dc)
   147  
   148  		f, err := os.Create("testdata/h1d_borders.png")
   149  		if err != nil {
   150  			t.Fatalf("could not create output plot file: %+v", err)
   151  		}
   152  		defer f.Close()
   153  
   154  		img := vgimg.PngCanvas{Canvas: c}
   155  		_, err = img.WriteTo(f)
   156  		if err != nil {
   157  			t.Fatalf("could not encode canvas to png: %+v", err)
   158  		}
   159  
   160  		err = f.Close()
   161  		if err != nil {
   162  			t.Fatalf("could not save file: %+v", err)
   163  		}
   164  	}, t, "h1d_borders.png")
   165  }