.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/model_ex/model_examples_stacked.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_examples_stacked.py: Data vs model with stacked components ===================================== Plot data and a model with stacked components. .. GENERATED FROM PYTHON SOURCE LINES 7-75 .. image-sg:: /example_gallery/model_ex/images/sphx_glr_model_examples_stacked_001.svg :alt: model examples stacked :srcset: /example_gallery/model_ex/images/sphx_glr_model_examples_stacked_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 ] signal_hist = make_hist(df[key][signal_mask], bins=50, range=range, weights=1) # 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] signal_scaling_factor = data_hist.sum().value / signal_hist.sum().value signal_hist *= signal_scaling_factor ### from plothist import add_luminosity, plot_data_model_comparison, plot_hist fig, ax_main, ax_comparison = plot_data_model_comparison( data_hist=data_hist, stacked_components=background_hists, stacked_labels=background_categories_labels, stacked_colors=background_categories_colors, xlabel=key, ylabel="Entries", ) # Signal histogram not part of the model and therefore not included in the comparison plot_hist( signal_hist, ax=ax_main, color="red", label="Signal", histtype="step", ) ax_main.legend() add_luminosity( collaboration="plothist", ax=ax_main, lumi=3, lumi_unit="zb", preliminary=True ) fig.savefig("model_examples_stacked.svg", bbox_inches="tight") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.367 seconds) .. _sphx_glr_download_example_gallery_model_ex_model_examples_stacked.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: model_examples_stacked.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: model_examples_stacked.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: model_examples_stacked.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_