plothist 1.4.0__py3-none-any.whl → 1.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- plothist/__init__.py +5 -5
- plothist/_version.py +2 -2
- plothist/comparison.py +170 -120
- plothist/examples/1d_hist/1d_comparison_asymmetry.py +37 -0
- plothist/examples/1d_hist/1d_comparison_difference.py +40 -0
- plothist/examples/1d_hist/1d_comparison_efficiency.py +37 -0
- plothist/examples/1d_hist/1d_comparison_only_efficiency.py +33 -0
- plothist/examples/1d_hist/1d_comparison_pull.py +37 -0
- plothist/examples/1d_hist/1d_comparison_ratio.py +37 -0
- plothist/examples/1d_hist/1d_comparison_relative_difference.py +37 -0
- plothist/examples/1d_hist/1d_comparison_split_ratio.py +37 -0
- plothist/examples/1d_hist/1d_elt1.py +38 -0
- plothist/examples/1d_hist/1d_elt1_stacked.py +45 -0
- plothist/examples/1d_hist/1d_elt2.py +33 -0
- plothist/examples/1d_hist/1d_hist_simple.py +28 -0
- plothist/examples/1d_hist/1d_int_category.py +41 -0
- plothist/examples/1d_hist/1d_profile.py +33 -0
- plothist/examples/1d_hist/1d_side_by_side.py +58 -0
- plothist/examples/1d_hist/1d_str_category.py +41 -0
- plothist/examples/1d_hist/README.rst +4 -0
- plothist/examples/2d_hist/2d_hist_correlations.py +65 -0
- plothist/examples/2d_hist/2d_hist_simple.py +28 -0
- plothist/examples/2d_hist/2d_hist_simple_discrete_colormap.py +42 -0
- plothist/examples/2d_hist/2d_hist_uneven.py +28 -0
- plothist/examples/2d_hist/2d_hist_with_projections.py +36 -0
- plothist/examples/2d_hist/README.rst +4 -0
- plothist/examples/README.rst +7 -0
- plothist/examples/advanced/1d_comparison_advanced.py +87 -0
- plothist/examples/advanced/1d_side_by_side_with_numbers.py +81 -0
- plothist/examples/advanced/README.rst +4 -0
- plothist/examples/advanced/asymmetry_comparison_advanced.py +133 -0
- plothist/examples/advanced/model_examples_flatten2D.py +86 -0
- plothist/examples/func_1d/README.rst +4 -0
- plothist/examples/func_1d/fct_1d.py +27 -0
- plothist/examples/func_1d/fct_1d_stacked.py +42 -0
- plothist/examples/model_ex/README.rst +4 -0
- plothist/examples/model_ex/model_all_comparisons.py +103 -0
- plothist/examples/model_ex/model_all_comparisons_no_model_unc.py +115 -0
- plothist/examples/model_ex/model_examples_pull.py +56 -0
- plothist/examples/model_ex/model_examples_pull_no_model_unc.py +59 -0
- plothist/examples/model_ex/model_examples_stacked.py +74 -0
- plothist/examples/model_ex/model_examples_stacked_unstacked.py +60 -0
- plothist/examples/model_ex/model_examples_unstacked.py +57 -0
- plothist/examples/model_ex/model_with_stacked_and_unstacked_function_components.py +50 -0
- plothist/examples/model_ex/model_with_stacked_and_unstacked_histograms_components.py +69 -0
- plothist/examples/model_ex/ratio_data_vs_model_with_stacked_and_unstacked_function_components.py +61 -0
- plothist/examples/utility/README.rst +4 -0
- plothist/examples/utility/add_text_example.py +39 -0
- plothist/examples/utility/color_palette_hists.py +94 -0
- plothist/examples/utility/color_palette_squares.py +100 -0
- plothist/examples/utility/matplotlib_vs_plothist_style.py +63 -0
- plothist/examples/utility/uncertainty_types.py +120 -0
- plothist/histogramming.py +60 -39
- plothist/plothist_style.py +56 -59
- plothist/plotters.py +210 -195
- plothist/test_helpers.py +43 -0
- plothist/variable_registry.py +46 -30
- {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/METADATA +1 -1
- plothist-1.6.0.dist-info/RECORD +64 -0
- plothist/scripts/__init__.py +0 -3
- plothist/scripts/make_examples.py +0 -209
- plothist-1.4.0.dist-info/RECORD +0 -17
- plothist-1.4.0.dist-info/entry_points.txt +0 -2
- {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/WHEEL +0 -0
- {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/licenses/AUTHORS.md +0 -0
- {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data/model comparisons, no model uncertainty
|
|
3
|
+
============================================
|
|
4
|
+
|
|
5
|
+
All supported comparisons between data and model, without model uncertainty.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# Optional: scale to data
|
|
40
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
41
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
42
|
+
|
|
43
|
+
###
|
|
44
|
+
import numpy as np
|
|
45
|
+
|
|
46
|
+
from plothist import (
|
|
47
|
+
add_text,
|
|
48
|
+
create_comparison_figure,
|
|
49
|
+
plot_comparison,
|
|
50
|
+
plot_data_model_comparison,
|
|
51
|
+
set_fitting_ylabel_fontsize,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
fig, axes = create_comparison_figure(
|
|
55
|
+
figsize=(6, 13),
|
|
56
|
+
nrows=6,
|
|
57
|
+
gridspec_kw={"height_ratios": [3, 1, 1, 1, 1, 1]},
|
|
58
|
+
hspace=0.3,
|
|
59
|
+
)
|
|
60
|
+
background_sum = sum(background_hists)
|
|
61
|
+
|
|
62
|
+
plot_data_model_comparison(
|
|
63
|
+
data_hist=data_hist,
|
|
64
|
+
stacked_components=background_hists,
|
|
65
|
+
stacked_labels=background_categories_labels,
|
|
66
|
+
stacked_colors=background_categories_colors,
|
|
67
|
+
xlabel="",
|
|
68
|
+
ylabel="Entries",
|
|
69
|
+
model_uncertainty=False, # <--
|
|
70
|
+
comparison="ratio",
|
|
71
|
+
fig=fig,
|
|
72
|
+
ax_main=axes[0],
|
|
73
|
+
ax_comparison=axes[1],
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
add_text(
|
|
77
|
+
r"Multiple data-model comparisons, $\mathbf{without}$ model uncertainty",
|
|
78
|
+
ax=axes[0],
|
|
79
|
+
)
|
|
80
|
+
add_text(r' $\mathbf{→}$ comparison = "ratio"', ax=axes[1], fontsize=13)
|
|
81
|
+
|
|
82
|
+
for k_comp, comparison in enumerate(
|
|
83
|
+
["split_ratio", "pull", "relative_difference", "difference"], start=2
|
|
84
|
+
):
|
|
85
|
+
ax_comparison = axes[k_comp]
|
|
86
|
+
|
|
87
|
+
# Copy the original histogram and set the uncertainties of the copy to 0.
|
|
88
|
+
background_sum_copy = background_sum.copy()
|
|
89
|
+
background_sum_copy[:] = np.c_[
|
|
90
|
+
background_sum_copy.values(), np.zeros_like(background_sum_copy.values())
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
plot_comparison(
|
|
94
|
+
data_hist,
|
|
95
|
+
background_sum_copy,
|
|
96
|
+
ax=ax_comparison,
|
|
97
|
+
comparison=comparison,
|
|
98
|
+
xlabel="",
|
|
99
|
+
h1_label="Data",
|
|
100
|
+
h2_label="Pred.",
|
|
101
|
+
h1_uncertainty_type="asymmetrical",
|
|
102
|
+
)
|
|
103
|
+
if comparison == "pull":
|
|
104
|
+
# Since the uncertainties of the model are neglected, the pull label is "(Data - Pred.)/sigma_Data"
|
|
105
|
+
ax_comparison.set_ylabel(r"$\frac{Data-Pred.}{\sigma_{Data}}$")
|
|
106
|
+
add_text(
|
|
107
|
+
rf' $\mathbf{{→}}$ comparison = "{comparison}"',
|
|
108
|
+
ax=ax_comparison,
|
|
109
|
+
fontsize=13,
|
|
110
|
+
)
|
|
111
|
+
set_fitting_ylabel_fontsize(ax_comparison)
|
|
112
|
+
|
|
113
|
+
axes[-1].set_xlabel(key)
|
|
114
|
+
|
|
115
|
+
fig.savefig("model_all_comparisons_no_model_unc.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Pull plot
|
|
3
|
+
=========
|
|
4
|
+
|
|
5
|
+
Compare data and model with pulls.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# Optional: scale to data
|
|
40
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
41
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
42
|
+
|
|
43
|
+
###
|
|
44
|
+
from plothist import plot_data_model_comparison
|
|
45
|
+
|
|
46
|
+
fig, ax_main, ax_comparison = plot_data_model_comparison(
|
|
47
|
+
data_hist=data_hist,
|
|
48
|
+
stacked_components=background_hists,
|
|
49
|
+
stacked_labels=background_categories_labels,
|
|
50
|
+
stacked_colors=background_categories_colors,
|
|
51
|
+
xlabel=rf"${key}\,\,[TeV/c^2]$",
|
|
52
|
+
ylabel="Candidates per 0.42 $TeV/c^2$",
|
|
53
|
+
comparison="pull",
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
fig.savefig("model_examples_pull.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Pull plot, no model uncertainty
|
|
3
|
+
===============================
|
|
4
|
+
|
|
5
|
+
Compare data and model with pulls, without model uncertainty.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# Optional: scale to data
|
|
40
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
41
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
42
|
+
|
|
43
|
+
###
|
|
44
|
+
from plothist import add_luminosity, plot_data_model_comparison
|
|
45
|
+
|
|
46
|
+
fig, ax_main, ax_comparison = plot_data_model_comparison(
|
|
47
|
+
data_hist=data_hist,
|
|
48
|
+
stacked_components=background_hists,
|
|
49
|
+
stacked_labels=background_categories_labels,
|
|
50
|
+
stacked_colors=background_categories_colors,
|
|
51
|
+
xlabel=rf"${key}\,\,[eV/c^2]$",
|
|
52
|
+
ylabel=r"Hits in the LMN per $4.2\times 10^{-1}\,\,eV/c^2$",
|
|
53
|
+
comparison="pull",
|
|
54
|
+
model_uncertainty=False, # <--
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
add_luminosity(collaboration="plothist", ax=ax_main, is_data=False)
|
|
58
|
+
|
|
59
|
+
fig.savefig("model_examples_pull_no_model_unc.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data vs model with stacked components
|
|
3
|
+
=====================================
|
|
4
|
+
|
|
5
|
+
Plot data and a model with stacked components.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
signal_hist = make_hist(df[key][signal_mask], bins=50, range=range, weights=1)
|
|
39
|
+
|
|
40
|
+
# Optional: scale to data
|
|
41
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
42
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
43
|
+
|
|
44
|
+
signal_scaling_factor = data_hist.sum().value / signal_hist.sum().value
|
|
45
|
+
signal_hist *= signal_scaling_factor
|
|
46
|
+
|
|
47
|
+
###
|
|
48
|
+
from plothist import add_luminosity, plot_data_model_comparison, plot_hist
|
|
49
|
+
|
|
50
|
+
fig, ax_main, ax_comparison = plot_data_model_comparison(
|
|
51
|
+
data_hist=data_hist,
|
|
52
|
+
stacked_components=background_hists,
|
|
53
|
+
stacked_labels=background_categories_labels,
|
|
54
|
+
stacked_colors=background_categories_colors,
|
|
55
|
+
xlabel=key,
|
|
56
|
+
ylabel="Entries",
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Signal histogram not part of the model and therefore not included in the comparison
|
|
60
|
+
plot_hist(
|
|
61
|
+
signal_hist,
|
|
62
|
+
ax=ax_main,
|
|
63
|
+
color="red",
|
|
64
|
+
label="Signal",
|
|
65
|
+
histtype="step",
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
ax_main.legend()
|
|
69
|
+
|
|
70
|
+
add_luminosity(
|
|
71
|
+
collaboration="plothist", ax=ax_main, lumi=3, lumi_unit="zb", preliminary=True
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
fig.savefig("model_examples_stacked.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data vs model with stacked and unstacked components
|
|
3
|
+
===================================================
|
|
4
|
+
|
|
5
|
+
Plot data and a model with stacked and unstacked components.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# Optional: scale to data
|
|
40
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
41
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
42
|
+
|
|
43
|
+
###
|
|
44
|
+
from plothist import plot_data_model_comparison
|
|
45
|
+
|
|
46
|
+
fig, ax_main, ax_comparison = plot_data_model_comparison(
|
|
47
|
+
data_hist=data_hist,
|
|
48
|
+
stacked_components=background_hists[:2],
|
|
49
|
+
stacked_labels=background_categories_labels[:2],
|
|
50
|
+
stacked_colors=background_categories_colors[:2],
|
|
51
|
+
unstacked_components=background_hists[2:],
|
|
52
|
+
unstacked_labels=background_categories_labels[2:],
|
|
53
|
+
unstacked_colors=background_categories_colors[2:],
|
|
54
|
+
xlabel=key,
|
|
55
|
+
ylabel="Entries",
|
|
56
|
+
model_sum_kwargs={"show": True, "label": "Model", "color": "navy"},
|
|
57
|
+
comparison_ylim=(0.5, 1.5),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
fig.savefig("model_examples_stacked_unstacked.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data vs model with unstacked components
|
|
3
|
+
=====================================
|
|
4
|
+
|
|
5
|
+
Plot data and a model with unstacked components.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
# Optional: scale to data
|
|
40
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
41
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
42
|
+
|
|
43
|
+
###
|
|
44
|
+
from plothist import plot_data_model_comparison
|
|
45
|
+
|
|
46
|
+
fig, ax_main, ax_comparison = plot_data_model_comparison(
|
|
47
|
+
data_hist=data_hist,
|
|
48
|
+
unstacked_components=background_hists,
|
|
49
|
+
unstacked_labels=background_categories_labels,
|
|
50
|
+
unstacked_colors=background_categories_colors,
|
|
51
|
+
xlabel=key,
|
|
52
|
+
ylabel="Entries",
|
|
53
|
+
model_sum_kwargs={"label": "Sum(hists)", "color": "navy"},
|
|
54
|
+
comparison_ylim=[0.5, 1.5],
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
fig.savefig("model_examples_unstacked.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Model with stacked and unstacked functional components
|
|
3
|
+
======================================================
|
|
4
|
+
|
|
5
|
+
Plot a model with stacked and unstacked functional components.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
key = "variable_1"
|
|
9
|
+
range = (-9, 12)
|
|
10
|
+
|
|
11
|
+
background_categories = [0, 1, 2]
|
|
12
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# Define some random functions that will be used as model components with functions
|
|
16
|
+
from scipy.stats import norm
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def f_signal(x):
|
|
20
|
+
return 1000 * norm.pdf(x, loc=0.5, scale=3)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def f_background1(x):
|
|
24
|
+
return 1000 * norm.pdf(x, loc=-1.5, scale=4)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def f_background2(x):
|
|
28
|
+
return 3000 * norm.pdf(x, loc=-1.8, scale=1.8)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
###
|
|
32
|
+
from plothist import add_text, plot_model
|
|
33
|
+
|
|
34
|
+
fig, ax = plot_model(
|
|
35
|
+
stacked_components=[f_background1, f_background2],
|
|
36
|
+
stacked_labels=background_categories_labels[:2],
|
|
37
|
+
unstacked_components=[f_signal],
|
|
38
|
+
unstacked_labels=["Signal"],
|
|
39
|
+
unstacked_colors=["black"],
|
|
40
|
+
xlabel=key,
|
|
41
|
+
ylabel=f"f({key})",
|
|
42
|
+
model_sum_kwargs={"show": True, "label": "Model", "color": "navy"},
|
|
43
|
+
function_range=range,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
add_text("Model made of functions", ax=ax)
|
|
47
|
+
|
|
48
|
+
fig.savefig(
|
|
49
|
+
"model_with_stacked_and_unstacked_function_components.svg", bbox_inches="tight"
|
|
50
|
+
)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Model with stacked and unstacked components
|
|
3
|
+
===========================================
|
|
4
|
+
|
|
5
|
+
Plot a model with stacked and unstacked components.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import get_color_palette, make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
signal_mask = df[category] == 7
|
|
22
|
+
data_mask = df[category] == 8
|
|
23
|
+
|
|
24
|
+
background_categories = [0, 1, 2]
|
|
25
|
+
background_categories_labels = [f"c{i}" for i in background_categories]
|
|
26
|
+
background_categories_colors = get_color_palette(
|
|
27
|
+
"cubehelix", len(background_categories)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
background_masks = [df[category] == p for p in background_categories]
|
|
31
|
+
|
|
32
|
+
# Make histograms
|
|
33
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
34
|
+
background_hists = [
|
|
35
|
+
make_hist(df[key][mask], bins=50, range=range, weights=1)
|
|
36
|
+
for mask in background_masks
|
|
37
|
+
]
|
|
38
|
+
signal_hist = make_hist(df[key][signal_mask], bins=50, range=range, weights=1)
|
|
39
|
+
|
|
40
|
+
# Optional: scale to data
|
|
41
|
+
background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
|
|
42
|
+
background_hists = [background_scaling_factor * h for h in background_hists]
|
|
43
|
+
|
|
44
|
+
signal_scaling_factor = data_hist.sum().value / signal_hist.sum().value
|
|
45
|
+
signal_hist *= signal_scaling_factor
|
|
46
|
+
|
|
47
|
+
###
|
|
48
|
+
from plothist import add_text, plot_model
|
|
49
|
+
|
|
50
|
+
fig, ax = plot_model(
|
|
51
|
+
stacked_components=background_hists,
|
|
52
|
+
stacked_labels=background_categories_labels,
|
|
53
|
+
stacked_colors=background_categories_colors,
|
|
54
|
+
unstacked_components=[signal_hist],
|
|
55
|
+
unstacked_labels=["Signal"],
|
|
56
|
+
unstacked_colors=["black"],
|
|
57
|
+
unstacked_kwargs_list=[{"linestyle": "dotted"}],
|
|
58
|
+
xlabel=key,
|
|
59
|
+
ylabel="Entries",
|
|
60
|
+
model_sum_kwargs={"show": True, "label": "Model", "color": "navy"},
|
|
61
|
+
model_uncertainty_label="Stat. unc.",
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
add_text("Model made of histograms", ax=ax)
|
|
65
|
+
|
|
66
|
+
fig.savefig(
|
|
67
|
+
"model_with_stacked_and_unstacked_histograms_components.svg",
|
|
68
|
+
bbox_inches="tight",
|
|
69
|
+
)
|
plothist/examples/model_ex/ratio_data_vs_model_with_stacked_and_unstacked_function_components.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Data vs functional model
|
|
3
|
+
========================
|
|
4
|
+
|
|
5
|
+
Compare data and model with stacked and unstacked functional components.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import make_hist
|
|
13
|
+
|
|
14
|
+
# Define the histograms
|
|
15
|
+
|
|
16
|
+
key = "variable_1"
|
|
17
|
+
range = (-9, 12)
|
|
18
|
+
category = "category"
|
|
19
|
+
|
|
20
|
+
# Define masks
|
|
21
|
+
data_mask = df[category] == 8
|
|
22
|
+
|
|
23
|
+
# Make histograms
|
|
24
|
+
data_hist = make_hist(df[key][data_mask], bins=50, range=range, weights=1)
|
|
25
|
+
|
|
26
|
+
# Define some random functions that will be used as model components with functions
|
|
27
|
+
from scipy.stats import norm
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def f_signal(x):
|
|
31
|
+
return 1000 * norm.pdf(x, loc=0.5, scale=3)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def f_background1(x):
|
|
35
|
+
return 1000 * norm.pdf(x, loc=-1.5, scale=4)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def f_background2(x):
|
|
39
|
+
return 3000 * norm.pdf(x, loc=-1.8, scale=1.8)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
###
|
|
43
|
+
from plothist import plot_data_model_comparison
|
|
44
|
+
|
|
45
|
+
fig, ax_main, ax_comparison = plot_data_model_comparison(
|
|
46
|
+
data_hist=data_hist,
|
|
47
|
+
stacked_components=[f_background1, f_background2],
|
|
48
|
+
stacked_labels=["c0", "c1"],
|
|
49
|
+
unstacked_components=[f_signal],
|
|
50
|
+
unstacked_labels=["Signal"],
|
|
51
|
+
unstacked_colors=["#8EBA42"],
|
|
52
|
+
xlabel=key,
|
|
53
|
+
ylabel="Entries",
|
|
54
|
+
model_sum_kwargs={"show": True, "label": "Model", "color": "navy"},
|
|
55
|
+
comparison="pull",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
fig.savefig(
|
|
59
|
+
"ratio_data_vs_model_with_stacked_and_unstacked_function_components.svg",
|
|
60
|
+
bbox_inches="tight",
|
|
61
|
+
)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Add text
|
|
3
|
+
========
|
|
4
|
+
|
|
5
|
+
Examples of use of ``add_text()``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
###
|
|
9
|
+
import matplotlib.pyplot as plt
|
|
10
|
+
|
|
11
|
+
from plothist import add_text
|
|
12
|
+
|
|
13
|
+
fig, ax = plt.subplots()
|
|
14
|
+
|
|
15
|
+
positions = [
|
|
16
|
+
("right_in", "top_in"),
|
|
17
|
+
("left_in", "top_in"),
|
|
18
|
+
("left_in", "bottom_in"),
|
|
19
|
+
("right_in", "bottom_in"),
|
|
20
|
+
("right", "top_out"),
|
|
21
|
+
("left", "top_out"),
|
|
22
|
+
("right_out", "top_in"),
|
|
23
|
+
("right_out", "bottom_in"),
|
|
24
|
+
("right", "bottom_out"),
|
|
25
|
+
("left", "bottom_out"),
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
for x, y in positions:
|
|
29
|
+
x_label = x.replace("_", r"\_")
|
|
30
|
+
y_label = y.replace("_", r"\_")
|
|
31
|
+
add_text(
|
|
32
|
+
f"$\\mathtt{{add\\_text()}}$\n"
|
|
33
|
+
f'$\\mathtt{{x = }}$"$\\mathtt{{{x_label}}}$"\n'
|
|
34
|
+
f'$\\mathtt{{y = }}$"$\\mathtt{{{y_label}}}$"',
|
|
35
|
+
x=x,
|
|
36
|
+
y=y,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
fig.savefig("add_text_example.svg", bbox_inches="tight")
|