Skip to content

Commit

Permalink
draft including ratio subplot within differ when comparing
Browse files Browse the repository at this point in the history
The denominator for the ratio is whatever histogram is first in the list. 

We try to inherit the color from the histogram in the ratio plot so that the legend applies.

If we want to include error bars, we should probably switch to using `hist` directly since they have better error bar handling and have ironed out a lot of the common bugs.
  • Loading branch information
tomeichlersmith committed Aug 29, 2024
1 parent d74f8cc commit 8b49d02
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions Validation/src/Validation/_differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,46 @@ def plot1d(self, column, xlabel,
All other key-word arguments are passed into each File.plot1d
"""
fig = plt.figure('differ',figsize=(11,8))
ax = fig.subplots()

raw_ax, ratio_ax = fig.subplots(
nrows = 2,
sharex = 'col',
height_ratios = [2, 1],
gridspec_kw = dict(
hspace = 0.05
)
)

raw_histograms = []
for f in self.files :
try:
weights, bins, patches = f.plot1d(ax, column, **hist_kwargs)
raw_histograms.append(f.plot1d(raw_ax, column, **hist_kwargs))
except uproot.KeyInFileError:
f.log.warn(f"Key {column} doesn't exist in {self}, skipping")
continue

ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_yscale(yscale)
ax.set_ylim(*ylim)
raw_ax.set_ylabel(ylabel)
raw_ax.set_yscale(yscale)
raw_ax.set_ylim(*ylim)

if 'title' not in legend_kw :
legend_kw['title'] = self.grp_name

if tick_labels is not None:
ax.set_xticks((bins[1:]+bins[:-1])/2)
ax.set_xticklabels(tick_labels)
ax.tick_params(axis='x', rotation=90)

raw_ax.legend(**legend_kw)

ax.legend(**legend_kw)
denominator, bins, _denominator_art = raw_histograms[0]
bin_centers = (bins[1:]+bins[:-1])/2
for values, _bins, art in raw_histograms[1:]:
ratio_ax.plot(
bin_centers,
values/denominator,
color = art[0].get_edgecolor()
)

ratio_ax.set_xlabel(xlabel)
if tick_labels is not None:
ratio_ax.set_xticks((bins[1:]+bins[:-1])/2)
ratio_ax.set_xticklabels(tick_labels)
ratio_ax.tick_params(axis='x', rotation=90)

if out_dir is None :
plt.show()
Expand Down

0 comments on commit 8b49d02

Please sign in to comment.