From 9d201a5c2454469984eddff307f88f55197de722 Mon Sep 17 00:00:00 2001 From: Tom Eichlersmith <31970302+tomeichlersmith@users.noreply.github.com> Date: Wed, 21 Aug 2024 10:34:56 -0500 Subject: [PATCH 1/2] draft including ratio subplot within differ when comparing 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. --- Validation/src/Validation/_differ.py | 42 +++++++++++++++++++--------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/Validation/src/Validation/_differ.py b/Validation/src/Validation/_differ.py index ab4adb2be..da309f0cc 100644 --- a/Validation/src/Validation/_differ.py +++ b/Validation/src/Validation/_differ.py @@ -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() From bb5b621a106b8f5686cea8f6b01af4cc07f75355 Mon Sep 17 00:00:00 2001 From: Tom Eichlersmith <31970302+tomeichlersmith@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:17:18 -0500 Subject: [PATCH 2/2] change connected dots to a scatter plot We could get a scatter plot by altering the `format` passed to `plot` but `scatter` has nice defaults. --- Validation/src/Validation/_differ.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Validation/src/Validation/_differ.py b/Validation/src/Validation/_differ.py index da309f0cc..752c29e50 100644 --- a/Validation/src/Validation/_differ.py +++ b/Validation/src/Validation/_differ.py @@ -137,12 +137,13 @@ def plot1d(self, column, xlabel, 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( + ratio_ax.scatter( bin_centers, values/denominator, color = art[0].get_edgecolor() ) - + + ratio_ax.set_ylabel('Ratio') ratio_ax.set_xlabel(xlabel) if tick_labels is not None: ratio_ax.set_xticks((bins[1:]+bins[:-1])/2)