plothist 1.4.0__py3-none-any.whl → 1.5.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.
Files changed (64) hide show
  1. plothist/_version.py +2 -2
  2. plothist/comparison.py +111 -105
  3. plothist/examples/1d_hist/1d_comparison_asymmetry.py +37 -0
  4. plothist/examples/1d_hist/1d_comparison_difference.py +40 -0
  5. plothist/examples/1d_hist/1d_comparison_efficiency.py +37 -0
  6. plothist/examples/1d_hist/1d_comparison_only_efficiency.py +33 -0
  7. plothist/examples/1d_hist/1d_comparison_pull.py +37 -0
  8. plothist/examples/1d_hist/1d_comparison_ratio.py +37 -0
  9. plothist/examples/1d_hist/1d_comparison_relative_difference.py +37 -0
  10. plothist/examples/1d_hist/1d_comparison_split_ratio.py +37 -0
  11. plothist/examples/1d_hist/1d_elt1.py +38 -0
  12. plothist/examples/1d_hist/1d_elt1_stacked.py +45 -0
  13. plothist/examples/1d_hist/1d_elt2.py +33 -0
  14. plothist/examples/1d_hist/1d_hist_simple.py +28 -0
  15. plothist/examples/1d_hist/1d_int_category.py +41 -0
  16. plothist/examples/1d_hist/1d_profile.py +33 -0
  17. plothist/examples/1d_hist/1d_side_by_side.py +58 -0
  18. plothist/examples/1d_hist/1d_str_category.py +41 -0
  19. plothist/examples/1d_hist/README.rst +4 -0
  20. plothist/examples/2d_hist/2d_hist_correlations.py +65 -0
  21. plothist/examples/2d_hist/2d_hist_simple.py +28 -0
  22. plothist/examples/2d_hist/2d_hist_simple_discrete_colormap.py +42 -0
  23. plothist/examples/2d_hist/2d_hist_uneven.py +28 -0
  24. plothist/examples/2d_hist/2d_hist_with_projections.py +36 -0
  25. plothist/examples/2d_hist/README.rst +4 -0
  26. plothist/examples/README.rst +7 -0
  27. plothist/examples/advanced/1d_comparison_advanced.py +87 -0
  28. plothist/examples/advanced/1d_side_by_side_with_numbers.py +81 -0
  29. plothist/examples/advanced/README.rst +4 -0
  30. plothist/examples/advanced/asymmetry_comparison_advanced.py +133 -0
  31. plothist/examples/advanced/model_examples_flatten2D.py +86 -0
  32. plothist/examples/func_1d/README.rst +4 -0
  33. plothist/examples/func_1d/fct_1d.py +27 -0
  34. plothist/examples/func_1d/fct_1d_stacked.py +42 -0
  35. plothist/examples/model_ex/README.rst +4 -0
  36. plothist/examples/model_ex/model_all_comparisons.py +103 -0
  37. plothist/examples/model_ex/model_all_comparisons_no_model_unc.py +115 -0
  38. plothist/examples/model_ex/model_examples_pull.py +56 -0
  39. plothist/examples/model_ex/model_examples_pull_no_model_unc.py +59 -0
  40. plothist/examples/model_ex/model_examples_stacked.py +74 -0
  41. plothist/examples/model_ex/model_examples_stacked_unstacked.py +60 -0
  42. plothist/examples/model_ex/model_examples_unstacked.py +57 -0
  43. plothist/examples/model_ex/model_with_stacked_and_unstacked_function_components.py +50 -0
  44. plothist/examples/model_ex/model_with_stacked_and_unstacked_histograms_components.py +69 -0
  45. plothist/examples/model_ex/ratio_data_vs_model_with_stacked_and_unstacked_function_components.py +61 -0
  46. plothist/examples/utility/README.rst +4 -0
  47. plothist/examples/utility/add_text_example.py +39 -0
  48. plothist/examples/utility/color_palette_hists.py +94 -0
  49. plothist/examples/utility/color_palette_squares.py +100 -0
  50. plothist/examples/utility/matplotlib_vs_plothist_style.py +63 -0
  51. plothist/histogramming.py +60 -39
  52. plothist/plothist_style.py +54 -57
  53. plothist/plotters.py +207 -194
  54. plothist/test_helpers.py +43 -0
  55. plothist/variable_registry.py +46 -30
  56. {plothist-1.4.0.dist-info → plothist-1.5.0.dist-info}/METADATA +1 -1
  57. plothist-1.5.0.dist-info/RECORD +63 -0
  58. plothist/scripts/__init__.py +0 -3
  59. plothist/scripts/make_examples.py +0 -209
  60. plothist-1.4.0.dist-info/RECORD +0 -17
  61. plothist-1.4.0.dist-info/entry_points.txt +0 -2
  62. {plothist-1.4.0.dist-info → plothist-1.5.0.dist-info}/WHEEL +0 -0
  63. {plothist-1.4.0.dist-info → plothist-1.5.0.dist-info}/licenses/AUTHORS.md +0 -0
  64. {plothist-1.4.0.dist-info → plothist-1.5.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,81 @@
1
+ """
2
+ Side-by-side categories, with numbers on top
3
+ ============================================
4
+
5
+ Plot multiple 1D histograms with categories side by side, and add the number of entries on top of each bar.
6
+ """
7
+
8
+ ###
9
+ import boost_histogram as bh
10
+ import matplotlib.pyplot as plt
11
+ import numpy as np
12
+
13
+ from plothist import plot_hist
14
+
15
+ rng = np.random.default_rng(83113111)
16
+
17
+ # Integer categories
18
+ categories = [-137, 12, 1234]
19
+ axis = bh.axis.IntCategory(categories=categories)
20
+
21
+ # Generate data for 3 histograms
22
+ data = [
23
+ rng.choice(categories, 50),
24
+ rng.choice(categories, 30),
25
+ rng.choice(categories, 35),
26
+ rng.choice(categories, 30),
27
+ ]
28
+
29
+ # Create and fill the histograms
30
+ histos = [bh.Histogram(axis, storage=bh.storage.Weight()) for _ in range(len(data))]
31
+ histos = [histo.fill(data[i]) for i, histo in enumerate(histos)]
32
+
33
+ labels = [f"$h_{{{i}}}$" for i in range(len(histos))]
34
+ colors = ["#348ABD", "#E24A33", "#988ED5", "#FBC15E"]
35
+
36
+ # Plot the histogram
37
+ fig, ax = plt.subplots()
38
+
39
+ # Use a specificity of matplotlib: when a list of histograms is given, it will plot them side by side unless stacked=True or histtype is a "step" type.
40
+ plot_hist(histos, ax=ax, label=labels, color=colors)
41
+
42
+
43
+ # Add the number of entries on top of each bar
44
+ # Get the correct shift in x-axis for each bar
45
+ def calculate_shifts(width, n_bars):
46
+ half_width = width / 2
47
+ shift = np.linspace(-half_width, half_width, n_bars, endpoint=False)
48
+ shift += width / (2 * n_bars)
49
+ return shift
50
+
51
+
52
+ bin_width = 0.8
53
+ shift = calculate_shifts(bin_width, len(histos))
54
+
55
+ # Loop over the histograms, add on top of each bar the number of entries
56
+ for i, histo in enumerate(histos):
57
+ for j, value in enumerate(histo.values()):
58
+ ax.text(
59
+ j + 0.5 + shift[i],
60
+ value,
61
+ int(
62
+ value
63
+ ), # If weighted, f"{height:.1f}" can be used as a better representation of the bin content
64
+ color="black",
65
+ ha="center",
66
+ va="bottom",
67
+ )
68
+
69
+ # Set the x-ticks to the middle of the bins and label them
70
+ ax.set_xlim(0, len(categories))
71
+ ax.set_xticks([i + 0.5 for i in range(len(categories))])
72
+ ax.set_xticklabels(categories)
73
+ ax.minorticks_off()
74
+ # Get nice looking y-axis ticks
75
+ ax.set_ylim(top=int(np.max([np.max(histo.values()) for histo in histos]) * 1.5))
76
+
77
+ ax.set_xlabel("Category")
78
+ ax.set_ylabel("Entries")
79
+ ax.legend()
80
+
81
+ fig.savefig("1d_side_by_side_with_numbers.svg", bbox_inches="tight")
@@ -0,0 +1,4 @@
1
+ Other advanced plots
2
+ --------------------
3
+
4
+ Gallery of images that are used in the other advanced plots section.
@@ -0,0 +1,133 @@
1
+ """
2
+ Advanced asymmetry comparison
3
+ =============================
4
+
5
+ Plot the asymmetry between 2 histograms and 2 functions.
6
+ """
7
+
8
+ # Get dummy data
9
+ from plothist_utils import get_dummy_data
10
+
11
+ df = get_dummy_data()
12
+
13
+ ###
14
+ from scipy.stats import norm
15
+
16
+ from plothist import (
17
+ add_luminosity,
18
+ add_text,
19
+ create_comparison_figure,
20
+ make_hist,
21
+ plot_comparison,
22
+ plot_error_hist,
23
+ plot_function,
24
+ )
25
+
26
+
27
+ # Define some random functions that will be used as Data fit functions
28
+ def f1(x):
29
+ return 4000 * norm.pdf(x, loc=-0.5, scale=1.6)
30
+
31
+
32
+ def f2(x):
33
+ return 4000 * norm.pdf(x, loc=0.5, scale=1.6)
34
+
35
+
36
+ name = "variable_1"
37
+ category = "category"
38
+
39
+ x1 = df[name][df[category] == 5]
40
+
41
+ x_range = (-9, 9)
42
+
43
+ # Create the histograms used as data
44
+ h1 = make_hist(x1 - 2.5, bins=50, range=x_range)
45
+ h2 = make_hist(x1 - 1.5, bins=50, range=x_range)
46
+
47
+ # Create the figure
48
+ fig, (ax_main, ax_comparison) = create_comparison_figure(
49
+ gridspec_kw={"height_ratios": [2, 1]}
50
+ )
51
+
52
+ # Define the marker style
53
+ marker_1 = {
54
+ "color": "tab:red",
55
+ "markeredgecolor": "tab:red",
56
+ "ls": "None",
57
+ "fmt": "o",
58
+ "markersize": 5,
59
+ "label": "$Data_1$",
60
+ }
61
+
62
+ marker_2 = {
63
+ "color": "tab:blue",
64
+ "markerfacecolor": "white",
65
+ "markeredgecolor": "tab:blue",
66
+ "ls": "None",
67
+ "fmt": "o",
68
+ "markersize": 5,
69
+ "label": "$Data_2$",
70
+ }
71
+
72
+ # Plot the data
73
+ plot_error_hist(
74
+ h1,
75
+ ax_main,
76
+ uncertainty_type="symmetrical",
77
+ density=False,
78
+ **marker_1,
79
+ )
80
+
81
+ plot_error_hist(
82
+ h2,
83
+ ax_main,
84
+ uncertainty_type="symmetrical",
85
+ density=False,
86
+ **marker_2,
87
+ )
88
+
89
+ # Plot the functions
90
+ plot_function(f1, x_range, ax_main, color=marker_1["color"], label="Data$_1$ fit")
91
+ plot_function(
92
+ f2, x_range, ax_main, color=marker_2["color"], linestyle="--", label="Data$_2$ fit"
93
+ )
94
+
95
+ # Plot the asymmetry comparison between the 2 histograms
96
+ plot_comparison(
97
+ h1,
98
+ h2,
99
+ ax=ax_comparison,
100
+ h1_label=r"$Data_1$",
101
+ h2_label=r"$Data_2$",
102
+ comparison="asymmetry",
103
+ comparison_ylim=(-1, 1),
104
+ )
105
+
106
+
107
+ # Define the asymmetry of the 2 functions
108
+ def asymmetry(x):
109
+ return (f1(x) - f2(x)) / (f1(x) + f2(x))
110
+
111
+
112
+ # Plot the asymmetry of the 2 functions
113
+ plot_function(asymmetry, x_range, ax_comparison, color="black")
114
+
115
+ ax_main.legend()
116
+
117
+ fig.align_ylabels()
118
+
119
+ ax_main.set_xlim(x_range)
120
+ ax_main.set_ylim(ymin=0)
121
+ ax_main.set_ylabel("Entries")
122
+ ax_main.legend()
123
+
124
+ ax_comparison.set_xlim(x_range)
125
+ ax_comparison.set_xlabel(name)
126
+
127
+ add_text("Data and fit asymmetry comparison", ax=ax_comparison, x="right")
128
+
129
+ add_luminosity(
130
+ collaboration="LMN 3", ax=ax_main, lumi="(1 + 0.3)", preliminary=True, x="left"
131
+ )
132
+
133
+ fig.savefig("asymmetry_comparison_advanced.svg", bbox_inches="tight")
@@ -0,0 +1,86 @@
1
+ """
2
+ Flatten 2D histograms
3
+ =====================
4
+
5
+ This example shows how to flatten and display 2D histograms on one dimension.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+ ###
13
+ from plothist import (
14
+ flatten_2d_hist,
15
+ get_color_palette,
16
+ make_2d_hist,
17
+ plot_data_model_comparison,
18
+ plot_hist,
19
+ )
20
+
21
+ # Define the histograms
22
+
23
+ key1 = "variable_1"
24
+ key2 = "variable_2"
25
+ # Bins [-12,0], [0,12] for variable 1,
26
+ # and bins [-12,-5], [-5,0], [0,5], [5,12] for variable 2
27
+ bins = [[-12, 0, 12], [-12, -5, 0, 5, 12]]
28
+ category = "category"
29
+
30
+ # Define datasets
31
+
32
+ signal_mask = df[category] == 7
33
+ data_mask = df[category] == 8
34
+
35
+ background_categories = [0, 1, 2, 3, 4, 5, 6]
36
+ background_categories_labels = [f"c{i}" for i in background_categories]
37
+ background_categories_colors = get_color_palette(
38
+ "cubehelix", len(background_categories)
39
+ )
40
+
41
+ background_masks = [df[category] == p for p in background_categories]
42
+
43
+ # Make histograms
44
+
45
+ data_hist = make_2d_hist(
46
+ [df[key][data_mask] for key in [key1, key2]], bins=bins, weights=1
47
+ )
48
+ background_hists = [
49
+ make_2d_hist([df[key][mask] for key in [key1, key2]], bins=bins, weights=1)
50
+ for mask in background_masks
51
+ ]
52
+ signal_hist = make_2d_hist(
53
+ [df[key][signal_mask] for key in [key1, key2]], bins=bins, weights=1
54
+ )
55
+
56
+ # Flatten the 2D histograms
57
+ data_hist = flatten_2d_hist(data_hist)
58
+ background_hists = [flatten_2d_hist(h) for h in background_hists]
59
+ signal_hist = flatten_2d_hist(signal_hist)
60
+
61
+ # Compare data and stacked histogram
62
+ fig, ax_main, ax_comparison = 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=rf"({key1} $\times$ {key2}) bin number",
68
+ ylabel="Entries",
69
+ )
70
+
71
+ plot_hist(
72
+ signal_hist,
73
+ ax=ax_main,
74
+ color="red",
75
+ label="Signal",
76
+ histtype="step",
77
+ )
78
+
79
+ for ax in [ax_main, ax_comparison]:
80
+ ax.set_xticks([i + 0.5 for i in range(8)])
81
+ ax.tick_params(axis="x", which="minor", bottom=False)
82
+ ax_comparison.set_xticklabels([str(i + 1) for i in range(8)])
83
+
84
+ ax_main.legend(ncol=3, fontsize=10, loc="upper left")
85
+
86
+ fig.savefig("model_examples_flatten2D.svg", bbox_inches="tight")
@@ -0,0 +1,4 @@
1
+ Plot simple functions
2
+ ---------------------
3
+
4
+ Gallery of images that are used in the basic function section.
@@ -0,0 +1,27 @@
1
+ """
2
+ Function
3
+ ========
4
+
5
+ Plot a 1D function with ``plot_function()``.
6
+ """
7
+
8
+ ###
9
+ from matplotlib import pyplot as plt
10
+ from scipy.stats import norm
11
+
12
+ from plothist import plot_function
13
+
14
+
15
+ # Define the gaussian function of mean=0.5 and std_dev=3
16
+ def f(x):
17
+ return 1000 * norm.pdf(x, loc=0.5, scale=3)
18
+
19
+
20
+ fig, ax = plt.subplots()
21
+
22
+ plot_function(f, range=(-10, 10), ax=ax)
23
+
24
+ ax.set_xlabel("x")
25
+ ax.set_ylabel("f(x)")
26
+
27
+ fig.savefig("fct_1d.svg", bbox_inches="tight")
@@ -0,0 +1,42 @@
1
+ """
2
+ Stacked functions
3
+ =================
4
+
5
+ Plot stacked functions using ``plot_function()``.
6
+ """
7
+
8
+ from scipy.stats import norm
9
+
10
+
11
+ # Define the gaussian function of mean=0.5 and std_dev=3
12
+ def f(x):
13
+ return 1000 * norm.pdf(x, loc=0.5, scale=3)
14
+
15
+
16
+ ###
17
+ from matplotlib import pyplot as plt
18
+
19
+ from plothist import plot_function
20
+
21
+
22
+ # Another function
23
+ def g(x):
24
+ return 1000 * norm.pdf(x, loc=2, scale=3)
25
+
26
+
27
+ fig, ax = plt.subplots()
28
+
29
+ plot_function(
30
+ [f, g],
31
+ range=(-10, 10),
32
+ ax=ax,
33
+ labels=["f1", "f2"],
34
+ stacked=True,
35
+ )
36
+
37
+ ax.set_xlabel("x")
38
+ ax.set_ylabel("f(x)")
39
+ ax.set_ylim(0)
40
+ ax.legend()
41
+
42
+ fig.savefig("fct_1d_stacked.svg", bbox_inches="tight")
@@ -0,0 +1,4 @@
1
+ Model and data
2
+ --------------
3
+
4
+ Gallery of images that are used in the plotting model and data section.
@@ -0,0 +1,103 @@
1
+ """
2
+ Data/model comparisons
3
+ ======================
4
+
5
+ All supported comparisons between data and model.
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 (
45
+ add_text,
46
+ create_comparison_figure,
47
+ plot_comparison,
48
+ plot_data_model_comparison,
49
+ set_fitting_ylabel_fontsize,
50
+ )
51
+
52
+ fig, axes = create_comparison_figure(
53
+ figsize=(6, 13),
54
+ nrows=6,
55
+ gridspec_kw={"height_ratios": [3, 1, 1, 1, 1, 1]},
56
+ hspace=0.3,
57
+ )
58
+ background_sum = sum(background_hists)
59
+
60
+ plot_data_model_comparison(
61
+ data_hist=data_hist,
62
+ stacked_components=background_hists,
63
+ stacked_labels=background_categories_labels,
64
+ stacked_colors=background_categories_colors,
65
+ xlabel="",
66
+ ylabel="Entries",
67
+ comparison="ratio",
68
+ fig=fig,
69
+ ax_main=axes[0],
70
+ ax_comparison=axes[1],
71
+ )
72
+
73
+ add_text(
74
+ r"Multiple data-model comparisons, $\mathbf{with}$ model uncertainty",
75
+ ax=axes[0],
76
+ )
77
+ add_text(r' $\mathbf{→}$ comparison = "ratio"', ax=axes[1], fontsize=13)
78
+
79
+ for k_comp, comparison in enumerate(
80
+ ["split_ratio", "pull", "relative_difference", "difference"], start=2
81
+ ):
82
+ ax_comparison = axes[k_comp]
83
+
84
+ plot_comparison(
85
+ data_hist,
86
+ background_sum,
87
+ ax=ax_comparison,
88
+ comparison=comparison,
89
+ xlabel="",
90
+ h1_label="Data",
91
+ h2_label="Pred.",
92
+ h1_uncertainty_type="asymmetrical",
93
+ )
94
+ add_text(
95
+ rf' $\mathbf{{→}}$ comparison = "{comparison}"',
96
+ ax=ax_comparison,
97
+ fontsize=13,
98
+ )
99
+ set_fitting_ylabel_fontsize(ax_comparison)
100
+
101
+ axes[-1].set_xlabel(key)
102
+
103
+ fig.savefig("model_all_comparisons.svg", bbox_inches="tight")
@@ -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")