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,37 @@
1
+ """
2
+ Ratio, split errors
3
+ ===================
4
+
5
+ Compare two 1D histograms using the ratio [h1/h2] method and split the errors.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+ name = "variable_1"
13
+
14
+ x1 = df[name][df["category"] == 2]
15
+ x2 = df[name][df["category"] == 3]
16
+
17
+ x_range = (min(*x1, *x2), max(*x1, *x2))
18
+
19
+ from plothist import make_hist
20
+
21
+ h1 = make_hist(x1, bins=50, range=x_range)
22
+ h2 = make_hist(x2, bins=50, range=x_range)
23
+
24
+ ###
25
+ from plothist import plot_two_hist_comparison
26
+
27
+ fig, ax_main, ax_comparison = plot_two_hist_comparison(
28
+ h1,
29
+ h2,
30
+ xlabel=name,
31
+ ylabel="Entries",
32
+ h1_label=r"$\mathbf{h1}$",
33
+ h2_label=r"$\mathbf{h2}$",
34
+ comparison="split_ratio",
35
+ )
36
+
37
+ fig.savefig("1d_comparison_split_ratio.svg", bbox_inches="tight")
@@ -0,0 +1,38 @@
1
+ """
2
+ Multiple histograms
3
+ ===================
4
+
5
+ Plot multiple histograms on the same plot using ``plot_hist()``.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+ ###
13
+ import matplotlib.pyplot as plt
14
+
15
+ from plothist import make_hist, plot_hist
16
+
17
+ name = "variable_1"
18
+ category = "category"
19
+
20
+ x1 = df[name][df[category] == 1]
21
+ x2 = df[name][df[category] == 2]
22
+
23
+ x_range = (min(*x1, *x2), max(*x1, *x2))
24
+
25
+ h1 = make_hist(x1, bins=50, range=x_range)
26
+ h2 = make_hist(x2, bins=50, range=x_range)
27
+
28
+ fig, ax = plt.subplots()
29
+
30
+ plot_hist(h1, ax=ax, histtype="step", linewidth=1.2, label="c1")
31
+ plot_hist(h2, ax=ax, histtype="step", linewidth=1.2, label="c2")
32
+
33
+ ax.set_xlabel(name)
34
+ ax.set_ylabel("Entries")
35
+ ax.set_xlim(x_range)
36
+ ax.legend()
37
+
38
+ fig.savefig("1d_elt1.svg", bbox_inches="tight")
@@ -0,0 +1,45 @@
1
+ """
2
+ Stack histograms
3
+ ================
4
+
5
+ Stack two 1D histograms with ``plot_hist()``.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+ import matplotlib.pyplot as plt
13
+
14
+ from plothist import make_hist, plot_hist
15
+
16
+ name = "variable_1"
17
+ category = "category"
18
+
19
+ x1 = df[name][df[category] == 1]
20
+ x2 = df[name][df[category] == 2]
21
+
22
+ x_range = (min(*x1, *x2), max(*x1, *x2))
23
+
24
+ h1 = make_hist(x1, bins=50, range=x_range)
25
+ h2 = make_hist(x2, bins=50, range=x_range)
26
+
27
+ ###
28
+ fig, ax = plt.subplots()
29
+
30
+ plot_hist(
31
+ [h1, h2],
32
+ label=["c1", "c2"],
33
+ ax=ax,
34
+ edgecolor="black",
35
+ linewidth=0.5,
36
+ histtype="stepfilled",
37
+ stacked=True,
38
+ )
39
+
40
+ ax.set_xlabel(name)
41
+ ax.set_ylabel("Entries")
42
+ ax.set_xlim(x_range)
43
+ ax.legend()
44
+
45
+ fig.savefig("1d_elt1_stacked.svg", bbox_inches="tight")
@@ -0,0 +1,33 @@
1
+ """
2
+ Histogram with uncertainties
3
+ ============================
4
+
5
+ Plot a 1D histogram with error bars using ``plot_error_hist()``.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+ ###
13
+ import matplotlib.pyplot as plt
14
+
15
+ from plothist import make_hist, plot_error_hist
16
+
17
+ name = "variable_1"
18
+ category = "category"
19
+
20
+ x1 = df[name][df[category] == 3]
21
+
22
+ h1 = make_hist(x1)
23
+
24
+ fig, ax = plt.subplots()
25
+
26
+ plot_error_hist(h1, ax=ax, color="black", label="$h1_{err}$")
27
+
28
+ ax.set_xlabel(name)
29
+ ax.set_ylabel("Entries")
30
+ ax.set_ylim(ymin=0)
31
+ ax.legend()
32
+
33
+ fig.savefig("1d_elt2.svg", bbox_inches="tight")
@@ -0,0 +1,28 @@
1
+ """
2
+ Histogram
3
+ =========
4
+
5
+ Plot a 1D histogram with ``plot_hist()``.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+ ###
13
+ import matplotlib.pyplot as plt
14
+
15
+ from plothist import make_hist, plot_hist
16
+
17
+ name = "variable_0"
18
+
19
+ fig, ax = plt.subplots()
20
+
21
+ h = make_hist(df[name])
22
+
23
+ plot_hist(h, ax=ax)
24
+
25
+ ax.set_xlabel(name)
26
+ ax.set_ylabel("Entries")
27
+
28
+ fig.savefig("1d_hist_simple.svg", bbox_inches="tight")
@@ -0,0 +1,41 @@
1
+ """
2
+ Integer categories
3
+ ==================
4
+
5
+ Plot a 1D histogram with integer categories.
6
+ """
7
+
8
+ ###
9
+ import boost_histogram as bh
10
+ import matplotlib.pyplot as plt
11
+
12
+ from plothist import plot_hist
13
+
14
+ # 3 integer categories
15
+ int_categories = [-10, 5, 72]
16
+
17
+ # Integer category axis with 3 bins
18
+ axis = bh.axis.IntCategory(categories=int_categories)
19
+
20
+ # 6 data points,
21
+ data = [-10, -10, 5, 72, 72, 72]
22
+
23
+ # Create and fill the histogram
24
+ h = bh.Histogram(axis, storage=bh.storage.Weight())
25
+ h.fill(data)
26
+
27
+ # Plot the histogram
28
+ fig, ax = plt.subplots()
29
+
30
+ plot_hist(h, ax=ax)
31
+
32
+ # Set the x-ticks to the middle of the bins and label them
33
+ ax.set_xticks([i + 0.5 for i in range(len(int_categories))])
34
+ ax.set_xticklabels(int_categories)
35
+ ax.minorticks_off()
36
+
37
+ ax.set_xlabel("Integer Category")
38
+ ax.set_ylabel("Entries")
39
+ ax.set_xlim(0, len(int_categories))
40
+
41
+ fig.savefig("1d_int_category.svg", bbox_inches="tight")
@@ -0,0 +1,33 @@
1
+ """
2
+ Mean histogram (profile)
3
+ ========================
4
+
5
+ Plot a 1D mean histogram (profile).
6
+ """
7
+
8
+ ###
9
+ import boost_histogram as bh
10
+ import matplotlib.pyplot as plt
11
+
12
+ from plothist import plot_error_hist
13
+
14
+ # Regular axis with 3 bins from -1 to 1
15
+ axis = bh.axis.Regular(3, -1, 1)
16
+
17
+ # 6 data points, two in each bin
18
+ data = [-0.5, -0.5, 0.0, 0.0, 0.5, 0.5]
19
+ sample = [0, 100, 40, 60, 0, 20]
20
+ weights = [1, 1, 1, 1, 1, 1]
21
+
22
+ h = bh.Histogram(axis, storage=bh.storage.WeightedMean())
23
+ h.fill(data, weight=weights, sample=sample)
24
+
25
+ fig, ax = plt.subplots()
26
+
27
+ plot_error_hist(h, ax=ax)
28
+
29
+ ax.set_xlabel("Variable")
30
+ ax.set_ylabel("Mean")
31
+ ax.set_xlim(-1, 1)
32
+
33
+ fig.savefig("1d_profile.svg", bbox_inches="tight")
@@ -0,0 +1,58 @@
1
+ """
2
+ Side-by-side categories
3
+ =======================
4
+
5
+ Plot multiple 1D histograms with categories side by side.
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(8311311)
16
+
17
+ # String categories
18
+ categories = ["A", "B", "C"]
19
+
20
+ # Axis with the 3 bins
21
+ axis = bh.axis.StrCategory(categories=categories)
22
+
23
+ ## Works also with integers
24
+ # categories = [-5, 10, 137]
25
+ # axis = bh.axis.IntCategory(categories=categories)
26
+
27
+ # Generate data for 3 histograms
28
+ data = [
29
+ rng.choice(categories, 20),
30
+ rng.choice(categories, 30),
31
+ rng.choice(categories, 40),
32
+ ]
33
+
34
+ # Create and fill the histograms
35
+ histos = [bh.Histogram(axis, storage=bh.storage.Weight()) for _ in range(len(data))]
36
+ histos = [histo.fill(data[i]) for i, histo in enumerate(histos)]
37
+
38
+ labels = [f"$h_{{{i}}}$" for i in range(len(histos))]
39
+
40
+ # Plot the histogram
41
+ fig, ax = plt.subplots()
42
+
43
+ # 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.
44
+ plot_hist(histos, ax=ax, label=labels)
45
+
46
+ # Set the x-ticks to the middle of the bins and label them
47
+ ax.set_xlim(0, len(categories))
48
+ ax.set_xticks([i + 0.5 for i in range(len(categories))])
49
+ ax.set_xticklabels(categories)
50
+ ax.minorticks_off()
51
+ # Get nice looking y-axis ticks
52
+ ax.set_ylim(top=int(np.max([np.max(histo.values()) for histo in histos]) * 1.5))
53
+
54
+ ax.set_xlabel("Category")
55
+ ax.set_ylabel("Entries")
56
+ ax.legend()
57
+
58
+ fig.savefig("1d_side_by_side.svg", bbox_inches="tight")
@@ -0,0 +1,41 @@
1
+ """
2
+ String categories
3
+ =================
4
+
5
+ Plot a 1D histogram with string categories.
6
+ """
7
+
8
+ ###
9
+ import boost_histogram as bh
10
+ import matplotlib.pyplot as plt
11
+
12
+ from plothist import plot_hist
13
+
14
+ # 3 str categories
15
+ str_categories = ["a", "b", "c"]
16
+
17
+ # String category axis with 3 bins
18
+ axis = bh.axis.StrCategory(categories=str_categories)
19
+
20
+ # 6 data points,
21
+ data = ["a", "a", "a", "b", "b", "c"]
22
+
23
+ # Create and fill the histogram
24
+ h = bh.Histogram(axis, storage=bh.storage.Weight())
25
+ h.fill(data)
26
+
27
+ # Plot the histogram
28
+ fig, ax = plt.subplots()
29
+
30
+ plot_hist(h, ax=ax)
31
+
32
+ # Set the x-ticks to the middle of the bins and label them
33
+ ax.set_xticks([i + 0.5 for i in range(len(str_categories))])
34
+ ax.set_xticklabels(str_categories)
35
+ ax.minorticks_off()
36
+
37
+ ax.set_xlabel("String Category")
38
+ ax.set_ylabel("Entries")
39
+ ax.set_xlim(0, len(str_categories))
40
+
41
+ fig.savefig("1d_str_category.svg", bbox_inches="tight")
@@ -0,0 +1,4 @@
1
+ Plot 1D histograms
2
+ ------------------
3
+
4
+ Gallery of images that are used in the 1D histogram section.
@@ -0,0 +1,65 @@
1
+ """
2
+ 2D histograms
3
+ =============
4
+
5
+ Plot multiple 2D histograms with the variable registry.
6
+ """
7
+
8
+ from plothist_utils import get_dummy_data
9
+
10
+ df = get_dummy_data()
11
+
12
+
13
+ ###
14
+ import os
15
+ import time
16
+ from itertools import combinations
17
+
18
+ from plothist import (
19
+ create_variable_registry,
20
+ get_variable_from_registry,
21
+ make_2d_hist,
22
+ plot_2d_hist,
23
+ update_variable_registry_ranges,
24
+ )
25
+
26
+ # No need to redo this step if the registry was already created before
27
+ variable_keys = ["variable_0", "variable_1", "variable_2"]
28
+ unique_id = str(int(time.time() * 1000))[-8:] # unique ID based on current time
29
+ temporary_registry_path = f"./_temporary_variable_registry_{unique_id}.yaml"
30
+ create_variable_registry(variable_keys, path=temporary_registry_path)
31
+ update_variable_registry_ranges(df, variable_keys, path=temporary_registry_path)
32
+
33
+ # Get all the correlation plot between the variables
34
+ variable_keys_combinations = list(combinations(variable_keys, 2))
35
+
36
+ figs = []
37
+
38
+ for variable_keys_combination in variable_keys_combinations:
39
+ variable0 = get_variable_from_registry(
40
+ variable_keys_combination[0], path=temporary_registry_path
41
+ )
42
+ variable1 = get_variable_from_registry(
43
+ variable_keys_combination[1], path=temporary_registry_path
44
+ )
45
+
46
+ h = make_2d_hist(
47
+ [df[variable0["name"]], df[variable1["name"]]],
48
+ bins=(variable0["bins"], variable1["bins"]),
49
+ range=(variable0["range"], variable1["range"]),
50
+ )
51
+
52
+ fig, ax, ax_colorbar = plot_2d_hist(h, colorbar_kwargs={"label": "Entries"})
53
+
54
+ ax.set_xlabel(variable0["name"])
55
+ ax.set_ylabel(variable1["name"])
56
+
57
+ ax.set_xlim(variable0["range"])
58
+ ax.set_ylim(variable1["range"])
59
+
60
+ figs.append(fig)
61
+
62
+ for i, fig in enumerate(figs):
63
+ fig.savefig(f"2d_hist_correlations_{i}.svg", bbox_inches="tight")
64
+
65
+ os.remove(temporary_registry_path)
@@ -0,0 +1,28 @@
1
+ """
2
+ 2D histogram
3
+ ============
4
+
5
+ Plot a 2D histogram with ``plot_2d_hist()``.
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
+
18
+ h = make_2d_hist([df[name_x], df[name_y]], bins=[10, 10])
19
+
20
+ fig, ax, ax_colorbar = plot_2d_hist(h, colorbar_kwargs={"label": "Entries"})
21
+
22
+ ax.set_xlabel(name_x)
23
+ ax.set_ylabel(name_y)
24
+
25
+ ax.set_xlim(-9, 9)
26
+ ax.set_ylim(-9, 9)
27
+
28
+ fig.savefig("2d_hist_simple.svg", bbox_inches="tight")
@@ -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,4 @@
1
+ Plot 2D histograms
2
+ ------------------
3
+
4
+ Gallery of images that are used in the section about plotting 2D histograms.
@@ -0,0 +1,7 @@
1
+ Example gallery
2
+ ===============
3
+
4
+ Gallery of images that are used in the doc.
5
+
6
+ .. note::
7
+ Click on an image to see the source code that generates it.
@@ -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")