plothist 1.4.0__py3-none-any.whl → 1.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. plothist/__init__.py +5 -5
  2. plothist/_version.py +2 -2
  3. plothist/comparison.py +170 -120
  4. plothist/examples/1d_hist/1d_comparison_asymmetry.py +37 -0
  5. plothist/examples/1d_hist/1d_comparison_difference.py +40 -0
  6. plothist/examples/1d_hist/1d_comparison_efficiency.py +37 -0
  7. plothist/examples/1d_hist/1d_comparison_only_efficiency.py +33 -0
  8. plothist/examples/1d_hist/1d_comparison_pull.py +37 -0
  9. plothist/examples/1d_hist/1d_comparison_ratio.py +37 -0
  10. plothist/examples/1d_hist/1d_comparison_relative_difference.py +37 -0
  11. plothist/examples/1d_hist/1d_comparison_split_ratio.py +37 -0
  12. plothist/examples/1d_hist/1d_elt1.py +38 -0
  13. plothist/examples/1d_hist/1d_elt1_stacked.py +45 -0
  14. plothist/examples/1d_hist/1d_elt2.py +33 -0
  15. plothist/examples/1d_hist/1d_hist_simple.py +28 -0
  16. plothist/examples/1d_hist/1d_int_category.py +41 -0
  17. plothist/examples/1d_hist/1d_profile.py +33 -0
  18. plothist/examples/1d_hist/1d_side_by_side.py +58 -0
  19. plothist/examples/1d_hist/1d_str_category.py +41 -0
  20. plothist/examples/1d_hist/README.rst +4 -0
  21. plothist/examples/2d_hist/2d_hist_correlations.py +65 -0
  22. plothist/examples/2d_hist/2d_hist_simple.py +28 -0
  23. plothist/examples/2d_hist/2d_hist_simple_discrete_colormap.py +42 -0
  24. plothist/examples/2d_hist/2d_hist_uneven.py +28 -0
  25. plothist/examples/2d_hist/2d_hist_with_projections.py +36 -0
  26. plothist/examples/2d_hist/README.rst +4 -0
  27. plothist/examples/README.rst +7 -0
  28. plothist/examples/advanced/1d_comparison_advanced.py +87 -0
  29. plothist/examples/advanced/1d_side_by_side_with_numbers.py +81 -0
  30. plothist/examples/advanced/README.rst +4 -0
  31. plothist/examples/advanced/asymmetry_comparison_advanced.py +133 -0
  32. plothist/examples/advanced/model_examples_flatten2D.py +86 -0
  33. plothist/examples/func_1d/README.rst +4 -0
  34. plothist/examples/func_1d/fct_1d.py +27 -0
  35. plothist/examples/func_1d/fct_1d_stacked.py +42 -0
  36. plothist/examples/model_ex/README.rst +4 -0
  37. plothist/examples/model_ex/model_all_comparisons.py +103 -0
  38. plothist/examples/model_ex/model_all_comparisons_no_model_unc.py +115 -0
  39. plothist/examples/model_ex/model_examples_pull.py +56 -0
  40. plothist/examples/model_ex/model_examples_pull_no_model_unc.py +59 -0
  41. plothist/examples/model_ex/model_examples_stacked.py +74 -0
  42. plothist/examples/model_ex/model_examples_stacked_unstacked.py +60 -0
  43. plothist/examples/model_ex/model_examples_unstacked.py +57 -0
  44. plothist/examples/model_ex/model_with_stacked_and_unstacked_function_components.py +50 -0
  45. plothist/examples/model_ex/model_with_stacked_and_unstacked_histograms_components.py +69 -0
  46. plothist/examples/model_ex/ratio_data_vs_model_with_stacked_and_unstacked_function_components.py +61 -0
  47. plothist/examples/utility/README.rst +4 -0
  48. plothist/examples/utility/add_text_example.py +39 -0
  49. plothist/examples/utility/color_palette_hists.py +94 -0
  50. plothist/examples/utility/color_palette_squares.py +100 -0
  51. plothist/examples/utility/matplotlib_vs_plothist_style.py +63 -0
  52. plothist/examples/utility/uncertainty_types.py +120 -0
  53. plothist/histogramming.py +60 -39
  54. plothist/plothist_style.py +56 -59
  55. plothist/plotters.py +210 -195
  56. plothist/test_helpers.py +43 -0
  57. plothist/variable_registry.py +46 -30
  58. {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/METADATA +1 -1
  59. plothist-1.6.0.dist-info/RECORD +64 -0
  60. plothist/scripts/__init__.py +0 -3
  61. plothist/scripts/make_examples.py +0 -209
  62. plothist-1.4.0.dist-info/RECORD +0 -17
  63. plothist-1.4.0.dist-info/entry_points.txt +0 -2
  64. {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/WHEEL +0 -0
  65. {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/licenses/AUTHORS.md +0 -0
  66. {plothist-1.4.0.dist-info → plothist-1.6.0.dist-info}/licenses/LICENSE +0 -0
@@ -1,13 +1,11 @@
1
1
  from __future__ import annotations
2
2
 
3
- from importlib.resources import files
4
-
5
3
  import matplotlib as mpl
6
4
  import matplotlib.pyplot as plt
7
5
  import numpy as np
8
6
 
9
7
 
10
- def set_style(style="default"):
8
+ def set_style(style: str = "default") -> None:
11
9
  """
12
10
  Set the plothist style.
13
11
 
@@ -32,22 +30,21 @@ def set_style(style="default"):
32
30
  available_styles = ["default"]
33
31
 
34
32
  if style in available_styles:
35
- style_file = files("plothist").joinpath(f"{style}_style.mplstyle")
36
- plt.style.use(style_file)
33
+ plt.style.use(f"plothist.{style}_style")
37
34
  else:
38
35
  raise ValueError(f"{style} not in the available styles: {available_styles}")
39
36
 
40
37
 
41
38
  def cubehelix_palette(
42
- ncolors=7,
43
- start=1.5,
44
- rotation=1.5,
45
- gamma=1.0,
46
- hue=0.8,
47
- lightest=0.8,
48
- darkest=0.3,
49
- reverse=True,
50
- ):
39
+ ncolors: int = 7,
40
+ start: float = 1.5,
41
+ rotation: float = 1.5,
42
+ gamma: float = 1.0,
43
+ hue: float = 0.8,
44
+ lightest: float = 0.8,
45
+ darkest: float = 0.3,
46
+ reverse: bool = True,
47
+ ) -> list[tuple[float, float, float]]:
51
48
  """
52
49
  Make a sequential palette from the cubehelix system, in which the perceived brightness is linearly increasing.
53
50
  This code is adapted from seaborn, which implements equation (2) of reference [1] below.
@@ -75,10 +72,9 @@ def cubehelix_palette(
75
72
 
76
73
  Returns
77
74
  -------
78
- list of RGB tuples
75
+ list[tuple[float, float, float]]
79
76
  The generated palette of colors represented as a list of RGB tuples.
80
77
 
81
-
82
78
  References
83
79
  ----------
84
80
  [1] Green, D. A. (2011). "A colour scheme for the display of astronomical
@@ -116,10 +112,12 @@ def cubehelix_palette(
116
112
  pal = cmap(x)[:, :3].tolist()
117
113
  if reverse:
118
114
  pal = pal[::-1]
119
- return pal
115
+ return [tuple(c) for c in pal]
120
116
 
121
117
 
122
- def get_color_palette(cmap, N):
118
+ def get_color_palette(
119
+ cmap: str, N: int
120
+ ) -> list[str] | list[tuple[float, float, float]]:
123
121
  """
124
122
  Get N different colors from a chosen colormap.
125
123
 
@@ -132,8 +130,9 @@ def get_color_palette(cmap, N):
132
130
 
133
131
  Returns
134
132
  -------
135
- list
136
- A list of RGB color tuples sampled from the colormap.
133
+ list[str] or list[tuple[float, float, float]]
134
+ A list of colors. If "ggplot" is selected, returns a list of hex color strings.
135
+ Otherwise, returns a list of RGB color tuples.
137
136
 
138
137
  References
139
138
  ----------
@@ -171,10 +170,10 @@ def get_color_palette(cmap, N):
171
170
  )
172
171
 
173
172
  plt_cmap = plt.get_cmap(cmap)
174
- return plt_cmap(np.linspace(0, 1, N))
173
+ return [tuple(k) for k in plt_cmap(np.linspace(0, 1, N))]
175
174
 
176
175
 
177
- def set_fitting_ylabel_fontsize(ax):
176
+ def set_fitting_ylabel_fontsize(ax: plt.Axes) -> float:
178
177
  """
179
178
  Get the suitable font size for a ylabel text that fits within the plot's y-axis limits.
180
179
 
@@ -190,9 +189,8 @@ def set_fitting_ylabel_fontsize(ax):
190
189
  """
191
190
  ylabel_fontsize = ax.yaxis.get_label().get_fontsize()
192
191
 
193
- # Check if renderer is available
194
- if ax.figure.canvas.get_renderer() is None:
195
- ax.figure.canvas.draw()
192
+ # Force renderer to be initialized
193
+ ax.figure.canvas.draw()
196
194
 
197
195
  while (
198
196
  ax.yaxis.get_label()
@@ -214,14 +212,14 @@ def set_fitting_ylabel_fontsize(ax):
214
212
 
215
213
 
216
214
  def add_text(
217
- text,
218
- x="left",
219
- y="top",
220
- fontsize=12,
221
- white_background=False,
222
- ax=None,
215
+ text: str,
216
+ x: float | str = "left",
217
+ y: float | str = "top",
218
+ fontsize: int = 12,
219
+ white_background: bool = False,
220
+ ax: plt.Axes | None = None,
223
221
  **kwargs,
224
- ):
222
+ ) -> None:
225
223
  """
226
224
  Add text to an axis.
227
225
 
@@ -229,9 +227,9 @@ def add_text(
229
227
  ----------
230
228
  text : str
231
229
  The text to add.
232
- x : float, optional
230
+ x : float | str, optional
233
231
  Horizontal position of the text in unit of the normalized x-axis length. The default is value "left", which is an alias for 0.0. Other aliases are "right", "left_in", "right_in", "right_out".
234
- y : float, optional
232
+ y : float | str, optional
235
233
  Vertical position of the text in unit of the normalized y-axis length. The default is value "top", which is an alias for 1.01. Other aliases are "top_in", "bottom_in", "top_out"="top", "bottom_out"="bottom".
236
234
  fontsize : int, optional
237
235
  Font size, by default 12.
@@ -279,14 +277,14 @@ def add_text(
279
277
  }
280
278
 
281
279
  if isinstance(x, str):
282
- x = x_values.get(x)
283
- if x is None:
284
- raise ValueError(f"{x} not a float or a valid position")
280
+ if x not in x_values:
281
+ raise ValueError(f"{x!r} is not a valid x position.")
282
+ x = x_values[x]
285
283
 
286
284
  if isinstance(y, str):
287
- y = y_values.get(y)
288
- if y is None:
289
- raise ValueError(f"{y} not a float or a valid position")
285
+ if y not in y_values:
286
+ raise ValueError(f"{y!r} is not a valid y position.")
287
+ y = y_values[y]
290
288
 
291
289
  t = ax.text(
292
290
  x,
@@ -303,19 +301,19 @@ def add_text(
303
301
 
304
302
 
305
303
  def add_luminosity(
306
- collaboration,
307
- x="right",
308
- y="top",
309
- fontsize=12,
310
- is_data=True,
311
- lumi="",
312
- lumi_unit="fb",
313
- preliminary=False,
314
- two_lines=False,
315
- white_background=False,
316
- ax=None,
304
+ collaboration: str,
305
+ x: float | str = "right",
306
+ y: float | str = "top",
307
+ fontsize: int = 12,
308
+ is_data: bool = True,
309
+ lumi: int | str = "",
310
+ lumi_unit: str = "fb",
311
+ preliminary: bool = False,
312
+ two_lines: bool = False,
313
+ white_background: bool = False,
314
+ ax: plt.Axes | None = None,
317
315
  **kwargs,
318
- ):
316
+ ) -> None:
319
317
  """
320
318
  Add the collaboration name and the integrated luminosity (or "Simulation").
321
319
 
@@ -323,17 +321,17 @@ def add_luminosity(
323
321
  ----------
324
322
  collaboration : str
325
323
  Collaboration name.
326
- x : float, optional
324
+ x : float | str, optional
327
325
  Horizontal position of the text in unit of the normalized x-axis length. The default is value "right", which is an alias for 1.0. Can take other aliases such as "left", "left_in", "right_in", "right_out".
328
- y : float, optional
326
+ y : float | str, optional
329
327
  Vertical position of the text in unit of the normalized y-axis length. The default is value "top", which is an alias for 1.01. Can take other aliases such as "top_in", "bottom_in", "top_out"="top", "bottom_out"="bottom".
330
328
  fontsize : int, optional
331
329
  Font size, by default 12.
332
330
  is_data : bool, optional
333
331
  If True, plot integrated luminosity. If False, plot "Simulation", by default True.
334
- lumi : int/string, optional
332
+ lumi : int | str, optional
335
333
  Integrated luminosity. If empty, do not plot luminosity. Default value is empty.
336
- lumi_unit : string, optional
334
+ lumi_unit : str, optional
337
335
  Integrated luminosity unit. Default value is fb. The exponent is automatically -1.
338
336
  preliminary : bool, optional
339
337
  If True, print "preliminary", by default False.
@@ -383,7 +381,7 @@ def add_luminosity(
383
381
  )
384
382
 
385
383
 
386
- def plot_reordered_legend(ax, order, **kwargs):
384
+ def plot_reordered_legend(ax: plt.Axes, order: list[int], **kwargs) -> None:
387
385
  """
388
386
  Reorder the legend handlers and labels on the given Matplotlib axis based
389
387
  on the specified order and plot the reordered legend.
@@ -392,7 +390,7 @@ def plot_reordered_legend(ax, order, **kwargs):
392
390
  ----------
393
391
  ax : matplotlib.axes.Axes
394
392
  The Matplotlib Axes object on which the legend is to be reordered.
395
- order : list of int
393
+ order : list[int]
396
394
  A list of integers specifying the new order of the legend items.
397
395
  The integers refer to the indices of the current legend items.
398
396
  kwargs : dict, optional
@@ -418,7 +416,6 @@ def plot_reordered_legend(ax, order, **kwargs):
418
416
  To reorder the legend so that 'Line 2' comes first, use:
419
417
 
420
418
  >>> plot_reordered_legend(ax, [1, 0])
421
-
422
419
  """
423
420
 
424
421
  # Extract the original handlers and labels