An example of making a histogram.
()
| 17 | |
| 18 | // An example of making a histogram. |
| 19 | func ExampleHistogram() { |
| 20 | rnd := rand.New(rand.NewPCG(1, 1)) |
| 21 | |
| 22 | // stdNorm returns the probability of drawing a |
| 23 | // value from a standard normal distribution. |
| 24 | stdNorm := func(x float64) float64 { |
| 25 | const sigma = 1.0 |
| 26 | const mu = 0.0 |
| 27 | const root2π = 2.50662827459517818309 |
| 28 | return 1.0 / (sigma * root2π) * math.Exp(-((x-mu)*(x-mu))/(2*sigma*sigma)) |
| 29 | } |
| 30 | |
| 31 | n := 10000 |
| 32 | vals := make(plotter.Values, n) |
| 33 | for i := range n { |
| 34 | vals[i] = rnd.NormFloat64() |
| 35 | } |
| 36 | |
| 37 | p := plot.New() |
| 38 | p.Title.Text = "Histogram" |
| 39 | h, err := plotter.NewHist(vals, 16) |
| 40 | if err != nil { |
| 41 | log.Panic(err) |
| 42 | } |
| 43 | h.Normalize(1) |
| 44 | p.Add(h) |
| 45 | |
| 46 | // The normal distribution function |
| 47 | norm := plotter.NewFunction(stdNorm) |
| 48 | norm.Color = color.RGBA{R: 255, A: 255} |
| 49 | norm.Width = vg.Points(2) |
| 50 | p.Add(norm) |
| 51 | |
| 52 | err = p.Save(200, 200, "testdata/histogram.png") |
| 53 | if err != nil { |
| 54 | log.Panic(err) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func ExampleHistogram_logScaleY() { |
| 59 | p := plot.New() |