Skip to content

Commit

Permalink
fix : flow plot bug on axis range for histplot and hist2dplot (#432)
Browse files Browse the repository at this point in the history
* fix : flow plot bug on axis range for histplot and hist2dplot

* feat : add test for one bin hist with flow bins #431
  • Loading branch information
Ming-Yan committed Jul 10, 2023
1 parent bd90705 commit 2aa455e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/mplhep/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ def histplot(
# Process input
hists = list(process_histogram_parts(H, bins))
final_bins, xtick_labels = get_plottable_protocol_bins(hists[0].axes[0])
_bin_widths = np.diff(final_bins)
_bin_centers = final_bins[1:] - _bin_widths / float(2)
assert final_bins.ndim == 1, "bins need to be 1 dimensional"
_x_axes_label = ax.get_xlabel()
x_axes_label = (
Expand Down Expand Up @@ -237,8 +239,8 @@ def histplot(
final_bins,
0,
[
final_bins[0] - (final_bins[-1] - final_bins[0]) * 0.08,
final_bins[0] - (final_bins[-1] - final_bins[0]) * 0.03,
final_bins[0] - _bin_widths[0] * len(_bin_widths) * 0.08,
final_bins[0] - _bin_widths[0] * len(_bin_widths) * 0.03,
],
)
value, variance = np.insert(value, 0, np.nan), np.insert(
Expand All @@ -252,8 +254,8 @@ def histplot(
flow_bins = np.append(
flow_bins,
[
final_bins[-1] + (final_bins[-1] - final_bins[0]) * 0.03,
final_bins[-1] + (final_bins[-1] - final_bins[0]) * 0.08,
final_bins[-1] + _bin_widths[-1] * len(_bin_widths) * 0.03,
final_bins[-1] + _bin_widths[-1] * len(_bin_widths) * 0.08,
],
)
value, variance = np.append(value, np.nan), np.append(variance, np.nan)
Expand Down Expand Up @@ -314,9 +316,6 @@ def iterable_not_string(arg):
for i in range(len(_chunked_kwargs)):
_chunked_kwargs[i][kwarg] = kwargs[kwarg]

_bin_widths = np.diff(final_bins)
_bin_centers = final_bins[1:] - _bin_widths / float(2)

############################
# # yerr calculation
_yerr: np.ndarray | None
Expand Down Expand Up @@ -529,7 +528,7 @@ def iterable_not_string(arg):
if flow == "hint":
ax.plot(
[
final_bins[0] - (final_bins[-3] - final_bins[2]) * 0.03,
final_bins[0] - _bin_widths[0] * len(_bin_widths) * 0.03,
final_bins[0],
],
[0, 0],
Expand All @@ -550,7 +549,7 @@ def iterable_not_string(arg):
ax.plot(
[
final_bins[-1],
final_bins[-1] + (final_bins[-3] - final_bins[2]) * 0.03,
final_bins[-1] + _bin_widths[-1] * len(_bin_widths) * 0.03,
],
[0, 0],
**kwargs,
Expand Down Expand Up @@ -798,7 +797,10 @@ def hist2dplot(
if any(h.values(flow=True)[0] > 0):
if flow == "hint":
ax.plot(
[xbins[0] - (xbins[-3] - xbins[2]) * 0.03, xbins[0]],
[
xbins[0] - np.diff(xbins)[0] * len(np.diff(xbins)) * 0.03,
xbins[0],
],
[0, 0],
transform=trans,
**kwargs,
Expand All @@ -809,7 +811,10 @@ def hist2dplot(
if any(h.values(flow=True)[:, 0] > 0):
if flow == "hint":
ax.plot(
[xbins[-1] + (xbins[-3] - xbins[2]) * 0.03, xbins[-1]],
[
xbins[-1] + np.diff(xbins)[-1] * len(np.diff(xbins)) * 0.03,
xbins[-1],
],
[0, 0],
transform=trans,
**kwargs,
Expand All @@ -820,7 +825,10 @@ def hist2dplot(
if any(h.values(flow=True)[-1] > 0):
if flow == "hint":
ax.plot(
[xbins[0], xbins[0] - (xbins[-3] - xbins[2]) * 0.03],
[
xbins[0],
xbins[0] - np.diff(xbins)[0] * len(np.diff(xbins)) * 0.03,
],
[1, 1],
transform=trans,
**kwargs,
Expand All @@ -832,7 +840,10 @@ def hist2dplot(
if any(h.values(flow=True)[:, -1] > 0):
if flow == "hint":
ax.plot(
[xbins[-1] + (xbins[-3] - xbins[2]) * 0.03, xbins[-1]],
[
xbins[-1] + np.diff(xbins)[-1] * len(np.diff(xbins)) * 0.03,
xbins[-1],
],
[1, 1],
transform=trans,
**kwargs,
Expand Down
Binary file modified tests/baseline/test_hist2dplot_flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_histplot_flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_inputs_bh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_inputs_bh_cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_inputs_uproot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/baseline/test_onebin_hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ def test_log():
return fig


@pytest.mark.mpl_image_compare(style="default", remove_text=True)
def test_onebin_hist():
import hist

fig, axs = plt.subplots()
h = hist.Hist(hist.axis.Regular(1, 0, 1))
h.fill([-1, 0.5])
hep.histplot(h, ax=axs)
return fig


@pytest.mark.mpl_image_compare(style="default", remove_text=True)
def test_histplot():
np.random.seed(0)
Expand Down

0 comments on commit 2aa455e

Please sign in to comment.