xarpes 0.2.4__py3-none-any.whl → 0.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.
xarpes/plotting.py CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (C) 2024 xARPES Developers
1
+ # Copyright (C) 2025 xARPES Developers
2
2
  # This program is free software under the terms of the GNU GPLv3 license.
3
3
 
4
4
  # get_ax_fig_plt and add_fig_kwargs originate from pymatgen/util/plotting.py.
@@ -11,21 +11,7 @@
11
11
 
12
12
  """Functions related to plotting."""
13
13
 
14
- from functools import wraps
15
14
  import matplotlib.pyplot as plt
16
- import matplotlib as mpl
17
-
18
- def plot_settings(name='default'):
19
- mpl.rc('xtick', labelsize=10, direction='in')
20
- mpl.rc('ytick', labelsize=10, direction='in')
21
- lw = dict(default=2.0, large=4.0)[name]
22
- mpl.rcParams['lines.linewidth'] = lw
23
- mpl.rcParams['lines.markersize'] = 3
24
- mpl.rcParams['xtick.major.size'] = 4
25
- mpl.rcParams['xtick.minor.size'] = 2
26
- mpl.rcParams['xtick.major.width'] = 0.8
27
- mpl.rcParams.update({'font.size': 16})
28
- mpl.use('Qt5Agg') # Backend for showing plots in terminal
29
15
 
30
16
  def get_ax_fig_plt(ax=None, **kwargs):
31
17
  r"""Helper function used in plot functions supporting an optional `Axes`
@@ -58,13 +44,16 @@ def get_ax_fig_plt(ax=None, **kwargs):
58
44
 
59
45
  return ax, fig, plt
60
46
 
47
+
61
48
  def add_fig_kwargs(func):
62
49
  """Decorator that adds keyword arguments for functions returning matplotlib
63
50
  figures.
64
51
 
65
- The function should return either a matplotlib figure or None to signal
66
- some sort of error/unexpected event.
52
+ The function should return either a matplotlib figure or a tuple, where the
53
+ first element is a matplotlib figure, or None to signal some sort of
54
+ error/unexpected event.
67
55
  """
56
+ from functools import wraps
68
57
  @wraps(func)
69
58
  def wrapper(*args, **kwargs):
70
59
  # pop the kwds used by the decorator.
@@ -75,14 +64,26 @@ def add_fig_kwargs(func):
75
64
  tight_layout = kwargs.pop('tight_layout', False)
76
65
  ax_grid = kwargs.pop('ax_grid', None)
77
66
  ax_annotate = kwargs.pop('ax_annotate', None)
78
- fig_close = kwargs.pop('fig_close', False)
79
-
80
- # Call func and return immediately if None is returned.
81
- fig = func(*args, **kwargs)
67
+ fig_close = kwargs.pop('fig_close', True)
68
+
69
+ import string
70
+
71
+ # Call the original function
72
+ result = func(*args, **kwargs)
73
+
74
+ # Determine if result is a figure or tuple with first element as figure
75
+ if isinstance(result, tuple):
76
+ fig = result[0]
77
+ rest = result[1:]
78
+ else:
79
+ fig = result
80
+ rest = None
81
+
82
+ # Return immediately if no figure is returned
82
83
  if fig is None:
83
- return fig
84
+ return result
84
85
 
85
- # Operate on matplotlib figure.
86
+ # Operate on the matplotlib figure
86
87
  if title is not None:
87
88
  fig.suptitle(title)
88
89
 
@@ -95,9 +96,10 @@ def add_fig_kwargs(func):
95
96
  ax.grid(bool(ax_grid))
96
97
 
97
98
  if ax_annotate:
98
- tags = ascii_letters
99
+ tags = string.ascii_letters
99
100
  if len(fig.axes) > len(tags):
100
- tags = (1 + len(ascii_letters) // len(fig.axes)) * ascii_letters
101
+ tags = (1 + len(string.ascii_letters) // len(fig.axes)) * \
102
+ string.ascii_letters
101
103
  for ax, tag in zip(fig.axes, tags):
102
104
  ax.annotate(f'({tag})', xy=(0.05, 0.95),
103
105
  xycoords='axes fraction')
@@ -106,10 +108,8 @@ def add_fig_kwargs(func):
106
108
  try:
107
109
  fig.tight_layout()
108
110
  except Exception as exc:
109
- # For some unknown reason, this problem shows up only on travis.
110
- # https://stackoverflow.com/questions/22708888/valueerror-when-using-matplotlib-tight-layout
111
- print('Ignoring Exception raised by fig.tight_layout\n',
112
- str(exc))
111
+ print('Ignoring Exception raised by fig.tight_layout ' +
112
+ '\n', str(exc))
113
113
 
114
114
  if savefig:
115
115
  fig.savefig(savefig)
@@ -119,12 +119,16 @@ def add_fig_kwargs(func):
119
119
  if fig_close:
120
120
  plt.close(fig=fig)
121
121
 
122
- return fig
122
+ # Reassemble the tuple if necessary and return
123
+ if rest is not None:
124
+ return (fig, *rest)
125
+ else:
126
+ return fig
123
127
 
124
128
  # Add docstring to the decorated method.
125
129
  doc_str = """\n\n
126
130
 
127
- notes
131
+ Notes
128
132
  -----
129
133
 
130
134
  Keyword arguments controlling the display of the figure:
@@ -141,18 +145,16 @@ def add_fig_kwargs(func):
141
145
  ax_grid True (False) to add (remove) grid from all axes in
142
146
  fig.
143
147
  Default: None i.e. fig is left unchanged.
144
- ax_annotate Add labels to subplots e.g. (a), (b).
148
+ ax_annotate Add labels to subplots e.g. (a), (b).
145
149
  Default: False
146
- fig_close Close figure. Default: False.
150
+ fig_close Close figure. Default: True.
147
151
  ================ ====================================================
148
152
 
149
153
  """
150
154
 
151
155
  if wrapper.__doc__ is not None:
152
- # Add s at the end of the docstring.
153
156
  wrapper.__doc__ += f'\n{doc_str}'
154
157
  else:
155
- # Use s
156
158
  wrapper.__doc__ = doc_str
157
159
 
158
160
  return wrapper