|
21 | 21 | "metadata": {}, |
22 | 22 | "outputs": [], |
23 | 23 | "source": [ |
| 24 | + "import matplotlib.pyplot as plt\n", |
| 25 | + "import numpy as np\n", |
24 | 26 | "import hist\n", |
25 | 27 | "from hist import Hist" |
26 | 28 | ] |
|
36 | 38 | ] |
37 | 39 | }, |
38 | 40 | { |
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 | + }, |
44 | 46 | "source": [ |
45 | | - "import numpy as np" |
| 47 | + "## Introduction" |
46 | 48 | ] |
47 | 49 | }, |
48 | 50 | { |
|
413 | 415 | "import PIL.Image\n", |
414 | 416 | "import PIL.ImageDraw\n", |
415 | 417 | "import PIL.ImageFilter\n", |
416 | | - "import PIL.ImageFont\n", |
417 | | - "import matplotlib.pyplot as plt" |
| 418 | + "import PIL.ImageFont" |
418 | 419 | ] |
419 | 420 | }, |
420 | 421 | { |
|
429 | 430 | "draw = PIL.ImageDraw.Draw(image)\n", |
430 | 431 | "\n", |
431 | 432 | "# 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", |
433 | 437 | "\n", |
434 | 438 | "draw.text((30, 0), \"PyHEP\", font=font)" |
435 | 439 | ] |
|
515 | 519 | "source": [ |
516 | 520 | "`hist` supports Stacked Histograms! A histogram stack holds multiple 1-D histograms into a stack, whose axes are required to match.\n", |
517 | 521 | "\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)." |
519 | 523 | ] |
520 | 524 | }, |
521 | 525 | { |
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": [], |
528 | 531 | "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\")" |
532 | 541 | ] |
533 | 542 | }, |
534 | 543 | { |
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", |
537 | 558 | "metadata": {}, |
| 559 | + "outputs": [], |
538 | 560 | "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();" |
542 | 563 | ] |
543 | 564 | }, |
544 | 565 | { |
|
558 | 579 | ] |
559 | 580 | }, |
560 | 581 | { |
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", |
563 | 612 | "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": [], |
564 | 624 | "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), \"-.\");" |
568 | 628 | ] |
569 | 629 | } |
570 | 630 | ], |
|
0 commit comments