In [ ]:
import (
    "log"
    "image/color"
    
    "go-hep.org/x/hep/hbook"
    "golang.org/x/exp/rand"
    "gonum.org/v1/gonum/stat/distuv"
)
In [ ]:
npoints := 10000
In [ ]:
// Create a normal distribution.
dist := distuv.Normal{
    Mu:    0,
    Sigma: 1,
    Src:   rand.New(rand.NewSource(0)),
}
In [ ]:
// Draw some random values from the standard
// normal distribution.
hist := hbook.NewH1D(100, -4, +4)
for i := 0; i < npoints; i++ {
    v := dist.Rand()
    hist.Fill(v, 1)
}
In [ ]:
// normalize histogram
area := 0.0
for _, bin := range hist.Binning().Bins() {
    area += bin.SumW() * bin.XWidth()
}
hist.Scale(1 / area)
In [ ]:
printf("entries:  %v\n", hist.Entries())
printf("x-mean:   %v\n", hist.XMean())
printf("x-stddev: %v\n", hist.XStdDev())
In [ ]:
import (
    "go-hep.org/x/hep/hplot"
    "gonum.org/v1/plot/vg"
)
In [ ]:
// Make a plot and set its title.
p := hplot.New()
p.Title.Text = "Histogram"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
In [ ]:
// Create a histogram of our values drawn
// from the standard normal.
h := hplot.NewH1D(hist)
h.Infos.Style = hplot.HInfoSummary
p.Add(h)

// The normal distribution function
norm := hplot.NewFunction(dist.Prob)
norm.Color = color.RGBA{R: 0xff, A: 255}
norm.Width = vg.Length(2)
p.Add(norm)

// draw a grid
p.Add(hplot.NewGrid())
In [ ]:
hplot.Show(p, 640, -1, "png")
In [ ]:
// Draw some random values from a shifted standard normal
// normal distribution.
h1 := hbook.NewH1D(100, -5, +5)
h2 := hbook.NewH1D(100, -5, +5)
for i := 0; i < npoints; i++ {
    h1.Fill(rand.NormFloat64()-1, 1.0)
    h2.Fill(rand.NormFloat64()+1, 1.3)
}
In [ ]:
p = hplot.New()
p.Title.Text = "Histos + color blending"
p.X.Label.Text = "x"
In [ ]:
hh1 := hplot.NewH1D(h1)
hh1.FillColor = color.NRGBA{R:0xff, A:128}
hh1.Color = color.Black
In [ ]:
hh2 := hplot.NewH1D(h2)
hh2.FillColor = color.NRGBA{B:0xff, A:128}
hh2.Color = color.Black
In [ ]:
p.Add(hh1, hh2, hplot.NewGrid())
hplot.Show(p, 640, -1, "png")