go-hep.org/x/hep@v0.38.1/hplot/hstack_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  	"fmt"
     9  	"image/color"
    10  	"log"
    11  	"runtime"
    12  	"testing"
    13  
    14  	"go-hep.org/x/hep/hbook"
    15  	"go-hep.org/x/hep/hplot"
    16  	"gonum.org/v1/plot/cmpimg"
    17  	"gonum.org/v1/plot/vg"
    18  )
    19  
    20  func TestHStack(t *testing.T) {
    21  	if runtime.GOOS == "darwin" {
    22  		t.Skipf("ignore test b/c of darwin+Mac-silicon")
    23  	}
    24  
    25  	checkPlot(cmpimg.CheckPlot)(ExampleHStack, t, "hstack.png")
    26  	checkPlot(cmpimg.CheckPlot)(ExampleHStack_withBand, t, "hstack_band.png")
    27  	checkPlot(cmpimg.CheckPlot)(ExampleHStack_withLogY, t, "hstack_logy.png")
    28  }
    29  
    30  func TestHStackPanic(t *testing.T) {
    31  	for _, tc := range []struct {
    32  		fct    func() []*hplot.H1D
    33  		panics error
    34  	}{
    35  		{
    36  			fct: func() []*hplot.H1D {
    37  				return nil
    38  			},
    39  			panics: fmt.Errorf("hplot: not enough histograms to make a stack"),
    40  		},
    41  		{
    42  			fct: func() []*hplot.H1D {
    43  				return make([]*hplot.H1D, 0)
    44  			},
    45  			panics: fmt.Errorf("hplot: not enough histograms to make a stack"),
    46  		},
    47  		{
    48  			fct: func() []*hplot.H1D {
    49  				return []*hplot.H1D{
    50  					hplot.NewH1D(hbook.NewH1D(10, 0, 10)),
    51  					hplot.NewH1D(hbook.NewH1D(11, 0, 10)),
    52  				}
    53  			},
    54  			panics: fmt.Errorf("hplot: bins length mismatch"),
    55  		},
    56  		{
    57  			fct: func() []*hplot.H1D {
    58  				return []*hplot.H1D{
    59  					hplot.NewH1D(hbook.NewH1D(10, 0, 10)),
    60  					hplot.NewH1D(hbook.NewH1D(10, 0, 11)),
    61  				}
    62  			},
    63  			panics: fmt.Errorf("hplot: bin range mismatch"),
    64  		},
    65  	} {
    66  		t.Run("", func(t *testing.T) {
    67  			defer func() {
    68  				err := recover()
    69  				if err == nil {
    70  					t.Fatalf("expected a panic")
    71  				}
    72  				switch err := err.(type) {
    73  				case string:
    74  					if got, want := err, tc.panics.Error(); got != want {
    75  						t.Fatalf(
    76  							"invalid panic message:\ngot= %v\nwant=%v",
    77  							got, want,
    78  						)
    79  					}
    80  				case error:
    81  					if got, want := err.Error(), tc.panics.Error(); got != want {
    82  						t.Fatalf(
    83  							"invalid panic message:\ngot= %v\nwant=%v",
    84  							got, want,
    85  						)
    86  					}
    87  				}
    88  			}()
    89  			_ = hplot.NewHStack(tc.fct())
    90  		})
    91  	}
    92  }
    93  
    94  func TestHStackCornerBins(t *testing.T) {
    95  	checkPlot(cmpimg.CheckPlot)(func() {
    96  		h1 := hbook.NewH1D(10, 0, 10)
    97  		h2 := hbook.NewH1D(10, 0, 10)
    98  		h3 := hbook.NewH1D(10, 0, 10)
    99  
   100  		h1.FillN(
   101  			[]float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   102  			[]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
   103  		)
   104  		h2.FillN(
   105  			[]float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   106  			[]float64{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
   107  		)
   108  		h3.FillN(
   109  			[]float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
   110  			[]float64{10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
   111  		)
   112  
   113  		colors := []color.Color{
   114  			color.NRGBA{122, 195, 106, 150},
   115  			color.NRGBA{90, 155, 212, 150},
   116  			color.NRGBA{250, 167, 91, 150},
   117  		}
   118  
   119  		hh1 := hplot.NewH1D(h1)
   120  		hh1.FillColor = colors[0]
   121  		hh1.LineStyle.Color = color.Black
   122  
   123  		hh2 := hplot.NewH1D(h2)
   124  		hh2.FillColor = colors[1]
   125  		hh2.LineStyle.Width = 0
   126  
   127  		hh3 := hplot.NewH1D(h3)
   128  		hh3.FillColor = colors[2]
   129  		hh3.LineStyle.Color = color.Black
   130  
   131  		hs := []*hplot.H1D{hh1, hh2, hh3}
   132  
   133  		p := hplot.New()
   134  		p.X.Label.Text = "X"
   135  		p.Y.Label.Text = "Y"
   136  		p.Y.Min = -0.5
   137  		p.Y.Max = 15.5
   138  		hstack := hplot.NewHStack(hs, hplot.WithLogY(false))
   139  		p.Add(hstack, hplot.NewGrid())
   140  		p.Legend.Add("h1", hs[0])
   141  		p.Legend.Add("h2", hs[1])
   142  		p.Legend.Add("h3", hs[2])
   143  		p.Legend.Top = true
   144  		p.Legend.Left = true
   145  
   146  		err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/hstack_corner_bins.png")
   147  		if err != nil {
   148  			log.Fatalf("error: %+v", err)
   149  		}
   150  
   151  	}, t, "hstack_corner_bins.png")
   152  }