Skip to content

Commit 2aa455e

Browse files
authored
fix : flow plot bug on axis range for histplot and hist2dplot (#432)
* fix : flow plot bug on axis range for histplot and hist2dplot * feat : add test for one bin hist with flow bins #431
1 parent bd90705 commit 2aa455e

8 files changed

+35
-13
lines changed

src/mplhep/plot.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ def histplot(
171171
# Process input
172172
hists = list(process_histogram_parts(H, bins))
173173
final_bins, xtick_labels = get_plottable_protocol_bins(hists[0].axes[0])
174+
_bin_widths = np.diff(final_bins)
175+
_bin_centers = final_bins[1:] - _bin_widths / float(2)
174176
assert final_bins.ndim == 1, "bins need to be 1 dimensional"
175177
_x_axes_label = ax.get_xlabel()
176178
x_axes_label = (
@@ -237,8 +239,8 @@ def histplot(
237239
final_bins,
238240
0,
239241
[
240-
final_bins[0] - (final_bins[-1] - final_bins[0]) * 0.08,
241-
final_bins[0] - (final_bins[-1] - final_bins[0]) * 0.03,
242+
final_bins[0] - _bin_widths[0] * len(_bin_widths) * 0.08,
243+
final_bins[0] - _bin_widths[0] * len(_bin_widths) * 0.03,
242244
],
243245
)
244246
value, variance = np.insert(value, 0, np.nan), np.insert(
@@ -252,8 +254,8 @@ def histplot(
252254
flow_bins = np.append(
253255
flow_bins,
254256
[
255-
final_bins[-1] + (final_bins[-1] - final_bins[0]) * 0.03,
256-
final_bins[-1] + (final_bins[-1] - final_bins[0]) * 0.08,
257+
final_bins[-1] + _bin_widths[-1] * len(_bin_widths) * 0.03,
258+
final_bins[-1] + _bin_widths[-1] * len(_bin_widths) * 0.08,
257259
],
258260
)
259261
value, variance = np.append(value, np.nan), np.append(variance, np.nan)
@@ -314,9 +316,6 @@ def iterable_not_string(arg):
314316
for i in range(len(_chunked_kwargs)):
315317
_chunked_kwargs[i][kwarg] = kwargs[kwarg]
316318

317-
_bin_widths = np.diff(final_bins)
318-
_bin_centers = final_bins[1:] - _bin_widths / float(2)
319-
320319
############################
321320
# # yerr calculation
322321
_yerr: np.ndarray | None
@@ -529,7 +528,7 @@ def iterable_not_string(arg):
529528
if flow == "hint":
530529
ax.plot(
531530
[
532-
final_bins[0] - (final_bins[-3] - final_bins[2]) * 0.03,
531+
final_bins[0] - _bin_widths[0] * len(_bin_widths) * 0.03,
533532
final_bins[0],
534533
],
535534
[0, 0],
@@ -550,7 +549,7 @@ def iterable_not_string(arg):
550549
ax.plot(
551550
[
552551
final_bins[-1],
553-
final_bins[-1] + (final_bins[-3] - final_bins[2]) * 0.03,
552+
final_bins[-1] + _bin_widths[-1] * len(_bin_widths) * 0.03,
554553
],
555554
[0, 0],
556555
**kwargs,
@@ -798,7 +797,10 @@ def hist2dplot(
798797
if any(h.values(flow=True)[0] > 0):
799798
if flow == "hint":
800799
ax.plot(
801-
[xbins[0] - (xbins[-3] - xbins[2]) * 0.03, xbins[0]],
800+
[
801+
xbins[0] - np.diff(xbins)[0] * len(np.diff(xbins)) * 0.03,
802+
xbins[0],
803+
],
802804
[0, 0],
803805
transform=trans,
804806
**kwargs,
@@ -809,7 +811,10 @@ def hist2dplot(
809811
if any(h.values(flow=True)[:, 0] > 0):
810812
if flow == "hint":
811813
ax.plot(
812-
[xbins[-1] + (xbins[-3] - xbins[2]) * 0.03, xbins[-1]],
814+
[
815+
xbins[-1] + np.diff(xbins)[-1] * len(np.diff(xbins)) * 0.03,
816+
xbins[-1],
817+
],
813818
[0, 0],
814819
transform=trans,
815820
**kwargs,
@@ -820,7 +825,10 @@ def hist2dplot(
820825
if any(h.values(flow=True)[-1] > 0):
821826
if flow == "hint":
822827
ax.plot(
823-
[xbins[0], xbins[0] - (xbins[-3] - xbins[2]) * 0.03],
828+
[
829+
xbins[0],
830+
xbins[0] - np.diff(xbins)[0] * len(np.diff(xbins)) * 0.03,
831+
],
824832
[1, 1],
825833
transform=trans,
826834
**kwargs,
@@ -832,7 +840,10 @@ def hist2dplot(
832840
if any(h.values(flow=True)[:, -1] > 0):
833841
if flow == "hint":
834842
ax.plot(
835-
[xbins[-1] + (xbins[-3] - xbins[2]) * 0.03, xbins[-1]],
843+
[
844+
xbins[-1] + np.diff(xbins)[-1] * len(np.diff(xbins)) * 0.03,
845+
xbins[-1],
846+
],
836847
[1, 1],
837848
transform=trans,
838849
**kwargs,
49 Bytes
Loading

tests/baseline/test_histplot_flow.png

-67 Bytes
Loading

tests/baseline/test_inputs_bh.png

-88 Bytes
Loading

tests/baseline/test_inputs_bh_cat.png

-14 Bytes
Loading

tests/baseline/test_inputs_uproot.png

16 Bytes
Loading

tests/baseline/test_onebin_hist.png

4.8 KB
Loading

tests/test_basic.py

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ def test_log():
6363
return fig
6464

6565

66+
@pytest.mark.mpl_image_compare(style="default", remove_text=True)
67+
def test_onebin_hist():
68+
import hist
69+
70+
fig, axs = plt.subplots()
71+
h = hist.Hist(hist.axis.Regular(1, 0, 1))
72+
h.fill([-1, 0.5])
73+
hep.histplot(h, ax=axs)
74+
return fig
75+
76+
6677
@pytest.mark.mpl_image_compare(style="default", remove_text=True)
6778
def test_histplot():
6879
np.random.seed(0)

0 commit comments

Comments
 (0)