scikit-hep / mplhep

Extended histogram plotting on top of matplotlib and HEP collaboration compatible styling
https://mplhep.readthedocs.io
MIT License
185 stars 64 forks source link

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

Open tcuisset opened 1 year ago

tcuisset commented 1 year ago

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
andrzejnovak commented 1 year ago

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

Ming-Yan commented 1 year ago

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