-
-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathhep_hplot.go
61 lines (51 loc) · 1.51 KB
/
hep_hplot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"image/color"
"log"
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hplot"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
"gonum.org/v1/plot/vg"
)
func main() {
// define amount of datapoints
const datapoints = 1024
// Create a normal distribution
distribution := distuv.Normal{
Mu: 0,
Sigma: 2,
Src: rand.New(rand.NewSource(0)),
}
// Draw some random values from the standard
// normal distribution into a 1 dimensional histogram
histogram := hbook.NewH1D(40, -4, +4)
for i := 0; i < datapoints; i++ {
v := distribution.Rand()
histogram.Fill(v, 1)
}
// normalize histogram
area := 0.0
for _, bin := range histogram.Binning.Bins {
area += bin.SumW() * bin.XWidth()
}
histogram.Scale(1 / area)
// Make a new plot and set its title text and axis description
plot := hplot.New()
plot.Title.Text = "Histogram"
plot.X.Label.Text = "X"
plot.Y.Label.Text = "Y"
// Create a new histogram and add previously defined values and texts
h := hplot.NewH1D(histogram, hplot.WithHInfo(hplot.HInfoSummary), hplot.WithYErrBars(true), hplot.WithBand(true))
h.YErrs.LineStyle.Color = color.RGBA{R: 255, G: 0, B: 0, A: 255}
plot.Add(h)
// Add a red line with the normal distribution function
norm := hplot.NewFunction(distribution.Prob)
norm.Color = color.RGBA{R: 255, A: 255}
norm.Width = vg.Points(2)
plot.Add(norm)
// Save the plot as PNG
if err := plot.Save(20*vg.Centimeter, -1, "hep-hplot-output.png"); err != nil {
log.Fatalf("error saving plot: %v\n", err)
}
}