Skip to content

Commit 5b261dc

Browse files
feat: add ratio plot support through .plot_ratio API (#161)
* feat: Add plot_ratio API * Add .plot_ratio to BastHist API - Adds ratio plot support for other Hists and callables * Factor out plot_ratio and plot_pull to perform the final subplot plotting - Majority of logic is factored out of plot_pull into _plot_ratiolike - _plot_ratiolike then calls plot_ratio or plot_pull as needed * Add test images for plot_ratio * Add plotting tests for plot_ratio * Update README and Changelog to reflect addition of plot_ratio * Add NameTuples for typing * force return type to be np.ndarray * type before to deal with control flow * Fixups thanks to Henry's advice * Ensure default uncertainty band color visible * bump location of __all__ to just below imports * Remove quotes * Rename _hist vars to either __hist or histogram * Add test for str alias for plot_ratio and plot_pull * Add str to types for plot_ratiolike * use TypeVar to type hist.BaseHist * Rename plot_x -> plot_x_array for clarity * Use quoted types over TypeVar Co-authored-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent 958037f commit 5b261dc

File tree

8 files changed

+515
-135
lines changed

8 files changed

+515
-135
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Hist currently provides everything boost-histogram provides, and the following e
5858
- Quick plotting routines encourage exploration:
5959
- `.plot()` provides 1D and 2D plots
6060
- `.plot2d_full()` shows 1D projects around a 2D plot
61+
- `.plot_ratio(...)` make a ratio plot between the histogram and another histogram or callable
6162
- `.plot_pull(...)` performs a pull plot
6263
- `.plot_pie()` makes a pie plot
6364
- `.show()` provides a nice str printout using Histoprint

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog
44
IN PROGRESS
55
--------------------
66

7+
* Add ``plot_ratio`` to the public API, which allows for making ratio plots between the
8+
histogram and either another histogram or a callable.
9+
`#161 <https://github.com/scikit-hep/hist/pull/161>`_
10+
711
* Add frequentist coverage interval support in the ``intervals`` module.
812
`#176 <https://github.com/scikit-hep/hist/pull/176>`_
913

src/hist/basehist.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import matplotlib.axes
3636
from mplhep.plot import Hist1DArtists, Hist2DArtists
3737

38+
from .plot import FitResultArtists, MainAxisArtists, RatiolikeArtists
39+
3840
InnerIndexing = Union[
3941
SupportsIndex, str, Callable[[bh.axis.Axis], int], slice, "ellipsis"
4042
]
@@ -401,20 +403,45 @@ def plot2d_full(
401403

402404
return hist.plot.plot2d_full(self, ax_dict=ax_dict, **kwargs)
403405

406+
def plot_ratio(
407+
self,
408+
other: Union["hist.BaseHist", Callable[[np.ndarray], np.ndarray], str],
409+
*,
410+
ax_dict: "Optional[Dict[str, matplotlib.axes.Axes]]" = None,
411+
**kwargs: Any,
412+
) -> "Tuple[MainAxisArtists, RatiolikeArtists]":
413+
"""
414+
``plot_ratio`` method for ``BaseHist`` object.
415+
416+
Return a tuple of artists following a structure of
417+
``(main_ax_artists, subplot_ax_artists)``
418+
"""
419+
420+
import hist.plot
421+
422+
return hist.plot._plot_ratiolike(
423+
self, other, ax_dict=ax_dict, view="ratio", **kwargs
424+
)
425+
404426
def plot_pull(
405427
self,
406-
func: Callable[[np.ndarray], np.ndarray],
428+
func: Union[Callable[[np.ndarray], np.ndarray], str],
407429
*,
408430
ax_dict: "Optional[Dict[str, matplotlib.axes.Axes]]" = None,
409431
**kwargs: Any,
410-
) -> "Tuple[matplotlib.axes.Axes, matplotlib.axes.Axes]":
432+
) -> "Tuple[FitResultArtists, RatiolikeArtists]":
411433
"""
412-
Plot_pull method for BaseHist object.
434+
``plot_pull`` method for ``BaseHist`` object.
435+
436+
Return a tuple of artists following a structure of
437+
``(main_ax_artists, subplot_ax_artists)``
413438
"""
414439

415440
import hist.plot
416441

417-
return hist.plot.plot_pull(self, func, ax_dict=ax_dict, **kwargs)
442+
return hist.plot._plot_ratiolike(
443+
self, func, ax_dict=ax_dict, view="pull", **kwargs
444+
)
418445

419446
def plot_pie(
420447
self,

0 commit comments

Comments
 (0)