.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/model_ex/model_all_comparisons.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_example_gallery_model_ex_model_all_comparisons.py: Data/model comparisons ====================== All supported comparisons between data and model. .. GENERATED FROM PYTHON SOURCE LINES 7-104 .. image-sg:: /example_gallery/model_ex/images/sphx_glr_model_all_comparisons_001.svg :alt: model all comparisons :srcset: /example_gallery/model_ex/images/sphx_glr_model_all_comparisons_001.svg :class: sphx-glr-single-img .. code-block:: Python from plothist_utils import get_dummy_data df = get_dummy_data() from plothist import get_color_palette, make_hist # Define the histograms key = "variable_1" range = (-9, 12) category = "category" # Define masks signal_mask = df[category] == 7 data_mask = df[category] == 8 background_categories = [0, 1, 2] background_categories_labels = [f"c{i}" for i in background_categories] background_categories_colors = get_color_palette( "cubehelix", len(background_categories) ) background_masks = [df[category] == p for p in background_categories] # Make histograms data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1) background_hists = [ make_hist(df[key][mask], bins=50, range=range, weights=1) for mask in background_masks ] # Optional: scale to data background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value background_hists = [background_scaling_factor * h for h in background_hists] ### from plothist import ( add_text, create_comparison_figure, plot_comparison, plot_data_model_comparison, set_fitting_ylabel_fontsize, ) fig, axes = create_comparison_figure( figsize=(6, 13), nrows=6, gridspec_kw={"height_ratios": [3, 1, 1, 1, 1, 1]}, hspace=0.3, ) background_sum = sum(background_hists) plot_data_model_comparison( data_hist=data_hist, stacked_components=background_hists, stacked_labels=background_categories_labels, stacked_colors=background_categories_colors, xlabel="", ylabel="Entries", comparison="ratio", fig=fig, ax_main=axes[0], ax_comparison=axes[1], ) add_text( r"Multiple data-model comparisons, $\mathbf{with}$ model uncertainty", ax=axes[0], ) add_text(r' $\mathbf{→}$ comparison = "ratio"', ax=axes[1], fontsize=13) for k_comp, comparison in enumerate( ["split_ratio", "pull", "relative_difference", "difference"], start=2 ): ax_comparison = axes[k_comp] plot_comparison( data_hist, background_sum, ax=ax_comparison, comparison=comparison, xlabel="", h1_label="Data", h2_label="Pred.", h1_uncertainty_type="asymmetrical", ) add_text( rf' $\mathbf{{→}}$ comparison = "{comparison}"', ax=ax_comparison, fontsize=13, ) set_fitting_ylabel_fontsize(ax_comparison) axes[-1].set_xlabel(key) fig.savefig("model_all_comparisons.svg", bbox_inches="tight") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.270 seconds) .. _sphx_glr_download_example_gallery_model_ex_model_all_comparisons.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: model_all_comparisons.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: model_all_comparisons.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: model_all_comparisons.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_