gsplot 0.1.2__tar.gz → 0.1.3__tar.gz

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 (36) hide show
  1. {gsplot-0.1.2 → gsplot-0.1.3}/PKG-INFO +2 -2
  2. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/__init__.py +6 -3
  3. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/data/load_file.py +144 -1
  4. gsplot-0.1.3/gsplot/figure/axes_base.py +145 -0
  5. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/scatter.py +1 -3
  6. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/style/label.py +1 -2
  7. gsplot-0.1.3/gsplot/style/title.py +122 -0
  8. gsplot-0.1.3/gsplot/version.py +2 -0
  9. {gsplot-0.1.2 → gsplot-0.1.3}/pyproject.toml +1 -1
  10. gsplot-0.1.2/gsplot/figure/axes_base.py +0 -288
  11. gsplot-0.1.2/gsplot/version.py +0 -2
  12. {gsplot-0.1.2 → gsplot-0.1.3}/LICENSE +0 -0
  13. {gsplot-0.1.2 → gsplot-0.1.3}/README.md +0 -0
  14. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/base/base.py +0 -0
  15. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/base/base_alias_validator.py +0 -0
  16. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/color/colormap.py +0 -0
  17. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/config/config.py +0 -0
  18. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/figure/axes.py +0 -0
  19. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/figure/axes_inset.py +0 -0
  20. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/figure/axes_range_base.py +0 -0
  21. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/figure/figure_tools.py +0 -0
  22. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/figure/show.py +0 -0
  23. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/figure/store.py +0 -0
  24. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/hello_world/hello_world.py +0 -0
  25. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/logger.py +0 -0
  26. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/path/path.py +0 -0
  27. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/line.py +0 -0
  28. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/line_base.py +0 -0
  29. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/line_colormap_base.py +0 -0
  30. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/line_colormap_dashed.py +0 -0
  31. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/line_colormap_solid.py +0 -0
  32. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/plot/scatter_colormap.py +0 -0
  33. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/style/graph.py +0 -0
  34. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/style/legend.py +0 -0
  35. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/style/legend_colormap.py +0 -0
  36. {gsplot-0.1.2 → gsplot-0.1.3}/gsplot/style/ticks.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: gsplot
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: General-scientific plot based on matplotlib
5
5
  Author: Giordano Mattoni
6
6
  Author-email: mattoni@scphys.kyoto-u.ac.jp
@@ -1,7 +1,7 @@
1
1
  from .color.colormap import get_cmap
2
2
  from .config.config import (Config, config_dict, config_entry_option,
3
3
  config_load, save_metadata)
4
- from .data.load_file import load_file
4
+ from .data.load_file import load_file, load_file_fast
5
5
  from .figure.axes import axes
6
6
  from .figure.axes_inset import axes_inset, axes_inset_padding
7
7
  from .figure.figure_tools import get_figure_size
@@ -22,6 +22,7 @@ from .style.legend import (legend, legend_axes, legend_get_handlers,
22
22
  legend_handlers, legend_reverse)
23
23
  from .style.legend_colormap import legend_colormap
24
24
  from .style.ticks import ticks_off, ticks_on, ticks_on_axes
25
+ from .style.title import title, title_axes
25
26
  from .version import __commit__, __version__
26
27
 
27
28
  # ╭──────────────────────────────────────────────────────────╮
@@ -48,6 +49,7 @@ __all__ = [
48
49
  "get_cmap",
49
50
  # data/load_file.py
50
51
  "load_file",
52
+ "load_file_fast",
51
53
  # figure/axes.py
52
54
  "axes",
53
55
  # figure/axes_inset.py
@@ -70,8 +72,6 @@ __all__ = [
70
72
  "pwd_main",
71
73
  # plot/line.py
72
74
  "line",
73
- # plot/line_colormap.py
74
- "line_colormap",
75
75
  # plot/line_colormap_solid.py
76
76
  "line_colormap_solid",
77
77
  # plot/line_colormap_dashed.py
@@ -103,4 +103,7 @@ __all__ = [
103
103
  "ticks_off",
104
104
  "ticks_on",
105
105
  "ticks_on_axes",
106
+ # style/title.py
107
+ "title",
108
+ "title_axes",
106
109
  ]
@@ -6,7 +6,7 @@ from numpy.typing import NDArray
6
6
 
7
7
  from ..base.base import CreateClassParams, ParamsGetter, bind_passed_params
8
8
 
9
- __all__: list[str] = ["load_file"]
9
+ __all__: list[str] = ["load_file", "load_file_fast"]
10
10
 
11
11
 
12
12
  class LoadFile:
@@ -186,3 +186,146 @@ def load_file(
186
186
  **class_params["kwargs"],
187
187
  )
188
188
  return _load_file.load_data()
189
+
190
+
191
+ class LoadFileFast:
192
+ """
193
+ A utility class to load data from a file or iterable source using NumPy's `loadtxt`.
194
+
195
+ This class provides an interface for loading unstructured data from files or iterables
196
+ with options for handling delimiters and skipping rows.
197
+
198
+ Parameters
199
+ --------------------
200
+ f : str, os.PathLike, Iterable[str], or Iterable[bytes]
201
+ The file path, file-like object, or iterable source from which to load data.
202
+ delimiter : str or None, optional
203
+ The string used to separate values. If `None`, any whitespace is treated as a delimiter (default is ",").
204
+ skiprows : int, optional
205
+ The number of rows to skip at the beginning of the file (default is 0).
206
+ unpack : bool, optional
207
+ Whether to unpack columns into separate arrays (default is True).
208
+ **kwargs : Any
209
+ Additional keyword arguments to pass to NumPy's `loadtxt`.
210
+
211
+ Attributes
212
+ --------------------
213
+ f : str, os.PathLike, Iterable[str], or Iterable[bytes]
214
+ The file path, file-like object, or iterable source from which to load data.
215
+ delimiter : str or None
216
+ The string used to separate values.
217
+ skiprows : int
218
+ The number of rows to skip at the beginning of the file.
219
+ unpack : bool
220
+ Whether to unpack columns into separate arrays.
221
+ kwargs : Any
222
+ Additional arguments passed to `loadtxt`.
223
+
224
+ Methods
225
+ --------------------
226
+ load_data()
227
+ Loads the data using NumPy's `loadtxt` with the specified parameters.
228
+ """
229
+
230
+ def __init__(
231
+ self,
232
+ f: str | PathLike | Iterable[str] | Iterable[bytes],
233
+ delimiter: str | None = ",",
234
+ skiprows: int = 0,
235
+ unpack: bool = True,
236
+ **kwargs: Any,
237
+ ) -> None:
238
+ self.f: str | PathLike | Iterable[str] | Iterable[bytes] = f
239
+ self.delimiter: str | None = delimiter
240
+ self.skiprows: int = skiprows
241
+ self.unpack: bool = unpack
242
+ self.kwargs: Any = kwargs
243
+
244
+ def load_data(self) -> NDArray[Any]:
245
+ """
246
+ Loads the data using NumPy's `loadtxt` with the specified parameters.
247
+
248
+ Returns
249
+ --------------------
250
+ numpy.ndarray
251
+ The loaded data as a NumPy array.
252
+ """
253
+ data = np.loadtxt(
254
+ fname=self.f,
255
+ skiprows=self.skiprows,
256
+ delimiter=self.delimiter,
257
+ unpack=self.unpack,
258
+ **self.kwargs,
259
+ )
260
+ return data
261
+
262
+
263
+ @bind_passed_params()
264
+ def load_file_fast(
265
+ f: str | PathLike | Iterable[str] | Iterable[bytes],
266
+ delimiter: str | None = ",",
267
+ skiprows: int = 0,
268
+ unpack: bool = True,
269
+ **kwargs: Any,
270
+ ) -> NDArray[Any]:
271
+ """
272
+ Loads unstructured data from a file or iterable source using the specified parameters.
273
+
274
+ This function provides a flexible interface for loading data with NumPy's `loadtxt`.
275
+ It captures and processes the passed parameters, allowing for customized file loading
276
+ options, such as handling delimiters, skipping rows, and unpacking columns.
277
+
278
+ Parameters
279
+ --------------------
280
+ f : str, os.PathLike, Iterable[str], or Iterable[bytes]
281
+ The file path, file-like object, or iterable source from which to load data.
282
+ delimiter : str or None, optional
283
+ The string used to separate values. If `None`, any whitespace is treated as a delimiter (default is ",").
284
+ skiprows : int, optional
285
+ The number of rows to skip at the beginning of the file (default is 0).
286
+ unpack : bool, optional
287
+ Whether to unpack columns into separate arrays (default is True).
288
+ **kwargs : Any
289
+ Additional keyword arguments to pass to NumPy's `loadtxt`.
290
+
291
+ Notes
292
+ --------------------
293
+ This function utilizes the `ParamsGetter` to retrieve bound parameters and
294
+ the `CreateClassParams` class to handle the merging of default, configuration,
295
+ and passed parameters.
296
+
297
+ Returns
298
+ --------------------
299
+ numpy.ndarray
300
+ The loaded data as a NumPy array.
301
+
302
+ Raises
303
+ --------------------
304
+ ValueError
305
+ If the file cannot be loaded or parsed correctly.
306
+
307
+ Examples
308
+ --------------------
309
+ >>> import gsplot as gs
310
+ >>> data = gs.load_file_fast("data.csv", delimiter=",", skiprows=1, unpack=False)
311
+ >>> print(data)
312
+ array([[1.0, 2.0, 3.0],
313
+ [4.0, 5.0, 6.0],
314
+ [7.0, 8.0, 9.0]])
315
+
316
+ >>> data = gs.load_file_fast(["1,2,3", "4,5,6", "7,8,9"], delimiter=",", unpack=True)
317
+ >>> print(data)
318
+ [array([1.0, 4.0, 7.0]), array([2.0, 5.0, 8.0]), array([3.0, 6.0, 9.0])]
319
+ """
320
+
321
+ passed_params: dict[str, Any] = ParamsGetter("passed_params").get_bound_params()
322
+ class_params = CreateClassParams(passed_params).get_class_params()
323
+
324
+ _load_file_fast: LoadFileFast = LoadFileFast(
325
+ class_params["f"],
326
+ class_params["delimiter"],
327
+ class_params["skiprows"],
328
+ class_params["unpack"],
329
+ **class_params["kwargs"],
330
+ )
331
+ return _load_file_fast.load_data()
@@ -0,0 +1,145 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any, Callable, TypeVar
4
+
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
+ from matplotlib.axes import Axes
8
+ from matplotlib.transforms import Bbox
9
+ from numpy.typing import NDArray
10
+
11
+ from .figure_tools import FigureLayout
12
+
13
+ F = TypeVar("F", bound=Callable[..., Any])
14
+
15
+ __all__: list[str] = []
16
+
17
+
18
+ class AxisLayout:
19
+ """
20
+ A utility class for managing axis layout properties in a Matplotlib figure.
21
+
22
+ This class provides methods to retrieve an axis's position and size, both in
23
+ normalized figure coordinates and in physical units (inches). It integrates
24
+ with the `AxesResolver` and `FigureLayout` classes to ensure consistent layout
25
+ calculations.
26
+
27
+ Parameters
28
+ --------------------
29
+ ax : matplotlib.axes.Axes
30
+ The target `Axes` object for which to manage the
31
+
32
+ Attributes
33
+ --------------------
34
+ ax : matplotlib.axes.Axes
35
+ The target `Axes` object for which to manage the layout.
36
+ fig_size : numpy.ndarray
37
+ The size of the figure in inches as a NumPy array.
38
+
39
+ Methods
40
+ --------------------
41
+ get_axis_position()
42
+ Returns the position of the axis in normalized figure coordinates.
43
+ get_axis_size()
44
+ Returns the size of the axis in normalized figure coordinates.
45
+ get_axis_position_inches()
46
+ Returns the position of the axis in physical units (inches).
47
+ get_axis_size_inches()
48
+ Returns the size of the axis in physical units (inches).
49
+
50
+ Examples
51
+ --------------------
52
+ >>> fig, ax = plt.subplots()
53
+ >>> layout = AxisLayout(ax)
54
+ >>> position = layout.get_axis_position()
55
+ >>> size = layout.get_axis_size()
56
+ >>> position_inches = layout.get_axis_position_inches()
57
+ """
58
+
59
+ def __init__(self, ax: Axes) -> None:
60
+ self.ax: Axes = ax
61
+ self.fig_size: NDArray[Any] = FigureLayout().get_figure_size()
62
+
63
+ def get_axis_position(self) -> Bbox:
64
+ """
65
+ Retrieves the position of the axis in normalized figure coordinates.
66
+
67
+ Returns
68
+ --------------------
69
+ matplotlib.transforms.Bbox
70
+ The position of the axis as a bounding box in normalized coordinates.
71
+
72
+ Examples
73
+ --------------------
74
+ >>> layout = AxisLayout(axis_index=0)
75
+ >>> position = layout.get_axis_position()
76
+ >>> print(position)
77
+ Bbox(x0=0.1, y0=0.1, x1=0.9, y1=0.9)
78
+ """
79
+ axis_position = self.ax.get_position()
80
+ return axis_position
81
+
82
+ def get_axis_size(self) -> NDArray[Any]:
83
+ """
84
+ Retrieves the size of the axis in normalized figure coordinates.
85
+
86
+ Returns
87
+ --------------------
88
+ numpy.ndarray
89
+ The width and height of the axis as a NumPy array.
90
+
91
+ Examples
92
+ --------------------
93
+ >>> layout = AxisLayout(axis_index=0)
94
+ >>> size = layout.get_axis_size()
95
+ >>> print(size)
96
+ array([0.8, 0.8])
97
+ """
98
+ axis_position_size = np.array(self.get_axis_position().size)
99
+ return axis_position_size
100
+
101
+ def get_axis_position_inches(self) -> Bbox:
102
+ """
103
+ Retrieves the position of the axis in physical units (inches).
104
+
105
+ Returns
106
+ --------------------
107
+ matplotlib.transforms.Bbox
108
+ The position of the axis as a bounding box in inches.
109
+
110
+ Examples
111
+ --------------------
112
+ >>> layout = AxisLayout(axis_index=0)
113
+ >>> position_inches = layout.get_axis_position_inches()
114
+ >>> print(position_inches)
115
+ Bbox(x0=1.6, y0=1.6, x1=14.4, y1=14.4)
116
+ """
117
+
118
+ axis_position = self.get_axis_position()
119
+
120
+ axis_position_inches = Bbox.from_bounds(
121
+ axis_position.x0 * self.fig_size[0],
122
+ axis_position.y0 * self.fig_size[1],
123
+ axis_position.width * self.fig_size[0],
124
+ axis_position.height * self.fig_size[1],
125
+ )
126
+ return axis_position_inches
127
+
128
+ def get_axis_size_inches(self) -> NDArray[Any]:
129
+ """
130
+ Retrieves the size of the axis in physical units (inches).
131
+
132
+ Returns
133
+ --------------------
134
+ numpy.ndarray
135
+ The width and height of the axis in inches as a NumPy array.
136
+
137
+ Examples
138
+ --------------------
139
+ >>> layout = AxisLayout(axis_index=0)
140
+ >>> size_inches = layout.get_axis_size_inches()
141
+ >>> print(size_inches)
142
+ array([12.8, 12.8])
143
+ """
144
+ axis_position_size_inches = np.array(self.get_axis_position_inches().size)
145
+ return axis_position_size_inches
@@ -141,7 +141,7 @@ class Scatter:
141
141
  self.x,
142
142
  self.y,
143
143
  s=self.size,
144
- c=self.color,
144
+ color=self.color,
145
145
  alpha=self.alpha,
146
146
  **self.kwargs,
147
147
  )
@@ -187,7 +187,6 @@ def scatter(
187
187
  - Alias validation is performed using the `AliasValidator` class.
188
188
 
189
189
  - 's' (size)
190
- - 'c' (color)
191
190
 
192
191
  Returns
193
192
  --------------------
@@ -204,7 +203,6 @@ def scatter(
204
203
  """
205
204
  alias_map = {
206
205
  "s": "size",
207
- "c": "color",
208
206
  }
209
207
 
210
208
  passed_params: dict[str, Any] = ParamsGetter("passed_params").get_bound_params()
@@ -902,7 +902,6 @@ def label(
902
902
  ) -> None:
903
903
  """
904
904
  Configures labels, limits, ticks, and layouts for Matplotlib axes.
905
-
906
905
  This function is a wrapper for the `Label` class.
907
906
 
908
907
  Parameters
@@ -923,7 +922,7 @@ def label(
923
922
  ypad_layout : int, default=2
924
923
  Vertical padding for tight layout.
925
924
  *args : Any
926
- Additional arguments for `ax.set_xlabel` and `ax.set_ylabel.
925
+ Additional arguments for `ax.set_xlabel` and `ax.set_ylabel`.
927
926
  **kwargs : Any
928
927
  Additional keyword arguments for `ax.set_xlabel` and `ax.set_ylabel`.
929
928
 
@@ -0,0 +1,122 @@
1
+ from typing import Any
2
+
3
+ import matplotlib.pyplot as plt
4
+ from matplotlib.axes import Axes
5
+ from matplotlib.text import Text
6
+
7
+ __all__ = ["title", "title_axes"]
8
+
9
+
10
+ class Title:
11
+ """
12
+ Set the title of the current figure.
13
+
14
+ Parameters
15
+ --------------------
16
+ title : str
17
+ **kwargs : Any
18
+
19
+ Attributes
20
+ --------------------
21
+ title : str
22
+ kwargs : Any
23
+
24
+ Methods
25
+ --------------------
26
+ set_title()
27
+ Set the title of the current figure
28
+ """
29
+
30
+ def __init__(self, title: str, **kwargs: Any) -> None:
31
+ self.title: str = title
32
+ self.kwargs: Any = kwargs
33
+
34
+ def set_title(self) -> Text:
35
+ """
36
+ Set the title of the current figure.
37
+
38
+ Returns
39
+ --------------------
40
+ Text
41
+ The title of the current figure.
42
+ """
43
+ return plt.gcf().suptitle(self.title, **self.kwargs)
44
+
45
+
46
+ def title(title: str, **kwargs: Any) -> Text:
47
+ """
48
+ Set the title of the current figure.
49
+
50
+ Parameters
51
+ --------------------
52
+ title : str
53
+ The title of the current figure.
54
+ **kwargs : Any
55
+ Additional keyword arguments to pass to the title.
56
+
57
+ Returns
58
+ --------------------
59
+ Text
60
+ The title of the current figure.
61
+ """
62
+ return Title(title=title, **kwargs).set_title()
63
+
64
+
65
+ class TitleAxes:
66
+ """
67
+ Set the title of the current figure.
68
+
69
+ Parameters
70
+ --------------------
71
+ ax : Axes
72
+ title : str
73
+ **kwargs : Any
74
+
75
+ Attributes
76
+ --------------------
77
+ ax : Axes
78
+ title : str
79
+ kwargs : Any
80
+
81
+ Methods
82
+ --------------------
83
+ set_title()
84
+ Set the title of the current figure
85
+ """
86
+
87
+ def __init__(self, ax: Axes, title: str, **kwargs: Any) -> None:
88
+ self.ax: Axes = ax
89
+ self.title: str = title
90
+ self.kwargs: Any = kwargs
91
+
92
+ def set_title(self) -> Text:
93
+ """
94
+ Set the title of the current figure.
95
+
96
+ Returns
97
+ --------------------
98
+ Text
99
+ The title of the current figure.
100
+ """
101
+ return self.ax.set_title(self.title, **self.kwargs)
102
+
103
+
104
+ def title_axes(ax: Axes, title: str, **kwargs: Any) -> Text:
105
+ """
106
+ Set the title of the current figure.
107
+
108
+ Parameters
109
+ --------------------
110
+ ax : Axes
111
+ The axes to set the title of.
112
+ title : str
113
+ The title of the current figure.
114
+ **kwargs : Any
115
+ Additional keyword arguments to pass to the title.
116
+
117
+ Returns
118
+ --------------------
119
+ Text
120
+ The title of the current figure.
121
+ """
122
+ return TitleAxes(ax=ax, title=title, **kwargs).set_title()
@@ -0,0 +1,2 @@
1
+ __version__ = '0.1.3'
2
+ __commit__ = '637c2d2e807a84c94b172b28e5c736bd885c3469'
@@ -1,7 +1,7 @@
1
1
  [tool.poetry]
2
2
  name = "gsplot"
3
3
 
4
- version = "0.1.2"
4
+ version = "0.1.3"
5
5
 
6
6
  description = "General-scientific plot based on matplotlib"
7
7
  authors = [
@@ -1,288 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from typing import Any, Callable, TypeVar
4
-
5
- import matplotlib.pyplot as plt
6
- import numpy as np
7
- from matplotlib.axes import Axes
8
- from matplotlib.transforms import Bbox
9
- from numpy.typing import NDArray
10
-
11
- from .figure_tools import FigureLayout
12
-
13
- F = TypeVar("F", bound=Callable[..., Any])
14
-
15
- __all__: list[str] = []
16
-
17
-
18
- # class AxesResolver:
19
- # """
20
- # Resolves an axis target to a Matplotlib `Axes` object or its index.
21
- #
22
- # This class provides a mechanism to convert an axis target, which can be either
23
- # an integer (index of the axis) or an `Axes` object, into a consistent representation
24
- # including the corresponding `Axes` object and its index within the current figure.
25
- #
26
- # Parameters
27
- # --------------------
28
- # axis_target : int or matplotlib.axes.Axes
29
- # The target axis to resolve. Can be an integer representing the index of the
30
- # axis in the current figure or a specific `Axes` object.
31
- #
32
- # Attributes
33
- # --------------------
34
- # axis_target : int or matplotlib.axes.Axes
35
- # The input target axis (as provided by the user).
36
- # _axis_index : int or None
37
- # The resolved index of the target axis in the current figure.
38
- # _axis : matplotlib.axes.Axes or None
39
- # The resolved `Axes` object corresponding to the target.
40
- #
41
- # Methods
42
- # --------------------
43
- # _resolve_type()
44
- # Resolves the type of the axis target and retrieves the corresponding
45
- # `Axes` object and its index.
46
- # axis_index
47
- # Returns the resolved index of the axis.
48
- # axis
49
- # Returns the resolved `Axes` object.
50
- #
51
- # Raises
52
- # --------------------
53
- # IndexError
54
- # If the provided axis index is out of range for the current figure.
55
- # ValueError
56
- # If the axis target is neither an integer nor an `Axes` object.
57
- #
58
- # Examples
59
- # --------------------
60
- # >>> import matplotlib.pyplot as plt
61
- # >>> fig, axs = plt.subplots(2, 2)
62
- # >>> resolver = AxesResolver(1) # Resolves the second axis (index 1)
63
- # >>> print(resolver.axis)
64
- # AxesSubplot(0.5,0.5;0.352273x0.352273)
65
- #
66
- # >>> resolver = AxesResolver(axs[0, 0]) # Resolves an Axes object directly
67
- # >>> print(resolver.axis_index)
68
- # 0
69
- # """
70
- #
71
- # def __init__(self, axis_target: int | Axes) -> None:
72
- # self.axis_target: int | Axes = axis_target
73
- #
74
- # self._axis_index: int | None = None
75
- # self._axis: Axes | None = None
76
- #
77
- # self._resolve_type()
78
- #
79
- # def _resolve_type(self) -> None:
80
- # """
81
- # Resolves the type of the axis target and retrieves the corresponding
82
- # `Axes` object and its index.
83
- #
84
- # Raises
85
- # --------------------
86
- # IndexError
87
- # If the provided axis index is out of range for the current figure.
88
- # ValueError
89
- # If the axis target is neither an integer nor an `Axes` object.
90
- # """
91
- #
92
- # def ordinal_suffix(n: int) -> str:
93
- # if 11 <= n % 100 <= 13:
94
- # suffix = "th"
95
- # else:
96
- # suffix = {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")
97
- # return f"{n}{suffix}"
98
- #
99
- # if isinstance(self.axis_target, int):
100
- # self._axis_index = self.axis_target
101
- # axes = plt.gcf().axes
102
- # try:
103
- # self._axis = axes[self._axis_index]
104
- # except IndexError:
105
- # error_message = f"Axes out of range: {self._axis_index} => Number of axes: {len(axes)}, but requested {ordinal_suffix(self._axis_index + 1)} axis."
106
- # raise IndexError(error_message)
107
- # elif isinstance(self.axis_target, Axes):
108
- # self._axis = self.axis_target
109
- # if self.axis_target in plt.gcf().axes:
110
- # self._axis_index = plt.gcf().axes.index(self._axis)
111
- # else:
112
- # # Add the axis to the current figure if it is not present
113
- # plt.gcf().add_axes(self._axis)
114
- # self._axis_index = len(plt.gcf().axes) - 1
115
- # else:
116
- # raise ValueError(
117
- # "Invalid axis target. Please provide an integer or Axes object."
118
- # )
119
- #
120
- # @property
121
- # def axis_index(self) -> int:
122
- # """
123
- # Returns the resolved index of the target axis.
124
- #
125
- # Returns
126
- # --------------------
127
- # int
128
- # The index of the resolved axis.
129
- #
130
- # Raises
131
- # --------------------
132
- # ValueError
133
- # If the axis index is not resolved.
134
- # """
135
- # if isinstance(self._axis_index, int):
136
- # return self._axis_index
137
- # else:
138
- # raise ValueError("Axis index not resolved. Please check the AxisResolver")
139
- #
140
- # @property
141
- # def axis(self) -> Axes:
142
- # """
143
- # Returns the resolved `Axes` object.
144
- #
145
- # Returns
146
- # --------------------
147
- # matplotlib.axes.Axes
148
- # The resolved `Axes` object.
149
- #
150
- # Raises
151
- # --------------------
152
- # ValueError
153
- # If the axis is not resolved.
154
- # """
155
- # if isinstance(self._axis, Axes):
156
- # return self._axis
157
- # else:
158
- # raise ValueError("Axis not resolced. Please check the AxisResolver")
159
-
160
-
161
- class AxisLayout:
162
- """
163
- A utility class for managing axis layout properties in a Matplotlib figure.
164
-
165
- This class provides methods to retrieve an axis's position and size, both in
166
- normalized figure coordinates and in physical units (inches). It integrates
167
- with the `AxesResolver` and `FigureLayout` classes to ensure consistent layout
168
- calculations.
169
-
170
- Parameters
171
- --------------------
172
- ax : matplotlib.axes.Axes
173
- The target `Axes` object for which to manage the
174
-
175
- Attributes
176
- --------------------
177
- ax : matplotlib.axes.Axes
178
- The target `Axes` object for which to manage the layout.
179
- fig_size : numpy.ndarray
180
- The size of the figure in inches as a NumPy array.
181
-
182
- Methods
183
- --------------------
184
- get_axis_position()
185
- Returns the position of the axis in normalized figure coordinates.
186
- get_axis_size()
187
- Returns the size of the axis in normalized figure coordinates.
188
- get_axis_position_inches()
189
- Returns the position of the axis in physical units (inches).
190
- get_axis_size_inches()
191
- Returns the size of the axis in physical units (inches).
192
-
193
- Examples
194
- --------------------
195
- >>> fig, ax = plt.subplots()
196
- >>> layout = AxisLayout(ax)
197
- >>> position = layout.get_axis_position()
198
- >>> size = layout.get_axis_size()
199
- >>> position_inches = layout.get_axis_position_inches()
200
- """
201
-
202
- def __init__(self, ax: Axes) -> None:
203
- self.ax: Axes = ax
204
- self.fig_size: NDArray[Any] = FigureLayout().get_figure_size()
205
-
206
- def get_axis_position(self) -> Bbox:
207
- """
208
- Retrieves the position of the axis in normalized figure coordinates.
209
-
210
- Returns
211
- --------------------
212
- matplotlib.transforms.Bbox
213
- The position of the axis as a bounding box in normalized coordinates.
214
-
215
- Examples
216
- --------------------
217
- >>> layout = AxisLayout(axis_index=0)
218
- >>> position = layout.get_axis_position()
219
- >>> print(position)
220
- Bbox(x0=0.1, y0=0.1, x1=0.9, y1=0.9)
221
- """
222
- axis_position = self.ax.get_position()
223
- return axis_position
224
-
225
- def get_axis_size(self) -> NDArray[Any]:
226
- """
227
- Retrieves the size of the axis in normalized figure coordinates.
228
-
229
- Returns
230
- --------------------
231
- numpy.ndarray
232
- The width and height of the axis as a NumPy array.
233
-
234
- Examples
235
- --------------------
236
- >>> layout = AxisLayout(axis_index=0)
237
- >>> size = layout.get_axis_size()
238
- >>> print(size)
239
- array([0.8, 0.8])
240
- """
241
- axis_position_size = np.array(self.get_axis_position().size)
242
- return axis_position_size
243
-
244
- def get_axis_position_inches(self) -> Bbox:
245
- """
246
- Retrieves the position of the axis in physical units (inches).
247
-
248
- Returns
249
- --------------------
250
- matplotlib.transforms.Bbox
251
- The position of the axis as a bounding box in inches.
252
-
253
- Examples
254
- --------------------
255
- >>> layout = AxisLayout(axis_index=0)
256
- >>> position_inches = layout.get_axis_position_inches()
257
- >>> print(position_inches)
258
- Bbox(x0=1.6, y0=1.6, x1=14.4, y1=14.4)
259
- """
260
-
261
- axis_position = self.get_axis_position()
262
-
263
- axis_position_inches = Bbox.from_bounds(
264
- axis_position.x0 * self.fig_size[0],
265
- axis_position.y0 * self.fig_size[1],
266
- axis_position.width * self.fig_size[0],
267
- axis_position.height * self.fig_size[1],
268
- )
269
- return axis_position_inches
270
-
271
- def get_axis_size_inches(self) -> NDArray[Any]:
272
- """
273
- Retrieves the size of the axis in physical units (inches).
274
-
275
- Returns
276
- --------------------
277
- numpy.ndarray
278
- The width and height of the axis in inches as a NumPy array.
279
-
280
- Examples
281
- --------------------
282
- >>> layout = AxisLayout(axis_index=0)
283
- >>> size_inches = layout.get_axis_size_inches()
284
- >>> print(size_inches)
285
- array([12.8, 12.8])
286
- """
287
- axis_position_size_inches = np.array(self.get_axis_position_inches().size)
288
- return axis_position_size_inches
@@ -1,2 +0,0 @@
1
- __version__ = '0.1.2'
2
- __commit__ = '828d28fda62f1f1935e76112444e53617990c7cc'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes