{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import (\n", " \"log\"\n", " \"image/color\"\n", " \n", " \"go-hep.org/x/hep/hbook\"\n", " \"golang.org/x/exp/rand\"\n", " \"gonum.org/v1/gonum/stat/distuv\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "npoints := 10000" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Create a normal distribution.\n", "dist := distuv.Normal{\n", " Mu: 0,\n", " Sigma: 1,\n", " Src: rand.New(rand.NewSource(0)),\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Draw some random values from the standard\n", "// normal distribution.\n", "hist := hbook.NewH1D(100, -4, +4)\n", "for i := 0; i < npoints; i++ {\n", " v := dist.Rand()\n", " hist.Fill(v, 1)\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// normalize histogram\n", "area := 0.0\n", "for _, bin := range hist.Binning().Bins() {\n", " area += bin.SumW() * bin.XWidth()\n", "}\n", "hist.Scale(1 / area)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "printf(\"entries: %v\\n\", hist.Entries())\n", "printf(\"x-mean: %v\\n\", hist.XMean())\n", "printf(\"x-stddev: %v\\n\", hist.XStdDev())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import (\n", " \"go-hep.org/x/hep/hplot\"\n", " \"gonum.org/v1/plot/vg\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Make a plot and set its title.\n", "p := hplot.New()\n", "p.Title.Text = \"Histogram\"\n", "p.X.Label.Text = \"X\"\n", "p.Y.Label.Text = \"Y\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Create a histogram of our values drawn\n", "// from the standard normal.\n", "h := hplot.NewH1D(hist)\n", "h.Infos.Style = hplot.HInfoSummary\n", "p.Add(h)\n", "\n", "// The normal distribution function\n", "norm := hplot.NewFunction(dist.Prob)\n", "norm.Color = color.RGBA{R: 0xff, A: 255}\n", "norm.Width = vg.Length(2)\n", "p.Add(norm)\n", "\n", "// draw a grid\n", "p.Add(hplot.NewGrid())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hplot.Show(p, 640, -1, \"png\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Draw some random values from a shifted standard normal\n", "// normal distribution.\n", "h1 := hbook.NewH1D(100, -5, +5)\n", "h2 := hbook.NewH1D(100, -5, +5)\n", "for i := 0; i < npoints; i++ {\n", " h1.Fill(rand.NormFloat64()-1, 1.0)\n", " h2.Fill(rand.NormFloat64()+1, 1.3)\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = hplot.New()\n", "p.Title.Text = \"Histos + color blending\"\n", "p.X.Label.Text = \"x\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hh1 := hplot.NewH1D(h1)\n", "hh1.FillColor = color.NRGBA{R:0xff, A:128}\n", "hh1.Color = color.Black" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hh2 := hplot.NewH1D(h2)\n", "hh2.FillColor = color.NRGBA{B:0xff, A:128}\n", "hh2.Color = color.Black" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p.Add(hh1, hh2, hplot.NewGrid())\n", "hplot.Show(p, 640, -1, \"png\")" ] } ], "metadata": { "kernelspec": { "display_name": "Go", "language": "go", "name": "go" }, "language_info": { "codemirror_mode": "go", "file_extension": ".go", "mimetype": "", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "go", "version": "unreleased" } }, "nbformat": 4, "nbformat_minor": 2 }