From 53fc0151e2add25dd3f34e4b68501a54e57bccb5 Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 13:42:35 +0900 Subject: [PATCH 1/8] add example showing how to plot categorical data --- docs/basics/1d_hist.rst | 29 + docs/examples/1d_hist/1d_int_category.py | 40 ++ docs/examples/1d_hist/1d_str_category.py | 40 ++ docs/img/1d_int_category.svg | 773 ++++++++++++++++++++++ docs/img/1d_str_category.svg | 799 +++++++++++++++++++++++ 5 files changed, 1681 insertions(+) create mode 100644 docs/examples/1d_hist/1d_int_category.py create mode 100644 docs/examples/1d_hist/1d_str_category.py create mode 100644 docs/img/1d_int_category.svg create mode 100644 docs/img/1d_str_category.svg diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index 0a4bb1f5..1a89f824 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -258,3 +258,32 @@ Note that most functions in ``plothist`` work only with counting histograms and .. image:: ../img/1d_profile.svg :alt: Profile plot :width: 500 + +Histogram with category axis +============================ + +The function :func:`make_hist() ` returns a a `boost_histogram.Histogram `_ with a regular or a variable axis, but the ``boost_histogram`` package also supports categorical axes. + +The examples below show how to create a histogram with a category axis in ``boost_histogram`` and plot it with ``plothist``. + +Integer category +---------------- + +.. literalinclude:: ../examples/1d_hist/1d_int_category.py + :language: python + :start-after: ### + +.. image:: ../img/1d_int_category.svg + :alt: Profile plot + :width: 500 + +String category +--------------- + +.. literalinclude:: ../examples/1d_hist/1d_str_category.py + :language: python + :start-after: ### + +.. image:: ../img/1d_str_category.svg + :alt: Profile plot + :width: 500 diff --git a/docs/examples/1d_hist/1d_int_category.py b/docs/examples/1d_hist/1d_int_category.py new file mode 100644 index 00000000..ce82eb72 --- /dev/null +++ b/docs/examples/1d_hist/1d_int_category.py @@ -0,0 +1,40 @@ +""" +1d integer category +=================== + +Plot a 1d histogram with integer categories. + +""" + +### +import boost_histogram as bh +from plothist import plot_hist +import matplotlib.pyplot as plt + +# 3 integer categories +int_categories = [-10, 5, 72] + +# Integer category axis with 3 bins +axis = bh.axis.IntCategory(categories=int_categories) + +# 6 data points, +data = [-10, -10, 5, 72, 72, 72] + +# Create and fill the histogram +h = bh.Histogram(axis, storage=bh.storage.Weight()) +h.fill(data) + +# Plot the histogram +fig, ax = plt.subplots() + +plot_hist(h, ax=ax) + +ax.set_xticks([i + 0.5 for i in range(len(int_categories))]) +ax.set_xticklabels(int_categories) +ax.minorticks_off() + +ax.set_xlabel("Integer Category") +ax.set_ylabel("Entries") +ax.set_xlim(0, len(int_categories)) + +fig.savefig("1d_int_category.svg", bbox_inches="tight") diff --git a/docs/examples/1d_hist/1d_str_category.py b/docs/examples/1d_hist/1d_str_category.py new file mode 100644 index 00000000..b94439ad --- /dev/null +++ b/docs/examples/1d_hist/1d_str_category.py @@ -0,0 +1,40 @@ +""" +1d string category +================== + +Plot a 1d histogram with string categories. + +""" + +### +import boost_histogram as bh +from plothist import plot_hist +import matplotlib.pyplot as plt + +# 3 str categories +str_categories = ["a", "b", "c"] + +# String category axis with 3 bins +axis = bh.axis.StrCategory(categories=str_categories) + +# 6 data points, +data = ["a", "a", "a", "b", "b", "c"] + +# Create and fill the histogram +h = bh.Histogram(axis, storage=bh.storage.Weight()) +h.fill(data) + +# Plot the histogram +fig, ax = plt.subplots() + +plot_hist(h, ax=ax) + +ax.set_xticks([i + 0.5 for i in range(len(str_categories))]) +ax.set_xticklabels(str_categories) +ax.minorticks_off() + +ax.set_xlabel("String Category") +ax.set_ylabel("Entries") +ax.set_xlim(0, len(str_categories)) + +fig.savefig("1d_str_category.svg", bbox_inches="tight") diff --git a/docs/img/1d_int_category.svg b/docs/img/1d_int_category.svg new file mode 100644 index 00000000..6464b2b4 --- /dev/null +++ b/docs/img/1d_int_category.svg @@ -0,0 +1,773 @@ + + + + + + + + + カリビアンカフェ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/img/1d_str_category.svg b/docs/img/1d_str_category.svg new file mode 100644 index 00000000..b3d0c8c7 --- /dev/null +++ b/docs/img/1d_str_category.svg @@ -0,0 +1,799 @@ + + + + + + + + + カリビアンカフェ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 03c40bab5344afc2894d44fa3754a2d7a954ba7f Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 13:52:57 +0900 Subject: [PATCH 2/8] fix figure label --- docs/basics/1d_hist.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index 1a89f824..54c8c665 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -274,7 +274,7 @@ Integer category :start-after: ### .. image:: ../img/1d_int_category.svg - :alt: Profile plot + :alt: Integer category plot :width: 500 String category @@ -285,5 +285,5 @@ String category :start-after: ### .. image:: ../img/1d_str_category.svg - :alt: Profile plot + :alt: String category plot :width: 500 From 25a4eeb627adbae7526c872fd5ffe638ca8d27ac Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 13:58:17 +0900 Subject: [PATCH 3/8] add comment --- docs/examples/1d_hist/1d_int_category.py | 1 + docs/examples/1d_hist/1d_str_category.py | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/examples/1d_hist/1d_int_category.py b/docs/examples/1d_hist/1d_int_category.py index ce82eb72..478075b7 100644 --- a/docs/examples/1d_hist/1d_int_category.py +++ b/docs/examples/1d_hist/1d_int_category.py @@ -29,6 +29,7 @@ plot_hist(h, ax=ax) +# Set the x-ticks to the middle of the bins and label them ax.set_xticks([i + 0.5 for i in range(len(int_categories))]) ax.set_xticklabels(int_categories) ax.minorticks_off() diff --git a/docs/examples/1d_hist/1d_str_category.py b/docs/examples/1d_hist/1d_str_category.py index b94439ad..e427404a 100644 --- a/docs/examples/1d_hist/1d_str_category.py +++ b/docs/examples/1d_hist/1d_str_category.py @@ -29,6 +29,7 @@ plot_hist(h, ax=ax) +# Set the x-ticks to the middle of the bins and label them ax.set_xticks([i + 0.5 for i in range(len(str_categories))]) ax.set_xticklabels(str_categories) ax.minorticks_off() From 8d3e47e85bfe1f452d8198d60b9141d7099fe1ba Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 14:13:38 +0900 Subject: [PATCH 4/8] add note --- docs/basics/1d_hist.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index 54c8c665..1478583a 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -251,6 +251,7 @@ In this example, the data points and the error bars are the average value and th Note that most functions in ``plothist`` work only with counting histograms and will raise an error if you try to use them with a mean histogram. + .. literalinclude:: ../examples/1d_hist/1d_profile.py :language: python :start-after: ### @@ -266,6 +267,12 @@ The function :func:`make_hist() ` returns a a The examples below show how to create a histogram with a category axis in ``boost_histogram`` and plot it with ``plothist``. +.. note:: + When we create the histograms in the examples below, we use the argument ``storage=bh.storage.Weight()``. + This is because functions in ``plothist`` assume histograms with a storage supporting weighted data in which the variance of each bin is tracked. + When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the above examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it. + + Integer category ---------------- From cc1f254810fc0759709f792dfb2c3eefb7ab1de4 Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 14:30:56 +0900 Subject: [PATCH 5/8] add link to bh documentation --- docs/basics/1d_hist.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index 1478583a..d62043bd 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -269,7 +269,7 @@ The examples below show how to create a histogram with a category axis in ``boos .. note:: When we create the histograms in the examples below, we use the argument ``storage=bh.storage.Weight()``. - This is because functions in ``plothist`` assume histograms with a storage supporting weighted data in which the variance of each bin is tracked. + This is because functions in ``plothist`` assume histograms with a storage supporting weighted data in which the variance of each bin is tracked (more details `here `_). When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the above examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it. From d84cba8ed583bd3e7b331cb0c8da46cf582b233e Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 14:41:35 +0900 Subject: [PATCH 6/8] improve doc --- docs/basics/1d_hist.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index d62043bd..51b72ecc 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -269,8 +269,8 @@ The examples below show how to create a histogram with a category axis in ``boos .. note:: When we create the histograms in the examples below, we use the argument ``storage=bh.storage.Weight()``. - This is because functions in ``plothist`` assume histograms with a storage supporting weighted data in which the variance of each bin is tracked (more details `here `_). - When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the above examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it. + This is because the functions in ``plothist`` assume histograms with a storage that supports weighted data, where the variance of each bin is tracked (more details `here < https://boost-histogram.readthedocs.io/en/latest/user-guide/storage.html#weight>`_ and `there `_). + When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the above examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it. Integer category From 87c2d1288f54e07f6df6ebc9083949ff28ee19fe Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 14:43:21 +0900 Subject: [PATCH 7/8] move note in doc --- docs/basics/1d_hist.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index 51b72ecc..d728cea3 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -267,11 +267,6 @@ The function :func:`make_hist() ` returns a a The examples below show how to create a histogram with a category axis in ``boost_histogram`` and plot it with ``plothist``. -.. note:: - When we create the histograms in the examples below, we use the argument ``storage=bh.storage.Weight()``. - This is because the functions in ``plothist`` assume histograms with a storage that supports weighted data, where the variance of each bin is tracked (more details `here < https://boost-histogram.readthedocs.io/en/latest/user-guide/storage.html#weight>`_ and `there `_). - When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the above examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it. - Integer category ---------------- @@ -284,6 +279,12 @@ Integer category :alt: Integer category plot :width: 500 +.. note:: + When we create the histograms in this example, we use the argument ``storage=bh.storage.Weight()``. + This is because the functions in ``plothist`` assume histograms with a storage that supports weighted data, where the variance of each bin is tracked (more details `here < https://boost-histogram.readthedocs.io/en/latest/user-guide/storage.html#weight>`_ and `there `_). + When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the previous examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it. + + String category --------------- From f325a64eb74f4b77b0d6ce988a99f54c989fe6f2 Mon Sep 17 00:00:00 2001 From: cyrraz Date: Wed, 27 Mar 2024 14:54:48 +0900 Subject: [PATCH 8/8] fix url doc url --- docs/basics/1d_hist.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/basics/1d_hist.rst b/docs/basics/1d_hist.rst index d728cea3..501374ff 100644 --- a/docs/basics/1d_hist.rst +++ b/docs/basics/1d_hist.rst @@ -281,7 +281,7 @@ Integer category .. note:: When we create the histograms in this example, we use the argument ``storage=bh.storage.Weight()``. - This is because the functions in ``plothist`` assume histograms with a storage that supports weighted data, where the variance of each bin is tracked (more details `here < https://boost-histogram.readthedocs.io/en/latest/user-guide/storage.html#weight>`_ and `there `_). + This is because the functions in ``plothist`` assume histograms with a storage that supports weighted data, where the variance of each bin is tracked (more details `here `_ and `there `_). When creating histograms with regular or variable axes with the function :func:`make_hist() `, as in most of the previous examples, the storage is automatically set to ``bh.storage.Weight()``, so the user does not have to worry about it.