Skip to content

Commit cbbd848

Browse files
author
Mori Ludovico
committed
add lightweight hist plotter
1 parent 17fb7b5 commit cbbd848

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/boostedhh/plotting.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,80 @@ def cutsLinePlot(
13291329
plt.show()
13301330
else:
13311331
plt.close()
1332+
1333+
1334+
def plot_hist(
1335+
data,
1336+
names,
1337+
nbins=100,
1338+
weights=None,
1339+
xlabel=None,
1340+
saveas=None,
1341+
text=None,
1342+
xlim=None,
1343+
log=False,
1344+
density=False,
1345+
int_xticks=False,
1346+
lumi=None,
1347+
year=None,
1348+
com=13.6,
1349+
):
1350+
"""Lightweight plotter for histograms.
1351+
Args:
1352+
data (list): List of data arrays to plot.
1353+
names (list): List of names for each data array.
1354+
nbins (int): Number of bins for the histogram.
1355+
weights (list): List of weights for each data array.
1356+
"""
1357+
1358+
hep.style.use("CMS")
1359+
colors = plt.cm.tab10.colors
1360+
fig, ax = plt.subplots(figsize=(12, 9))
1361+
hep.cms.label("Preliminary", data=True, lumi=lumi, year=year, com=com)
1362+
if weights is not None:
1363+
for d, w, name, c in zip(data, weights, names, colors[: len(data)]):
1364+
ax.hist(
1365+
d,
1366+
bins=nbins,
1367+
weights=w,
1368+
range=xlim,
1369+
label=name,
1370+
color=c,
1371+
density=density,
1372+
log=log,
1373+
histtype="step",
1374+
linewidth=2,
1375+
)
1376+
else:
1377+
for d, name, c in zip(data, names, colors[: len(data)]):
1378+
ax.hist(
1379+
d,
1380+
bins=nbins,
1381+
range=xlim,
1382+
label=name,
1383+
color=c,
1384+
density=density,
1385+
log=log,
1386+
histtype="step",
1387+
linewidth=2,
1388+
)
1389+
if xlabel:
1390+
ax.set_xlabel(xlabel)
1391+
if text is not None:
1392+
ax.text(
1393+
0.02,
1394+
0.6,
1395+
text,
1396+
fontsize=13,
1397+
bbox={"facecolor": "white", "edgecolor": "black"},
1398+
transform=ax.transAxes,
1399+
)
1400+
if int_xticks:
1401+
ax.xaxis.get_major_locator().set_params(integer=True)
1402+
ax.set_ylabel("Normalized frequency")
1403+
ax.set_xlim(xlim)
1404+
ax.legend()
1405+
if saveas:
1406+
plt.savefig(saveas)
1407+
print(f"saved figure as {saveas}")
1408+
plt.close()

0 commit comments

Comments
 (0)