go-hep.org/x/hep@v0.38.1/hplot/s2d_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  	"image/color"
     9  	"log"
    10  	"math/rand/v2"
    11  
    12  	"go-hep.org/x/hep/hbook"
    13  	"go-hep.org/x/hep/hplot"
    14  	"gonum.org/v1/gonum/mat"
    15  	"gonum.org/v1/gonum/stat/distmv"
    16  	"gonum.org/v1/plot/plotter"
    17  	"gonum.org/v1/plot/plotutil"
    18  	"gonum.org/v1/plot/vg"
    19  	"gonum.org/v1/plot/vg/draw"
    20  )
    21  
    22  // ExampleS2D draws some scatter points.
    23  func ExampleS2D() {
    24  	const npoints = 1000
    25  
    26  	dist, ok := distmv.NewNormal(
    27  		[]float64{0, 1},
    28  		mat.NewSymDense(2, []float64{4, 0, 0, 2}),
    29  		rand.New(rand.NewPCG(1234, 1234)),
    30  	)
    31  	if !ok {
    32  		log.Fatalf("error creating distmv.Normal")
    33  	}
    34  
    35  	s2d := hbook.NewS2D()
    36  
    37  	v := make([]float64, 2)
    38  	// Draw some random values from the standard
    39  	// normal distribution.
    40  	for range npoints {
    41  		v = dist.Rand(v)
    42  		s2d.Fill(hbook.Point2D{X: v[0], Y: v[1]})
    43  	}
    44  
    45  	p := hplot.New()
    46  	p.Title.Text = "Scatter-2D"
    47  	p.X.Label.Text = "X"
    48  	p.Y.Label.Text = "Y"
    49  	p.Add(plotter.NewGrid())
    50  
    51  	s := hplot.NewS2D(s2d)
    52  	s.GlyphStyle.Color = color.RGBA{R: 255, A: 255}
    53  	s.GlyphStyle.Radius = vg.Points(2)
    54  
    55  	p.Add(s)
    56  
    57  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/s2d.png")
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  }
    62  
    63  // ExampleS2D_withErrorBars draws some scatter points
    64  // with their error bars.
    65  func ExampleS2D_withErrorBars() {
    66  	pts := []hbook.Point2D{
    67  		{X: 1, Y: 1, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, ErrY: hbook.Range{Min: 2, Max: 3}},
    68  		{X: 2, Y: 2, ErrX: hbook.Range{Min: 0.5, Max: 1.5}, ErrY: hbook.Range{Min: 5, Max: 2}},
    69  	}
    70  	s2d := hbook.NewS2D(pts...)
    71  
    72  	p := hplot.New()
    73  	p.Title.Text = "Scatter-2D (with error bars)"
    74  	p.X.Label.Text = "X"
    75  	p.Y.Label.Text = "Y"
    76  	p.Add(plotter.NewGrid())
    77  
    78  	s := hplot.NewS2D(s2d,
    79  		hplot.WithXErrBars(true),
    80  		hplot.WithYErrBars(true),
    81  		hplot.WithGlyphStyle(draw.GlyphStyle{
    82  			Color:  color.RGBA{R: 255, A: 255},
    83  			Radius: vg.Points(4),
    84  			Shape:  draw.CrossGlyph{},
    85  		}),
    86  	)
    87  
    88  	p.Add(s)
    89  	p.Legend.Add("s2d", s)
    90  
    91  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/s2d_errbars.png")
    92  	if err != nil {
    93  		log.Fatal(err)
    94  	}
    95  }
    96  
    97  // ExampleS2D_withBand draws some scatter points
    98  // with their error bars and a band
    99  func ExampleS2D_withBand() {
   100  	pts := []hbook.Point2D{
   101  		{X: 1, Y: 1, ErrY: hbook.Range{Min: 2, Max: 3}},
   102  		{X: 2, Y: 2, ErrY: hbook.Range{Min: 5, Max: 2}},
   103  		{X: 3, Y: 3, ErrY: hbook.Range{Min: 2, Max: 2}},
   104  		{X: 4, Y: 4, ErrY: hbook.Range{Min: 1.2, Max: 2}},
   105  	}
   106  	s2d := hbook.NewS2D(pts...)
   107  
   108  	p := hplot.New()
   109  	p.Title.Text = "Scatter-2D (with band)"
   110  	p.X.Label.Text = "X"
   111  	p.Y.Label.Text = "Y"
   112  	p.Add(plotter.NewGrid())
   113  
   114  	s := hplot.NewS2D(s2d, hplot.WithBand(true), hplot.WithYErrBars(true))
   115  	s.GlyphStyle.Color = color.Black
   116  	s.GlyphStyle.Radius = vg.Points(4)
   117  	s.LineStyle.Width = 1
   118  	s.LineStyle.Dashes = plotutil.Dashes(2)
   119  
   120  	p.Add(s)
   121  	p.Legend.Add("s2d", s)
   122  
   123  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/s2d_band.png")
   124  	if err != nil {
   125  		log.Fatal(err)
   126  	}
   127  }
   128  
   129  // ExampleS2D_withStepsKind draws some scatter points
   130  // with their error bars, using a step-like style
   131  func ExampleS2D_withStepsKind() {
   132  	pts := []hbook.Point2D{
   133  		{X: 1, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 1, ErrY: hbook.Range{Min: 2, Max: 3}},
   134  		{X: 2, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 2, ErrY: hbook.Range{Min: 5, Max: 2}},
   135  		{X: 3, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 3, ErrY: hbook.Range{Min: 2, Max: 2}},
   136  		{X: 4, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 4, ErrY: hbook.Range{Min: 1.2, Max: 2}},
   137  	}
   138  	s2d := hbook.NewS2D(pts...)
   139  
   140  	p := hplot.New()
   141  	p.Title.Text = "Scatter-2D (with steps)"
   142  	p.X.Label.Text = "X"
   143  	p.Y.Label.Text = "Y"
   144  	p.Add(plotter.NewGrid())
   145  
   146  	s := hplot.NewS2D(s2d, hplot.WithStepsKind(hplot.HiSteps), hplot.WithYErrBars(true))
   147  	s.GlyphStyle.Color = color.Black
   148  	s.GlyphStyle.Radius = vg.Points(4)
   149  	s.LineStyle.Width = 1
   150  	s.LineStyle.Dashes = plotutil.Dashes(2)
   151  
   152  	p.Add(s)
   153  
   154  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/s2d_steps.png")
   155  	if err != nil {
   156  		log.Fatal(err)
   157  	}
   158  }
   159  
   160  // ExampleS2D_withPreMidPostSteps draws some scatter points using a step-like style
   161  func ExampleS2D_withPreMidPostSteps() {
   162  	s2ds := make(map[hplot.StepsKind]*hbook.S2D)
   163  	steps := []hplot.StepsKind{hplot.PreSteps, hplot.MidSteps, hplot.PostSteps}
   164  	names := []string{"pre-steps", "mid-steps", "post-steps"}
   165  
   166  	for i, step := range steps {
   167  		pts := []hbook.Point2D{
   168  			{X: 1 + float64(i)*10, Y: 1},
   169  			{X: 2 + float64(i)*10, Y: 2},
   170  			{X: 3 + float64(i)*10, Y: 3},
   171  			{X: 4 + float64(i)*10, Y: 4},
   172  		}
   173  		s2ds[step] = hbook.NewS2D(pts...)
   174  	}
   175  
   176  	p := hplot.New()
   177  	p.Title.Text = "Scatter-2D (with steps)"
   178  	p.X.Label.Text = "X"
   179  	p.X.Min = 0
   180  	p.X.Max = 25
   181  	p.Y.Label.Text = "Y"
   182  	p.Y.Min = 0
   183  	p.Add(plotter.NewGrid())
   184  
   185  	for i, step := range steps {
   186  		s := hplot.NewS2D(s2ds[step], hplot.WithStepsKind(step))
   187  		s.GlyphStyle.Shape = draw.CircleGlyph{}
   188  		s.GlyphStyle.Color = plotutil.Color(i + 1)
   189  		s.GlyphStyle.Radius = vg.Points(2)
   190  		s.LineStyle.Width = 1
   191  		s.LineStyle.Color = plotutil.Color(i + 1)
   192  
   193  		p.Add(s)
   194  		p.Legend.Add(names[i], s)
   195  	}
   196  
   197  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/s2d_premidpost_steps.png")
   198  	if err != nil {
   199  		log.Fatal(err)
   200  	}
   201  }
   202  
   203  // ExampleS2D_withSteps_withBand draws some scatter points
   204  // with their error bars, using a step-like style together with a band
   205  func ExampleS2D_withStepsKind_withBand() {
   206  	pts := []hbook.Point2D{
   207  		{X: 1, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 1, ErrY: hbook.Range{Min: 2, Max: 3}},
   208  		{X: 2, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 5, ErrY: hbook.Range{Min: 5, Max: 2}},
   209  		{X: 3, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 10, ErrY: hbook.Range{Min: 2, Max: 2}},
   210  		{X: 4, ErrX: hbook.Range{Min: 0.5, Max: 0.5}, Y: 15, ErrY: hbook.Range{Min: 1.2, Max: 2}},
   211  	}
   212  	s2d := hbook.NewS2D(pts...)
   213  
   214  	p := hplot.New()
   215  	p.Title.Text = "Scatter-2D (with steps and band)"
   216  	p.X.Label.Text = "X"
   217  	p.Y.Label.Text = "Y"
   218  	p.Add(plotter.NewGrid())
   219  
   220  	s := hplot.NewS2D(s2d, hplot.WithStepsKind(hplot.HiSteps), hplot.WithYErrBars(true), hplot.WithBand(true))
   221  	s.GlyphStyle.Color = color.Black
   222  	s.GlyphStyle.Radius = vg.Points(4)
   223  	s.LineStyle.Width = 1
   224  	s.LineStyle.Dashes = plotutil.Dashes(2)
   225  
   226  	p.Add(s)
   227  
   228  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/s2d_steps_band.png")
   229  	if err != nil {
   230  		log.Fatal(err)
   231  	}
   232  }