gsplot 0.0.1__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.
@@ -0,0 +1,296 @@
1
+ from typing import Any
2
+
3
+ import numpy as np
4
+ from matplotlib.axes import Axes
5
+ from matplotlib.collections import PathCollection
6
+ from numpy.typing import ArrayLike, NDArray
7
+
8
+ from ..base.base import CreateClassParams, ParamsGetter, bind_passed_params
9
+ from ..base.base_alias_validator import AliasValidator
10
+ from ..figure.axes_base import AxesRangeSingleton, AxesResolver
11
+ from ..style.legend_colormap import LegendColormap
12
+
13
+ __all__: list[str] = ["scatter_colormap"]
14
+
15
+
16
+ class ScatterColormap:
17
+ """
18
+ A class for creating scatter plots with colormap-based coloring on a specified Matplotlib axis.
19
+
20
+ Parameters
21
+ --------------------
22
+ axis_target : int or matplotlib.axes.Axes
23
+ The target axis for the scatter plot. Can be an axis index or a Matplotlib `Axes` object.
24
+ x : ArrayLike
25
+ The x-coordinates of the scatter points.
26
+ y : ArrayLike
27
+ The y-coordinates of the scatter points.
28
+ cmapdata : ArrayLike
29
+ The data used to determine the color of the scatter points.
30
+ size : int or float, optional
31
+ Size of the scatter points (default is 1).
32
+ cmap : str, optional
33
+ The name of the colormap to use (default is "viridis").
34
+ vmin : int or float, optional
35
+ The minimum value of the colormap scale (default is 0).
36
+ vmax : int or float, optional
37
+ The maximum value of the colormap scale (default is 1).
38
+ alpha : int or float, optional
39
+ Opacity of the scatter points (default is 1).
40
+ label : str or None, optional
41
+ Label for the colormap, used in legends (default is None).
42
+ **kwargs : Any
43
+ Additional keyword arguments passed to the `scatter` method of Matplotlib's `Axes`.
44
+
45
+ Attributes
46
+ --------------------
47
+ axis_index : int
48
+ The resolved index of the target axis.
49
+ axis : matplotlib.axes.Axes
50
+ The resolved target axis object.
51
+ x : numpy.ndarray
52
+ The x-coordinates as a NumPy array.
53
+ y : numpy.ndarray
54
+ The y-coordinates as a NumPy array.
55
+ cmapdata : numpy.ndarray
56
+ The colormap data as a NumPy array.
57
+ cmap_norm : numpy.ndarray
58
+ Normalized colormap data.
59
+ vmin : float
60
+ The minimum value of the colormap scale.
61
+ vmax : float
62
+ The maximum value of the colormap scale.
63
+
64
+ Methods
65
+ --------------------
66
+ add_legend_colormap() -> None
67
+ Adds a colormap legend to the plot, if a label is provided.
68
+ get_cmap_norm() -> numpy.ndarray
69
+ Normalizes the colormap data to a range of [0, 1].
70
+ plot() -> matplotlib.collections.PathCollection
71
+ Creates and plots the scatter points with colormap-based coloring.
72
+
73
+ Examples
74
+ --------------------
75
+ >>> x = [1, 2, 3, 4]
76
+ >>> y = [10, 20, 15, 25]
77
+ >>> cmapdata = [0.1, 0.5, 0.3, 0.9]
78
+ >>> scatter = ScatterColormap(axis_target=0, x=x, y=y, cmapdata=cmapdata, cmap="plasma")
79
+ >>> scatter.plot()
80
+ """
81
+
82
+ def __init__(
83
+ self,
84
+ axis_target: int | Axes,
85
+ x: ArrayLike,
86
+ y: ArrayLike,
87
+ cmapdata: ArrayLike,
88
+ size: int | float = 1,
89
+ cmap: str = "viridis",
90
+ vmin: int | float = 0,
91
+ vmax: int | float = 1,
92
+ alpha: int | float = 1,
93
+ label: str | None = None,
94
+ **kwargs: Any,
95
+ ) -> None:
96
+ self.axis_target: int | Axes = axis_target
97
+
98
+ self.axis_index: int = AxesResolver(self.axis_target).axis_index
99
+ self.axis: Axes = AxesResolver(self.axis_target).axis
100
+
101
+ self._x: ArrayLike = x
102
+ self._y: ArrayLike = y
103
+ self._cmapdata: ArrayLike = cmapdata
104
+ self.size: int | float = size
105
+ self.cmap: str = cmap
106
+ self._vmin: int | float = vmin
107
+ self._vmax: int | float = vmax
108
+ self.alpha: int | float = alpha
109
+ self.label: str | None = label
110
+ self.kwargs: Any = kwargs
111
+
112
+ self.x: NDArray[Any] = np.array(self._x)
113
+ self.y: NDArray[Any] = np.array(self._y)
114
+ self.cmapdata: NDArray[Any] = np.array(self._cmapdata)
115
+ self.vmin: float = float(self._vmin)
116
+ self.vmax: float = float(self._vmax)
117
+
118
+ self.cmap_norm: NDArray[Any] = self.get_cmap_norm()
119
+
120
+ if self.label is not None:
121
+ self.add_legend_colormap()
122
+
123
+ def add_legend_colormap(self) -> None:
124
+ """
125
+ Adds a colormap legend to the plot.
126
+
127
+ If a label is provided, this method creates a colormap legend with stripes
128
+ corresponding to the colormap data.
129
+
130
+ Notes
131
+ --------------------
132
+ The legend is created using the `LegendColormap` class.
133
+
134
+ Examples
135
+ --------------------
136
+ >>> scatter = ScatterColormap(axis_target=0, x=[1, 2], y=[3, 4], cmapdata=[0.1, 0.9], label="Intensity")
137
+ >>> scatter.add_legend_colormap()
138
+ """
139
+ if self.label is not None:
140
+ LegendColormap(
141
+ axis_target=self.axis_target,
142
+ cmap=self.cmap,
143
+ label=self.label,
144
+ num_stripes=len(self.cmapdata),
145
+ ).legend_colormap()
146
+
147
+ def get_cmap_norm(self) -> NDArray[Any]:
148
+ """
149
+ Normalizes the colormap data to a range of [0, 1].
150
+
151
+ The normalization is based on the minimum and maximum values of the colormap data.
152
+
153
+ Returns
154
+ --------------------
155
+ numpy.ndarray
156
+ Normalized colormap data.
157
+
158
+ Examples
159
+ --------------------
160
+ >>> scatter = ScatterColormap(axis_target=0, x=[1, 2], y=[3, 4], cmapdata=[0.1, 0.9])
161
+ >>> scatter.get_cmap_norm()
162
+ array([0. , 1.])
163
+ """
164
+ cmapdata_max = max(self.cmapdata)
165
+ cmapdata_min = min(self.cmapdata)
166
+ cmap_norm: NDArray[Any] = (self.cmapdata - cmapdata_min) / (
167
+ cmapdata_max - cmapdata_min
168
+ )
169
+ return cmap_norm
170
+
171
+ @AxesRangeSingleton.update
172
+ def plot(self) -> PathCollection:
173
+ """
174
+ Plots the scatter points with colormap-based coloring.
175
+
176
+ This method uses the normalized colormap data to assign colors to the scatter points
177
+ and creates the plot on the specified axis.
178
+
179
+ Returns
180
+ --------------------
181
+ matplotlib.collections.PathCollection
182
+ The scatter plot as a PathCollection object.
183
+
184
+ Notes
185
+ --------------------
186
+ - This method is decorated with `@AxesRangeSingleton.update` to update the axis range with the scatter data.
187
+ - The colormap and normalization are applied using the `cmap` and `cmap_norm` attributes.
188
+
189
+ Examples
190
+ --------------------
191
+ >>> scatter = ScatterColormap(axis_target=0, x=[1, 2, 3], y=[4, 5, 6], cmapdata=[0.2, 0.5, 0.8])
192
+ >>> scatter.plot()
193
+ <matplotlib.collections.PathCollection>
194
+ """
195
+ _plot = self.axis.scatter(
196
+ x=self.x,
197
+ y=self.y,
198
+ s=self.size,
199
+ c=self.cmap_norm,
200
+ cmap=self.cmap,
201
+ vmin=self.vmin,
202
+ vmax=self.vmax,
203
+ alpha=self.alpha,
204
+ **self.kwargs,
205
+ )
206
+ return _plot
207
+
208
+
209
+ @bind_passed_params()
210
+ def scatter_colormap(
211
+ axis_target: int | Axes,
212
+ x: ArrayLike,
213
+ y: ArrayLike,
214
+ cmapdata: ArrayLike,
215
+ size: int | float = 1,
216
+ cmap: str = "viridis",
217
+ vmin: int | float = 0,
218
+ vmax: int | float = 1,
219
+ alpha: int | float = 1,
220
+ label: str | None = None,
221
+ **kwargs: Any,
222
+ ) -> PathCollection:
223
+ """
224
+ Creates a scatter plot with colormap-based coloring on the specified axis.
225
+
226
+ This function uses the `ScatterColormap` class to generate a scatter plot with customizable
227
+ size, colormap, and transparency.
228
+
229
+ Parameters
230
+ --------------------
231
+ axis_target : int or matplotlib.axes.Axes
232
+ The target axis for the scatter plot. Can be an axis index or a Matplotlib `Axes` object.
233
+ x : ArrayLike
234
+ The x-coordinates of the scatter points.
235
+ y : ArrayLike
236
+ The y-coordinates of the scatter points.
237
+ cmapdata : ArrayLike
238
+ The data used to determine the color of the scatter points.
239
+ size : int or float, optional
240
+ Size of the scatter points (default is 1).
241
+ cmap : str, optional
242
+ The name of the colormap to use (default is "viridis").
243
+ vmin : int or float, optional
244
+ The minimum value of the colormap scale (default is 0).
245
+ vmax : int or float, optional
246
+ The maximum value of the colormap scale (default is 1).
247
+ alpha : int or float, optional
248
+ Opacity of the scatter points (default is 1).
249
+ label : str or None, optional
250
+ Label for the colormap, used in legends (default is None).
251
+ **kwargs : Any
252
+ Additional keyword arguments passed to the `scatter` method of Matplotlib's `Axes`.
253
+
254
+ Notes
255
+ --------------------
256
+ - This function utilizes the `ParamsGetter` to retrieve bound parameters and the `CreateClassParams` class to handle the merging of default, configuration, and passed parameters.
257
+ - Alias validation is performed using the `AliasValidator` class.
258
+
259
+ - 's' (size)
260
+
261
+ Returns
262
+ --------------------
263
+ matplotlib.collections.PathCollection
264
+ The scatter plot as a PathCollection object.
265
+
266
+ Examples
267
+ --------------------
268
+ >>> import gsplot as gs
269
+ >>> x = [1, 2, 3, 4]
270
+ >>> y = [10, 20, 15, 25]
271
+ >>> cmapdata = [0.1, 0.5, 0.3, 0.9]
272
+ >>> gs.scatter_colormap(axis_target=0, x=x, y=y, cmapdata=cmapdata, cmap="plasma", label="Data")
273
+ <matplotlib.collections.PathCollection>
274
+ """
275
+ alias_map = {
276
+ "s": "size",
277
+ }
278
+
279
+ passed_params: dict[str, Any] = ParamsGetter("passed_params").get_bound_params()
280
+ AliasValidator(alias_map, passed_params).validate()
281
+ class_params: dict[str, Any] = CreateClassParams(passed_params).get_class_params()
282
+
283
+ _scatter_colormap = ScatterColormap(
284
+ class_params["axis_target"],
285
+ class_params["x"],
286
+ class_params["y"],
287
+ class_params["cmapdata"],
288
+ class_params["size"],
289
+ class_params["cmap"],
290
+ class_params["vmin"],
291
+ class_params["vmax"],
292
+ class_params["alpha"],
293
+ class_params["label"],
294
+ **class_params["kwargs"],
295
+ )
296
+ return _scatter_colormap.plot()