Skip to content

Commit

Permalink
fix : minor fixes
Browse files Browse the repository at this point in the history
- set space with nan
- change space/flow bin width
- revert updated image
  • Loading branch information
Ming-Yan committed Mar 19, 2023
1 parent 2c3b83f commit 063f1d9
Show file tree
Hide file tree
Showing 36 changed files with 77 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.3.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -23,7 +23,7 @@ repos:
args: ["--include-version-classifiers", "--max-py-version=3.11"]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.10.1
hooks:
- id: isort
args: ["-a", "from __future__ import annotations"]
Expand Down
76 changes: 27 additions & 49 deletions src/mplhep/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ def histplot(
)

# Cast to plottables

plottables = [
Plottable(h.values(), edges=final_bins, variances=h.variances()) for h in hists
]
Expand All @@ -181,17 +180,21 @@ def histplot(
plottables = []
final_bins = np.array(
[
final_bins[0] - (final_bins[1] - final_bins[0]) * 3,
final_bins[0] - (final_bins[1] - 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,
final_bins[-1] + (final_bins[1] - final_bins[0]),
final_bins[-1] + (final_bins[1] - final_bins[0]) * 3,
final_bins[-1] + (final_bins[-1] - final_bins[0]) * 0.03,
final_bins[-1] + (final_bins[-1] - final_bins[0]) * 0.08,
]
)
for h in hists:
value, variance = h.view(flow=True)["value"], h.view(flow=True)["variance"]
value, variance = np.insert(value, -1, 0), np.insert(variance, -1, 0)
value, variance = np.insert(value, 1, 0), np.insert(variance, 1, 0)
value, variance = np.insert(value, -1, np.nan), np.insert(
variance, -1, np.nan
)
value, variance = np.insert(value, 1, np.nan), np.insert(
variance, 1, np.nan
)
plottables.append(Plottable(value, edges=final_bins, variances=variance))
# "sum": Add under/overflow bin to first/last bin
elif flow == "sum":
Expand Down Expand Up @@ -465,31 +468,18 @@ def iterable_not_string(arg):
if flow == "hint":
ax.plot(
[
final_bins[0] - (final_bins[1] - final_bins[0]) / 2.0,
final_bins[0] - (final_bins[-3] - final_bins[2]) * 0.03,
final_bins[0],
],
[0, 0],
**kwargs,
)
ax.plot(
[
final_bins[0] - (final_bins[1] - final_bins[0]) / 2.0,
final_bins[0],
],
[1, 1],
**kwargs,
)
if flow == "show":
ax.plot(
[final_bins[1], final_bins[2]],
[0, 0],
**kwargs,
)
ax.plot(
[final_bins[1], final_bins[2]],
[1, 1],
**kwargs,
)
xticks[0] = ""
xticks[1] = f"<{final_bins[2]}"

Expand All @@ -499,30 +489,17 @@ def iterable_not_string(arg):
ax.plot(
[
final_bins[-1],
final_bins[-1] + (final_bins[1] - final_bins[0]) / 2.0,
final_bins[-1] + (final_bins[-3] - final_bins[2]) * 0.03,
],
[0, 0],
**kwargs,
)
ax.plot(
[
final_bins[-1],
final_bins[-1] + (final_bins[1] - final_bins[0]) / 2.0,
],
[1, 1],
**kwargs,
)
if flow == "show":
ax.plot(
[final_bins[-3], final_bins[-2]],
[0, 0],
**kwargs,
)
ax.plot(
[final_bins[-3], final_bins[-2]],
[1, 1],
**kwargs,
)
xticks[-1] = ""
xticks[-2] = f">{final_bins[-3]}"
ax.set_xticklabels(xticks)
Expand Down Expand Up @@ -612,26 +589,27 @@ def hist2dplot(
# "show": Add additional bin with 2 times bin width
if flow == "show":
H = hist.view(flow=True)["value"]

xbins = np.array(
[
xbins[0] - (xbins[1] - xbins[0]) * 3,
xbins[0] - (xbins[1] - xbins[0]),
xbins[0] - (xbins[-1] - xbins[0]) * 0.08,
xbins[0] - (xbins[-1] - xbins[0]) * 0.03,
*xbins,
xbins[-1] + (xbins[1] - xbins[0]),
xbins[-1] + (xbins[1] - xbins[0]) * 3,
xbins[-1] + (xbins[-1] - xbins[0]) * 0.03,
xbins[-1] + (xbins[-1] - xbins[0]) * 0.08,
]
)
ybins = np.array(
[
ybins[0] - (ybins[1] - ybins[0]) * 3,
ybins[0] - (ybins[1] - ybins[0]),
ybins[0] - (ybins[-1] - ybins[0]) * 0.08,
ybins[0] - (ybins[-1] - ybins[0]) * 0.03,
*ybins,
ybins[-1] + (ybins[1] - ybins[0]),
ybins[-1] + (ybins[1] - ybins[0]) * 3,
ybins[-1] + (ybins[-1] - ybins[0]) * 0.03,
ybins[-1] + (ybins[-1] - ybins[0]) * 0.08,
]
)
H = np.insert(H, (1, -1), 0, axis=-1)
H = np.insert(H, (1, -1), np.zeros(np.shape(H)[1]), axis=0)
H = np.insert(H, (1, -1), np.nan, axis=-1)
H = np.insert(H, (1, -1), np.full(np.shape(H)[1], np.nan), axis=0)

if flow == "sum":
H[0, 0], H[-1, -1], H[0, -1], H[-1, 0] = (
Expand Down Expand Up @@ -711,7 +689,7 @@ def hist2dplot(
if hist.view(flow=True)["value"][0, 0] > 0.0:
if flow == "hint":
ax.plot(
[xbins[0] - (xbins[1] - xbins[0]) / 2.0, xbins[0]],
[xbins[0] - (xbins[-3] - xbins[2]) * 0.03, xbins[0]],
[0, 0],
transform=trans,
**kwargs,
Expand All @@ -725,7 +703,7 @@ def hist2dplot(
if hist.view(flow=True)["value"][-1, 0] > 0.0:
if flow == "hint":
ax.plot(
[xbins[-1] + (xbins[1] - xbins[0]) / 2.0, xbins[-1]],
[xbins[-1] + (xbins[-3] - xbins[2]) * 0.03, xbins[-1]],
[0, 0],
transform=trans,
**kwargs,
Expand All @@ -739,7 +717,7 @@ def hist2dplot(
if hist.view(flow=True)["value"][0, -1] > 0.0:
if flow == "hint":
ax.plot(
[xbins[0], xbins[0] - (xbins[1] - xbins[0]) / 2.0],
[xbins[0], xbins[0] - (xbins[-3] - xbins[2]) * 0.03],
[1, 1],
transform=trans,
**kwargs,
Expand All @@ -753,7 +731,7 @@ def hist2dplot(
if hist.view(flow=True)["value"][-1, -1] > 0.0:
if flow == "hint":
ax.plot(
[xbins[-1] + (xbins[1] - xbins[0]) / 2.0, xbins[-1]],
[xbins[-1] + (xbins[-3] - xbins[2]) * 0.03, xbins[-1]],
[1, 1],
transform=trans,
**kwargs,
Expand Down
Binary file modified tests/baseline/test_hist2dplot.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_hist2dplot_cbar_False.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_hist2dplot_cbar_True.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_hist2dplot_cbar_subplots.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_hist2dplot_custom_labels.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_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_hist2dplot_inputs_nobin.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.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_density.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 added tests/baseline/test_histplot_flow_stacked.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_histplot_flow_unequal.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_kwargs.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_multiple.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_real.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_stack.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_types.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_w2.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_basic.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 modified tests/baseline/test_label_loc.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_labeltext_loc.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_log.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_simple.png
Binary file modified tests/baseline/test_simple2d.png
Binary file modified tests/baseline/test_simple_xerr.png
Binary file modified tests/baseline/test_style_alice.png
Binary file modified tests/baseline/test_style_atlas.png
Binary file modified tests/baseline/test_style_cms.png
Binary file modified tests/baseline/test_style_lhcb.png
Binary file modified tests/baseline/test_style_lhcb2.png
48 changes: 48 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,54 @@ def test_histplot_flow():
return fig


@pytest.mark.mpl_image_compare(style="default")
def test_histplot_flow_unequal():
np.random.seed(0)

h = hist.Hist(
hist.axis.Variable(
[5, 8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.7, 9, 9.3, 10, 13, 14.5, 15], name="x"
),
hist.storage.Weight(),
)
h.fill(np.random.normal(10, 3, 400))
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
axs = axs.flatten()

hep.histplot(h, ax=axs[0], flow="hint")
hep.histplot(h, ax=axs[1], flow="None")
hep.histplot(h, ax=axs[2], flow="show")
hep.histplot(h, ax=axs[3], flow="sum")

axs[0].set_title("Default", fontsize=18)
axs[1].set_title("None", fontsize=18)
axs[2].set_title("Show", fontsize=18)
axs[3].set_title("Sum", fontsize=18)
fig.subplots_adjust(hspace=0.1, wspace=0.1)
return fig


@pytest.mark.mpl_image_compare(style="default")
def test_histplot_flow_stacked():
np.random.seed(0)
h = hist.Hist(hist.axis.Regular(20, 5, 15, name="x"), hist.storage.Weight())
h.fill(np.random.normal(10, 3, 400))
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(10, 10))
axs = axs.flatten()

hep.histplot([h, h * 2], ax=axs[0], stack=True, flow="hint")
hep.histplot([h, h * 2], ax=axs[1], stack=True, flow="None")
hep.histplot([h, h * 2], ax=axs[2], stack=True, flow="show")
hep.histplot([h, h * 2], ax=axs[3], stack=True, flow="sum")

axs[0].set_title("Default", fontsize=18)
axs[1].set_title("None", fontsize=18)
axs[2].set_title("Show", fontsize=18)
axs[3].set_title("Sum", fontsize=18)
fig.subplots_adjust(hspace=0.1, wspace=0.1)
return fig


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

0 comments on commit 063f1d9

Please sign in to comment.