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,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
+ )
@@ -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,4 @@
1
+ Utilities
2
+ ---------
3
+
4
+ Gallery of images that are used in the utility section.
@@ -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")
@@ -0,0 +1,94 @@
1
+ """
2
+ Color palettes in stacked histograms
3
+ ====================================
4
+
5
+ Examples of color palettes in stacked histograms.
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 add_text, get_color_palette, make_hist, plot_error_hist, plot_model
15
+
16
+ # Define the histograms
17
+ key = "variable_1"
18
+ xrange = (-8, 10)
19
+ category = "category"
20
+
21
+ # Define masks
22
+ signal_mask = df[category] == 7
23
+ data_mask = df[category] == 8
24
+
25
+ background_categories = [0, 1, 2, 3, 4, 5]
26
+ background_categories_labels = [f"c{i}" for i in background_categories]
27
+ background_masks = [df[category] == p for p in background_categories]
28
+
29
+ # Make histograms
30
+ data_hist = make_hist(df[key][data_mask], bins=50, range=xrange, weights=1)
31
+ background_hists = [
32
+ make_hist(df[key][mask], bins=50, range=xrange, weights=1)
33
+ for mask in background_masks
34
+ ]
35
+ signal_hist = make_hist(df[key][signal_mask], bins=50, range=xrange, weights=1)
36
+
37
+ # Optional: scale to data
38
+ background_scaling_factor = data_hist.sum().value / sum(background_hists).sum().value
39
+ background_hists = [background_scaling_factor * h for h in background_hists]
40
+
41
+ signal_scaling_factor = data_hist.sum().value / signal_hist.sum().value
42
+ signal_hist *= signal_scaling_factor
43
+
44
+
45
+ # Plotting section
46
+ nrows, ncols = 2, 2
47
+
48
+ fig, axes = plt.subplots(
49
+ nrows=nrows,
50
+ ncols=ncols,
51
+ figsize=(12, 10),
52
+ )
53
+ fig.subplots_adjust(hspace=0.25)
54
+
55
+ cmap_list = ["viridis", "ggplot", "coolwarm", "YlGnBu_r"]
56
+ ax_coords = [(x, y) for x in range(nrows) for y in range(ncols)]
57
+
58
+ for k, cmap_name in enumerate(cmap_list):
59
+ background_categories_colors = get_color_palette(
60
+ cmap_name, len(background_categories)
61
+ )
62
+
63
+ ax = axes[ax_coords[k]]
64
+
65
+ plot_model(
66
+ stacked_components=background_hists,
67
+ stacked_labels=background_categories_labels,
68
+ stacked_colors=background_categories_colors,
69
+ xlabel=key,
70
+ ylabel="Entries",
71
+ model_uncertainty=False,
72
+ fig=fig,
73
+ ax=ax,
74
+ )
75
+
76
+ plot_error_hist(
77
+ data_hist,
78
+ color="black",
79
+ label="Data",
80
+ ax=ax,
81
+ )
82
+
83
+ ax.set_xlim(xrange)
84
+ ax.legend()
85
+
86
+ cmap_name = cmap_name.replace("_", r"\_")
87
+ add_text(
88
+ rf"$\mathrm{{\mathbf{{cmap = {cmap_name}}}}}$", x="right", fontsize=12, ax=ax
89
+ )
90
+
91
+ fig.savefig(
92
+ "color_palette_hists.svg",
93
+ bbox_inches="tight",
94
+ )
@@ -0,0 +1,100 @@
1
+ """
2
+ Color palettes
3
+ ==============
4
+
5
+ Examples of color palettes.
6
+ """
7
+
8
+ import matplotlib.colors as mcolors
9
+ import matplotlib.pyplot as plt
10
+ import numpy as np
11
+ from matplotlib import patches
12
+
13
+ from plothist import get_color_palette
14
+
15
+
16
+ def create_palette_plot(colors, fig_name, add_text=False, add_black_border=False):
17
+ ncolors = len(colors)
18
+
19
+ # Create a figure and axis
20
+ fig, ax = plt.subplots(figsize=(ncolors, 1))
21
+
22
+ # Plot the colored squares with small spacing
23
+ square_size = 1
24
+ spacing = 0.1
25
+ x = 0
26
+
27
+ for color in colors:
28
+ rect = patches.Rectangle((x, 0), square_size, square_size, color=color)
29
+ ax.add_patch(rect)
30
+ x += square_size + spacing
31
+
32
+ if add_text:
33
+ # Add text displaying the color value
34
+ ax.text(
35
+ x - (square_size + spacing) / 1.81,
36
+ -0.18,
37
+ mcolors.rgb2hex(color).upper(),
38
+ ha="center",
39
+ fontsize=10,
40
+ )
41
+
42
+ # Set the x-axis limits and show the ticks
43
+ ax.set_xlim(-0.5, x - spacing)
44
+ ax.set_xticks(np.arange(0, x, square_size + spacing))
45
+ ax.set_xticklabels(np.arange(1, ncolors + 1), fontsize=8)
46
+ ax.set_xticklabels([]) # Remove the x-tick labels
47
+
48
+ # Set the y-axis ticks and labels
49
+ ax.set_yticks([])
50
+ ax.set_yticklabels([])
51
+
52
+ # Remove the borders around the plot
53
+ ax.spines["top"].set_visible(False)
54
+ ax.spines["bottom"].set_visible(False)
55
+ ax.spines["left"].set_visible(False)
56
+ ax.spines["right"].set_visible(False)
57
+
58
+ # Remove the x-label and y-label
59
+ ax.set_xlabel("")
60
+ ax.set_ylabel("")
61
+
62
+ if add_black_border:
63
+ # Add a black border rectangle
64
+ border_rect = patches.Rectangle(
65
+ (0, 0),
66
+ x - spacing,
67
+ square_size,
68
+ edgecolor="black",
69
+ facecolor="none",
70
+ linewidth=1,
71
+ )
72
+ ax.add_patch(border_rect)
73
+
74
+ # Adjust the padding and remove extra whitespace
75
+ plt.margins(0)
76
+ plt.gca().set_axis_off()
77
+ plt.subplots_adjust(left=0.05, right=0.95)
78
+
79
+ plt.savefig(fig_name, bbox_inches="tight")
80
+
81
+ return fig
82
+
83
+
84
+ figs = []
85
+
86
+ ncolors = 7
87
+
88
+ ncolors_ggplot = 7 if ncolors > 7 else ncolors
89
+ colors = get_color_palette("ggplot", ncolors_ggplot)
90
+ figs.append(
91
+ create_palette_plot(colors, fig_name="usage_style_cycle.svg", add_text=True)
92
+ )
93
+
94
+ colors = get_color_palette("cubehelix", ncolors)
95
+ figs.append(create_palette_plot(colors, fig_name="usage_cubehelix.svg"))
96
+
97
+ cmap_list = ["viridis", "coolwarm", "YlGnBu_r"]
98
+ for cmap_name in cmap_list:
99
+ colors = get_color_palette(cmap_name, ncolors)
100
+ figs.append(create_palette_plot(colors, fig_name=f"usage_{cmap_name}_palette.svg"))