plothist 1.3.2__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.
- plothist/__init__.py +66 -74
- plothist/_version.py +21 -0
- plothist/_version.pyi +2 -0
- plothist/comparison.py +115 -106
- 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/histogramming.py +77 -48
- plothist/plothist_style.py +61 -62
- plothist/plotters.py +280 -233
- plothist/test_helpers.py +43 -0
- plothist/variable_registry.py +62 -43
- {plothist-1.3.2.dist-info → plothist-1.5.0.dist-info}/METADATA +20 -12
- plothist-1.5.0.dist-info/RECORD +63 -0
- {plothist-1.3.2.dist-info → plothist-1.5.0.dist-info}/licenses/LICENSE +1 -1
- plothist/dummy_data.csv +0 -100001
- plothist/get_dummy_data.py +0 -17
- plothist/scripts/__init__.py +0 -2
- plothist/scripts/install_latin_modern_fonts.py +0 -145
- plothist/scripts/make_examples.py +0 -210
- plothist-1.3.2.dist-info/RECORD +0 -18
- plothist-1.3.2.dist-info/entry_points.txt +0 -3
- {plothist-1.3.2.dist-info → plothist-1.5.0.dist-info}/WHEEL +0 -0
- {plothist-1.3.2.dist-info → plothist-1.5.0.dist-info}/licenses/AUTHORS.md +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""
|
|
2
|
+
2D histogram, discrete colormap
|
|
3
|
+
===============================
|
|
4
|
+
|
|
5
|
+
Plot a 2D histogram with ``plot_2d_hist()`` with a discrete colormap.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
from plothist import make_2d_hist, plot_2d_hist
|
|
13
|
+
|
|
14
|
+
name_x = "variable_0"
|
|
15
|
+
name_y = "variable_1"
|
|
16
|
+
|
|
17
|
+
# We only take a subset of the data
|
|
18
|
+
nentries = 1000
|
|
19
|
+
|
|
20
|
+
h = make_2d_hist([df[name_x][:nentries], df[name_y][:nentries]])
|
|
21
|
+
|
|
22
|
+
###
|
|
23
|
+
from matplotlib.colors import ListedColormap
|
|
24
|
+
|
|
25
|
+
from plothist import get_color_palette
|
|
26
|
+
|
|
27
|
+
# 0 entries will be white, the rest will have one color from the plasma colormap per entry value
|
|
28
|
+
cmap = ListedColormap(
|
|
29
|
+
["white", *list(get_color_palette("plasma", int(h.values().max()) * 2 - 1))]
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
fig, ax, ax_colorbar = plot_2d_hist(
|
|
33
|
+
h, colorbar_kwargs={"label": "Entries"}, pcolormesh_kwargs={"cmap": cmap}
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
ax.set_xlabel(name_x)
|
|
37
|
+
ax.set_ylabel(name_y)
|
|
38
|
+
|
|
39
|
+
ax.set_xlim(-4.5, 4.5)
|
|
40
|
+
ax.set_ylim(-4.5, 4.5)
|
|
41
|
+
|
|
42
|
+
fig.savefig("2d_hist_simple_discrete_colormap.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""
|
|
2
|
+
2D histogram, uneven binning
|
|
3
|
+
============================
|
|
4
|
+
|
|
5
|
+
Plot a 2D histogram with uneven binning.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
###
|
|
13
|
+
from plothist import make_2d_hist, plot_2d_hist
|
|
14
|
+
|
|
15
|
+
name_x = "variable_0"
|
|
16
|
+
name_y = "variable_1"
|
|
17
|
+
# Bins [-10,0], [0,10] for variable 1,
|
|
18
|
+
# and bins [-10,-5], [-5,0], [0,5], [5,10] for variable 2
|
|
19
|
+
bins = [[-10, 0, 10], [-10, -5, 0, 5, 10]]
|
|
20
|
+
|
|
21
|
+
h = make_2d_hist([df[name_x], df[name_y]], bins=bins)
|
|
22
|
+
|
|
23
|
+
fig, ax, ax_colorbar = plot_2d_hist(h, colorbar_kwargs={"label": "Entries"})
|
|
24
|
+
|
|
25
|
+
ax.set_xlabel(name_x)
|
|
26
|
+
ax.set_ylabel(name_y)
|
|
27
|
+
|
|
28
|
+
fig.savefig("2d_hist_uneven.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
2D histogram with projections
|
|
3
|
+
==============================
|
|
4
|
+
|
|
5
|
+
Plot a 2D histogram with the two 1D projections.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
###
|
|
13
|
+
from plothist import make_2d_hist, plot_2d_hist_with_projections
|
|
14
|
+
|
|
15
|
+
name_x = "variable_0"
|
|
16
|
+
name_y = "variable_1"
|
|
17
|
+
|
|
18
|
+
h = make_2d_hist([df[name_x], df[name_y]])
|
|
19
|
+
|
|
20
|
+
(
|
|
21
|
+
fig,
|
|
22
|
+
ax_2d,
|
|
23
|
+
ax_x_projection,
|
|
24
|
+
ax_y_projection,
|
|
25
|
+
ax_colorbar,
|
|
26
|
+
) = plot_2d_hist_with_projections(
|
|
27
|
+
h,
|
|
28
|
+
xlabel="variable_0",
|
|
29
|
+
ylabel="variable_1",
|
|
30
|
+
ylabel_x_projection="Entries",
|
|
31
|
+
xlabel_y_projection="Entries",
|
|
32
|
+
offset_x_labels=False,
|
|
33
|
+
colorbar_kwargs={"label": "Entries"},
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
fig.savefig("2d_hist_with_projections.svg", bbox_inches="tight")
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Multiple comparisons
|
|
3
|
+
====================
|
|
4
|
+
|
|
5
|
+
Compare two 1D histograms using the pull and ratio methods on the same plot.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from plothist_utils import get_dummy_data
|
|
9
|
+
|
|
10
|
+
df = get_dummy_data()
|
|
11
|
+
|
|
12
|
+
###
|
|
13
|
+
from plothist import (
|
|
14
|
+
create_comparison_figure,
|
|
15
|
+
get_color_palette,
|
|
16
|
+
make_hist,
|
|
17
|
+
plot_comparison,
|
|
18
|
+
plot_error_hist,
|
|
19
|
+
plot_hist,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
name = "variable_1"
|
|
23
|
+
category = "category"
|
|
24
|
+
|
|
25
|
+
x1 = df[name][df[category] == 1]
|
|
26
|
+
x2 = df[name][df[category] == 4]
|
|
27
|
+
x3 = df[name][df[category] == 3]
|
|
28
|
+
x4 = df[name][df[category] == 5]
|
|
29
|
+
|
|
30
|
+
x_range = (-9, 9)
|
|
31
|
+
|
|
32
|
+
h1 = make_hist(x3, bins=50, range=x_range)
|
|
33
|
+
h2 = make_hist(x4, bins=50, range=x_range)
|
|
34
|
+
h3 = make_hist(x1, bins=50, range=x_range)
|
|
35
|
+
h4 = make_hist(x2, bins=50, range=x_range)
|
|
36
|
+
|
|
37
|
+
# Create the 3 axes that we need for this plot
|
|
38
|
+
fig, axes = create_comparison_figure(
|
|
39
|
+
figsize=(6, 6), nrows=3, gridspec_kw={"height_ratios": [5, 1, 1]}
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Assign each axes: 1 to plot the histograms and 2 for the comparison plots
|
|
43
|
+
ax_main, ax1_comparison, ax2_comparison = axes
|
|
44
|
+
|
|
45
|
+
# Get the red and the blue from the default color cycle
|
|
46
|
+
colors = get_color_palette("ggplot", 2)
|
|
47
|
+
|
|
48
|
+
# Here, we use step as a histtype to only draw the line
|
|
49
|
+
plot_hist(h1, label="Train A", ax=ax_main, histtype="step", linewidth=1.2, density=True)
|
|
50
|
+
plot_hist(h3, label="Train B", ax=ax_main, histtype="step", linewidth=1.2, density=True)
|
|
51
|
+
# And then, to make the plot easier to read, we redraw them with stepfilled, which add color below the line
|
|
52
|
+
plot_hist(
|
|
53
|
+
h1, ax=ax_main, histtype="stepfilled", color=colors[0], alpha=0.2, density=True
|
|
54
|
+
)
|
|
55
|
+
plot_hist(
|
|
56
|
+
h3, ax=ax_main, histtype="stepfilled", color=colors[1], alpha=0.2, density=True
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# We plot 2 additional histograms with point style
|
|
60
|
+
plot_error_hist(h2, label="Test A", ax=ax_main, color="blue", density=True)
|
|
61
|
+
plot_error_hist(h4, label="Test B", ax=ax_main, color="red", density=True)
|
|
62
|
+
|
|
63
|
+
# First comparison is using pulls. We also change the color of the bars to make the plot easier to read
|
|
64
|
+
plot_comparison(
|
|
65
|
+
h2, h1, ax=ax1_comparison, comparison="pull", color=colors[0], alpha=0.7
|
|
66
|
+
)
|
|
67
|
+
# Second comparison is using the default "ratio". Same strategy as pulls
|
|
68
|
+
plot_comparison(h4, h3, ax=ax2_comparison, color=colors[1], alpha=0.7)
|
|
69
|
+
|
|
70
|
+
# Harmonize the range of each axes
|
|
71
|
+
ax_main.set_xlim(x_range)
|
|
72
|
+
ax1_comparison.set_xlim(x_range)
|
|
73
|
+
ax2_comparison.set_xlim(x_range)
|
|
74
|
+
|
|
75
|
+
# Set the labels for the different axes
|
|
76
|
+
ax_main.set_ylabel("Entry density")
|
|
77
|
+
ax1_comparison.set_ylabel("$Pull_{A}$")
|
|
78
|
+
ax2_comparison.set_ylabel("$Ratio_{B}$")
|
|
79
|
+
ax2_comparison.set_xlabel("Variable [unit]")
|
|
80
|
+
|
|
81
|
+
# Add the legend
|
|
82
|
+
ax_main.legend(loc="upper left")
|
|
83
|
+
|
|
84
|
+
# Align the ylabels
|
|
85
|
+
fig.align_ylabels()
|
|
86
|
+
|
|
87
|
+
fig.savefig("1d_comparison_advanced.svg", bbox_inches="tight")
|
|
@@ -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,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,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,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")
|