mplhep icon indicating copy to clipboard operation
mplhep copied to clipboard

[Bug] histplot raises IndexError when plotting an histogram with one bin

Open tcuisset opened this issue 2 years ago • 2 comments

Hi, histplot crashes when plotting histograms with a single bin, when trying to draw overflow bins.

Reproducer :

import hist
import mplhep as hep

h = hist.Hist(hist.axis.Regular(1, 0, 1))
h.fill([-1, 0.5])
hep.histplot(h)

Output

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[17], line 1
----> 1 hep.histplot(h)

File [***/lib/python3.10/site-packages/mplhep/plot.py:532], in histplot(H, bins, yerr, w2, w2method, stack, density, binwnorm, histtype, xerr, label, sort, edges, binticks, ax, flow, **kwargs)
    528 if underflow > 0.0:
    529     if flow == "hint":
    530         ax.plot(
    531             [
--> 532                 final_bins[0] - (final_bins[-3] - final_bins[2]) * 0.03,
    533                 final_bins[0],
    534             ],
    535             [0, 0],
    536             **kwargs,
    537         )
    538     if flow == "show":
    539         ax.plot(
    540             [flow_bins[1], flow_bins[2]],
    541             [0, 0],
    542             **kwargs,
    543         )

IndexError: index -3 is out of bounds for axis 0 with size 2

tcuisset avatar Jun 12 '23 15:06 tcuisset

Thanks for the report @tcuisset, @Ming-Yan could you take a look?

andrzejnovak avatar Jun 13 '23 11:06 andrzejnovak

Hi @andrzejnovak @tcuisset ,thanks for pointing this out, I open a new PR to solve this issue for both 1d/2d hist

Ming-Yan avatar Jun 13 '23 17:06 Ming-Yan

Fixed in https://github.com/scikit-hep/mplhep/commit/7894d19d664b148618ee66b1ecea3541a56159b4

andrzejnovak avatar Oct 15 '25 08:10 andrzejnovak