go-hep.org/x/hep@v0.38.1/hplot/h2d_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  	"testing"
    10  
    11  	"go-hep.org/x/hep/hbook"
    12  	"go-hep.org/x/hep/hplot"
    13  	"gonum.org/v1/plot/cmpimg"
    14  	"gonum.org/v1/plot/plotter"
    15  	"gonum.org/v1/plot/vg"
    16  )
    17  
    18  func TestH2D(t *testing.T) {
    19  	checkPlot(cmpimg.CheckPlot)(ExampleH2D, t, "h2d_plot.png")
    20  }
    21  
    22  func TestH2DWithLegend(t *testing.T) {
    23  	checkPlot(cmpimg.CheckPlot)(ExampleH2D_withLegend, t, "h2d_plot_legend.png")
    24  }
    25  
    26  func TestH2DABCD(t *testing.T) {
    27  	checkPlot(cmpimg.CheckPlot)(func() {
    28  		h := hbook.NewH2D(2, 0, 2, 2, 0, 2)
    29  		h.Fill(0, 0, 1)
    30  		h.Fill(1, 0, 2)
    31  		h.Fill(0, 1, 3)
    32  		h.Fill(1, 1, 4)
    33  
    34  		p := hplot.New()
    35  		p.Title.Text = "Hist-2D"
    36  		p.X.Label.Text = "x"
    37  		p.X.Min = -1
    38  		p.X.Max = +3
    39  		p.Y.Label.Text = "y"
    40  		p.Y.Min = -1
    41  		p.Y.Max = +3
    42  
    43  		p.Add(hplot.NewH2D(h, nil))
    44  		p.Add(plotter.NewGrid())
    45  
    46  		err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/h2d_plot_abcd.png")
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  	}, t, "h2d_plot_abcd.png")
    51  }
    52  
    53  func TestH2MinMax(t *testing.T) {
    54  	checkPlot(cmpimg.CheckPlot)(func() {
    55  		type pair struct{ i, j int }
    56  
    57  		const n = 3
    58  		h2 := hbook.NewH2D(n, 0, n, n, 0, n)
    59  		ws := map[pair]float64{
    60  			{0, 0}: 0,
    61  			{0, 1}: 1,
    62  			{0, 2}: 2,
    63  			{1, 0}: 0,
    64  			{1, 1}: 1,
    65  			{1, 2}: 2,
    66  			{2, 0}: 0,
    67  			{2, 1}: 1,
    68  			{2, 2}: 2,
    69  		}
    70  		for i := range n {
    71  			ix := float64(i)
    72  			for j := range n {
    73  				iy := float64(j)
    74  				p := pair{i, j}
    75  				v := ws[p]
    76  				h2.Fill(ix, iy, v)
    77  			}
    78  		}
    79  
    80  		pl := hplot.New()
    81  		pl.X.Min = 0
    82  		pl.X.Max = 3
    83  		pl.Y.Min = 0
    84  		pl.Y.Max = 3
    85  		hh := hplot.NewH2D(h2, labels{})
    86  		hh.HeatMap.Min = 0
    87  		hh.HeatMap.Max = 3
    88  		pl.Add(hh)
    89  
    90  		err := pl.Save(5*vg.Centimeter, 5*vg.Centimeter, "testdata/h2d_plot_minmax.png")
    91  		if err != nil {
    92  			t.Fatal(err)
    93  		}
    94  
    95  	}, t, "h2d_plot_minmax.png")
    96  }
    97  
    98  type labels struct{}
    99  
   100  func (labels) Colors() []color.Color {
   101  	return []color.Color{
   102  		color.RGBA{255, 0, 0, 255},
   103  		color.RGBA{0, 255, 0, 255},
   104  		color.RGBA{0, 0, 255, 255},
   105  		color.Black,
   106  	}
   107  }