Skip to content

Commit 2cc0a97

Browse files
committed
fix: fill out code for final two examples
1 parent ac51e24 commit 2cc0a97

2 files changed

Lines changed: 99 additions & 45 deletions

File tree

1-PyHEP-topical.ipynb

Lines changed: 89 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"metadata": {},
2222
"outputs": [],
2323
"source": [
24+
"import matplotlib.pyplot as plt\n",
25+
"import numpy as np\n",
2426
"import hist\n",
2527
"from hist import Hist"
2628
]
@@ -36,13 +38,13 @@
3638
]
3739
},
3840
{
39-
"cell_type": "code",
40-
"execution_count": null,
41-
"id": "ce94c89a-d0cd-4f45-9b8b-b2ef3b23962a",
42-
"metadata": {},
43-
"outputs": [],
41+
"cell_type": "markdown",
42+
"id": "00bdb721-5f66-4128-a070-f92850572e88",
43+
"metadata": {
44+
"tags": []
45+
},
4446
"source": [
45-
"import numpy as np"
47+
"## Introduction"
4648
]
4749
},
4850
{
@@ -413,8 +415,7 @@
413415
"import PIL.Image\n",
414416
"import PIL.ImageDraw\n",
415417
"import PIL.ImageFilter\n",
416-
"import PIL.ImageFont\n",
417-
"import matplotlib.pyplot as plt"
418+
"import PIL.ImageFont"
418419
]
419420
},
420421
{
@@ -429,7 +430,10 @@
429430
"draw = PIL.ImageDraw.Draw(image)\n",
430431
"\n",
431432
"# use a truetype font\n",
432-
"font = PIL.ImageFont.truetype(\"DejaVuSans-Bold.ttf\", 90)\n",
433+
"try:\n",
434+
" font = PIL.ImageFont.truetype(\"DejaVuSans-Bold.ttf\", 90)\n",
435+
"except OSError:\n",
436+
" font = PIL.ImageFont.truetype(\"Arial Bold.ttf\", 90)\n",
433437
"\n",
434438
"draw.text((30, 0), \"PyHEP\", font=font)"
435439
]
@@ -515,30 +519,47 @@
515519
"source": [
516520
"`hist` supports Stacked Histograms! A histogram stack holds multiple 1-D histograms into a stack, whose axes are required to match.\n",
517521
"\n",
518-
"Manipulation and mutliple math operations can also be performed on stacks. You can find more about them in the [Hist - Stack documentation](https://hist.readthedocs.io/en/latest/user-guide/notebooks/Stack.html)."
522+
"Manipulation and multiple math operations can also be performed on stacks. You can find more about them in the [Hist - Stack documentation](https://hist.readthedocs.io/en/latest/user-guide/notebooks/Stack.html)."
519523
]
520524
},
521525
{
522-
"cell_type": "markdown",
523-
"id": "d52dcb66-060f-4529-976e-c6d5be99cff8",
524-
"metadata": {
525-
"jp-MarkdownHeadingCollapsed": true,
526-
"tags": []
527-
},
526+
"cell_type": "code",
527+
"execution_count": null,
528+
"id": "d990e620-6955-4328-8459-01f4efe6984f",
529+
"metadata": {},
530+
"outputs": [],
528531
"source": [
529-
"<div>\n",
530-
"<img src=\"https://hist.readthedocs.io/en/latest/_images/user-guide_notebooks_Stack_4_0.png\" width=\"360\"/>\n",
531-
"</div>"
532+
"ax = hist.axis.Regular(25, -5, 5, flow=False, name=\"x\")\n",
533+
"cax = hist.axis.StrCategory([\"signal\", \"upper\", \"lower\"], name=\"c\")\n",
534+
"full_hist = Hist(ax, cax)\n",
535+
"\n",
536+
"full_hist.fill(x=rng.standard_normal(size=600), c=\"signal\")\n",
537+
"full_hist.fill(x=2 * rng.standard_normal(size=500) + 2, c=\"upper\")\n",
538+
"full_hist.fill(x=2 * rng.standard_normal(size=500) - 2, c=\"lower\")\n",
539+
"\n",
540+
"s = full_hist.stack(\"c\")"
532541
]
533542
},
534543
{
535-
"cell_type": "markdown",
536-
"id": "239c82ac-6e24-49dd-be26-6c999f0af83e",
544+
"cell_type": "code",
545+
"execution_count": null,
546+
"id": "b1848cee-352a-4d12-a12e-913499bbe54a",
547+
"metadata": {},
548+
"outputs": [],
549+
"source": [
550+
"s.plot()\n",
551+
"plt.legend();"
552+
]
553+
},
554+
{
555+
"cell_type": "code",
556+
"execution_count": null,
557+
"id": "d6e8eb97-dc31-4336-bc96-a2779c069661",
537558
"metadata": {},
559+
"outputs": [],
538560
"source": [
539-
"<div>\n",
540-
"<img src=\"https://hist.readthedocs.io/en/latest/_images/user-guide_notebooks_Stack_6_0.png\" width=\"360\"/>\n",
541-
"</div>"
561+
"s[::-1].plot(stack=True, histtype=\"fill\")\n",
562+
"plt.legend();"
542563
]
543564
},
544565
{
@@ -558,13 +579,52 @@
558579
]
559580
},
560581
{
561-
"cell_type": "markdown",
562-
"id": "a4a89cdf-2b41-484d-9527-9543f926009c",
582+
"cell_type": "code",
583+
"execution_count": null,
584+
"id": "f480f23f-65ab-47da-93cd-2c26ee02a537",
585+
"metadata": {},
586+
"outputs": [],
587+
"source": [
588+
"from scipy import interpolate"
589+
]
590+
},
591+
{
592+
"cell_type": "code",
593+
"execution_count": null,
594+
"id": "0e13d997-871e-495a-8a09-d386f4365a32",
595+
"metadata": {},
596+
"outputs": [],
597+
"source": [
598+
"# We obtain evenly spaced numbers over the specified interval.\n",
599+
"x = np.linspace(-27, 27, num=250, endpoint=True)\n",
600+
"\n",
601+
"# Define a Hist object and fill it.\n",
602+
"h = Hist.new.Reg(10, -30, 30).Double()\n",
603+
"centers = h.axes[0].centers\n",
604+
"weights = np.cos(-(centers**2) / 9.0) ** 2\n",
605+
"h.fill(centers, weight=weights)"
606+
]
607+
},
608+
{
609+
"cell_type": "code",
610+
"execution_count": null,
611+
"id": "c88ad823-b3c4-4a66-b8e2-ce9bf8c81e7a",
563612
"metadata": {},
613+
"outputs": [],
614+
"source": [
615+
"linear_interp = interpolate.interp1d(h.axes[0].centers, h.values(), kind=\"linear\")"
616+
]
617+
},
618+
{
619+
"cell_type": "code",
620+
"execution_count": null,
621+
"id": "20fe51c2-edbe-449e-a20a-d0f65bcafb72",
622+
"metadata": {},
623+
"outputs": [],
564624
"source": [
565-
"<div>\n",
566-
"<img src=\"https://hist.readthedocs.io/en/latest/_images/user-guide_notebooks_Interpolation_10_0.png\" width=\"360\"/>\n",
567-
"</div>"
625+
"h.plot() # Plot the histogram\n",
626+
"plt.plot(h.axes[0].centers, h.values(), \"o\")\n",
627+
"plt.plot(x, linear_interp(x), \"-.\");"
568628
]
569629
}
570630
],

files/SimpleExample.ipynb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"import micropip\n",
10-
"\n",
11-
"await micropip.install(\"hist\")\n",
12-
"await micropip.install(\"mplhep<0.3.0\")"
9+
"%matplotlib inline"
1310
]
1411
},
1512
{
@@ -18,7 +15,10 @@
1815
"metadata": {},
1916
"outputs": [],
2017
"source": [
21-
"import hist"
18+
"import micropip\n",
19+
"\n",
20+
"await micropip.install(\"hist\")\n",
21+
"await micropip.install(\"mplhep<0.3.0\")"
2222
]
2323
},
2424
{
@@ -27,7 +27,8 @@
2727
"metadata": {},
2828
"outputs": [],
2929
"source": [
30-
"h = hist.Hist.new.Reg(10, 0, 1).Double()"
30+
"import hist\n",
31+
"import numpy as np"
3132
]
3233
},
3334
{
@@ -36,7 +37,7 @@
3637
"metadata": {},
3738
"outputs": [],
3839
"source": [
39-
"import numpy as np"
40+
"rng = np.random.default_rng()"
4041
]
4142
},
4243
{
@@ -45,7 +46,7 @@
4546
"metadata": {},
4647
"outputs": [],
4748
"source": [
48-
"h.fill(np.random.normal(0.5, 0.3, size=1_000_000))"
49+
"h = hist.Hist.new.Reg(10, 0, 1).Double()"
4950
]
5051
},
5152
{
@@ -54,7 +55,7 @@
5455
"metadata": {},
5556
"outputs": [],
5657
"source": [
57-
"%matplotlib inline"
58+
"h.fill(rng.normal(0.5, 0.3, size=1_000_000))"
5859
]
5960
},
6061
{
@@ -65,13 +66,6 @@
6566
"source": [
6667
"h.plot()"
6768
]
68-
},
69-
{
70-
"cell_type": "code",
71-
"execution_count": null,
72-
"metadata": {},
73-
"outputs": [],
74-
"source": []
7569
}
7670
],
7771
"metadata": {

0 commit comments

Comments
 (0)