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.
gsplot/style/graph.py ADDED
@@ -0,0 +1,466 @@
1
+ import matplotlib.pyplot as plt
2
+ from matplotlib import rcParams
3
+ from matplotlib.axes import Axes
4
+ from matplotlib.figure import Figure
5
+
6
+ from ..figure.axes_base import AxesResolver
7
+
8
+ __all__: list[str] = [
9
+ "graph_square",
10
+ "graph_square_axes",
11
+ "graph_white",
12
+ "graph_white_axes",
13
+ "graph_transparent",
14
+ "graph_transparent_axes",
15
+ "graph_facecolor",
16
+ ]
17
+
18
+
19
+ class GraphSquare:
20
+ """
21
+ A class to set Matplotlib axes to have a square aspect ratio.
22
+
23
+ This class provides methods to adjust the aspect ratio of a single axis
24
+ or all axes in the current figure to make them square.
25
+
26
+ Attributes
27
+ --------------------
28
+ _axes : list[matplotlib.axes.Axes]
29
+ List of all axes in the current figure.
30
+
31
+ Methods
32
+ --------------------
33
+ set_square(axis_target: int | matplotlib.axes.Axes) -> None
34
+ Sets the aspect ratio of the specified axis to square.
35
+ set_square_axes() -> None
36
+ Sets the aspect ratio of all axes in the current figure to square.
37
+
38
+ Examples
39
+ --------------------
40
+ >>> gs = GraphSquare()
41
+ >>> gs.set_square(0) # Set the first axis to square aspect ratio
42
+ >>> gs.set_square_axes() # Set all axes to square aspect ratio
43
+ """
44
+
45
+ def __init__(self) -> None:
46
+ self._axes: list[Axes] = plt.gcf().axes
47
+
48
+ def set_square(self, axis_target: int | Axes) -> None:
49
+ """
50
+ Sets the aspect ratio of the specified axis to square.
51
+
52
+ Parameters
53
+ --------------------
54
+ axis_target : int or matplotlib.axes.Axes
55
+ The target axis to adjust. Can be an axis index or a Matplotlib `Axes` object.
56
+
57
+ Returns
58
+ --------------------
59
+ None
60
+
61
+ Notes
62
+ --------------------
63
+ - Uses the `AxesResolver` to resolve the target axis.
64
+ - Sets the aspect ratio of the specified axis using `set_box_aspect`.
65
+
66
+ Examples
67
+ --------------------
68
+ >>> gs = GraphSquare()
69
+ >>> gs.set_square(0) # Set the first axis to square aspect ratio
70
+ """
71
+
72
+ axis: Axes = AxesResolver(axis_target).axis
73
+ axis.set_box_aspect(1)
74
+
75
+ def set_square_axes(self) -> None:
76
+ """
77
+ Sets the aspect ratio of all axes in the current figure to square.
78
+
79
+ This method iterates through all axes in the current figure and sets their
80
+ aspect ratio to square.
81
+
82
+ Returns
83
+ --------------------
84
+ None
85
+
86
+ Examples
87
+ --------------------
88
+ >>> gs = GraphSquare()
89
+ >>> gs.set_square_axes() # Set all axes to square aspect ratio
90
+ """
91
+ for axis in self._axes:
92
+ axis.set_box_aspect(1)
93
+
94
+
95
+ def graph_square(axis_target: int | Axes) -> None:
96
+ """
97
+ Sets the aspect ratio of a specified axis to square.
98
+
99
+ This function is a wrapper for the `set_square` method of the `GraphSquare` class.
100
+
101
+ Parameters
102
+ --------------------
103
+ axis_target : int or matplotlib.axes.Axes
104
+ The target axis to adjust. Can be an axis index or a Matplotlib `Axes` object.
105
+
106
+ Returns
107
+ --------------------
108
+ None
109
+
110
+ Examples
111
+ --------------------
112
+ >>> import gsplot as gs
113
+ >>> gs.graph_square(0) # Set the first axis to square aspect ratio
114
+ """
115
+ GraphSquare().set_square(axis_target)
116
+
117
+
118
+ def graph_square_axes() -> None:
119
+ """
120
+ Sets the aspect ratio of all axes in the current figure to square.
121
+
122
+ This function is a wrapper for the `set_square_axes` method of the `GraphSquare` class.
123
+
124
+ Returns
125
+ --------------------
126
+ None
127
+
128
+ Examples
129
+ --------------------
130
+ >>> import gsplot as gs
131
+ >>> gs.graph_square_axes() # Set all axes to square aspect ratio
132
+ """
133
+ GraphSquare().set_square_axes()
134
+
135
+
136
+ class GraphWhite:
137
+ """
138
+ A class to apply a white color scheme to Matplotlib axes.
139
+
140
+ This class provides methods to set the text, spines, and ticks of a single
141
+ axis or all axes in the current figure to white, making them suitable for
142
+ dark-themed backgrounds.
143
+
144
+ Attributes
145
+ --------------------
146
+ _axes : list[matplotlib.axes.Axes]
147
+ List of all axes in the current figure.
148
+
149
+ Methods
150
+ --------------------
151
+ set_white(axis_target: int | matplotlib.axes.Axes) -> None
152
+ Sets the text, spines, and ticks of the specified axis to white.
153
+ set_white_axes() -> None
154
+ Applies the white color scheme to all axes in the current figure.
155
+
156
+ Examples
157
+ --------------------
158
+ >>> gw = GraphWhite()
159
+ >>> gw.set_white(0) # Apply white color scheme to the first axis
160
+ >>> gw.set_white_axes() # Apply white color scheme to all axes
161
+ """
162
+
163
+ def __init__(self) -> None:
164
+ self._axes: list[Axes] = plt.gcf().axes
165
+
166
+ def set_white(self, axis_target: int | Axes) -> None:
167
+ """
168
+ Sets the text, spines, and ticks of the specified axis to white.
169
+
170
+ This method modifies the color of axis labels, title, spines, and ticks to white
171
+ and makes the axis background transparent.
172
+
173
+ Parameters
174
+ --------------------
175
+ axis_target : int or matplotlib.axes.Axes
176
+ The target axis to modify. Can be an axis index or a Matplotlib `Axes` object.
177
+
178
+ Returns
179
+ --------------------
180
+ None
181
+
182
+ Notes
183
+ --------------------
184
+ - Uses the `AxesResolver` to resolve the target axis.
185
+ - Sets transparency for the axis background by adjusting the patch alpha value.
186
+
187
+ Examples
188
+ --------------------
189
+ >>> gw = GraphWhite()
190
+ >>> gw.set_white(0) # Apply white color scheme to the first axis
191
+ """
192
+ axis: Axes = AxesResolver(axis_target).axis
193
+
194
+ axis.xaxis.label.set_color("w")
195
+ axis.yaxis.label.set_color("w")
196
+ axis.title.set_color("w")
197
+
198
+ for spine in axis.spines.values():
199
+ spine.set_edgecolor("w")
200
+
201
+ axis.tick_params(axis="x", which="both", colors="w")
202
+ axis.tick_params(axis="y", which="both", colors="w")
203
+
204
+ axis.patch.set_alpha(0)
205
+
206
+ def set_white_axes(self) -> None:
207
+ """
208
+ Applies the white color scheme to all axes in the current figure.
209
+
210
+ This method iterates through all axes in the current figure and modifies
211
+ their text, spines, and ticks to white, while also updating global text
212
+ color settings.
213
+
214
+ Returns
215
+ --------------------
216
+ None
217
+
218
+ Notes
219
+ --------------------
220
+ - Updates the `rcParams` to set the default text color to white.
221
+
222
+ Examples
223
+ --------------------
224
+ >>> gw = GraphWhite()
225
+ >>> gw.set_white_axes() # Apply white color scheme to all axes
226
+ """
227
+
228
+ rcParams["text.color"] = "w"
229
+ for axis_index in range(len(self._axes)):
230
+ self.set_white(axis_index)
231
+
232
+
233
+ def graph_white(axis_target: int | Axes) -> None:
234
+ """
235
+ Applies the white color scheme to a specified axis.
236
+
237
+ This function is a wrapper for the `set_white` method of the `GraphWhite` class.
238
+
239
+ Parameters
240
+ --------------------
241
+ axis_target : int or matplotlib.axes.Axes
242
+ The target axis to modify. Can be an axis index or a Matplotlib `Axes` object.
243
+
244
+ Returns
245
+ --------------------
246
+ None
247
+
248
+ Examples
249
+ --------------------
250
+ >>> import gsplot as gs
251
+ >>> gs.graph_white(0) # Apply white color scheme to the first axis
252
+ """
253
+ GraphWhite().set_white(axis_target)
254
+
255
+
256
+ def graph_white_axes() -> None:
257
+ """
258
+ Applies the white color scheme to all axes in the current figure.
259
+
260
+ This function is a wrapper for the `set_white_axes` method of the `GraphWhite` class.
261
+
262
+ Returns
263
+ --------------------
264
+ None
265
+
266
+ Examples
267
+ --------------------
268
+ >>> import gsplot as gs
269
+ >>> gs.graph_white_axes() # Apply white color scheme to all axes
270
+ """
271
+ GraphWhite().set_white_axes()
272
+
273
+
274
+ class GraphTransparent:
275
+ """
276
+ A class to apply transparency to Matplotlib figures and axes.
277
+
278
+ This class provides methods to set individual axes or all axes in the current
279
+ figure to be transparent. It also updates Matplotlib's `rcParams` for figure,
280
+ axes, and saved figure face colors to be transparent.
281
+
282
+ Attributes
283
+ --------------------
284
+ _axes : list[matplotlib.axes.Axes]
285
+ List of all axes in the current figure.
286
+
287
+ Methods
288
+ --------------------
289
+ set_transparent(axis_target: int | matplotlib.axes.Axes) -> None
290
+ Sets a specified axis to be transparent.
291
+ set_transparent_axes() -> None
292
+ Sets all axes in the current figure to be transparent.
293
+
294
+ Examples
295
+ --------------------
296
+ >>> gt = GraphTransparent()
297
+ >>> gt.set_transparent(0) # Set the first axis to be transparent
298
+ >>> gt.set_transparent_axes() # Set all axes to be transparent
299
+ """
300
+
301
+ def __init__(self) -> None:
302
+ self._axes: list[Axes] = plt.gcf().axes
303
+
304
+ rcParams.update(
305
+ {
306
+ "figure.facecolor": (1.0, 0.0, 0.0, 0),
307
+ "axes.facecolor": (0.0, 1.0, 0.0, 0),
308
+ "savefig.facecolor": (0.0, 0.0, 1.0, 0),
309
+ }
310
+ )
311
+
312
+ def set_transparent(self, axis_target: int | Axes) -> None:
313
+ """
314
+ Sets a specified axis to be transparent.
315
+
316
+ Parameters
317
+ --------------------
318
+ axis_target : int or matplotlib.axes.Axes
319
+ The target axis to modify. Can be an axis index or a Matplotlib `Axes` object.
320
+
321
+ Returns
322
+ --------------------
323
+ None
324
+
325
+ Notes
326
+ --------------------
327
+ - Uses `AxesResolver` to resolve the target axis.
328
+ - Modifies the `patch` attribute of the axis to make it transparent.
329
+
330
+ Examples
331
+ --------------------
332
+ >>> gt = GraphTransparent()
333
+ >>> gt.set_transparent(0) # Make the first axis transparent
334
+ """
335
+
336
+ axis: Axes = AxesResolver(axis_target).axis
337
+ axis.patch.set_alpha(0)
338
+
339
+ def set_transparent_axes(self) -> None:
340
+ """
341
+ Sets all axes in the current figure to be transparent.
342
+
343
+ This method iterates through all axes in the current figure and makes them
344
+ transparent.
345
+
346
+ Returns
347
+ --------------------
348
+ None
349
+
350
+ Examples
351
+ --------------------
352
+ >>> gt = GraphTransparent()
353
+ >>> gt.set_transparent_axes() # Make all axes transparent
354
+ """
355
+ for axis in self._axes:
356
+ self.set_transparent(axis)
357
+
358
+
359
+ def graph_transparent(axis_target: int | Axes) -> None:
360
+ """
361
+ Sets a specified axis to be transparent.
362
+
363
+ This function is a wrapper for the `set_transparent` method of the `GraphTransparent` class.
364
+
365
+ Parameters
366
+ --------------------
367
+ axis_target : int or matplotlib.axes.Axes
368
+ The target axis to modify. Can be an axis index or a Matplotlib `Axes` object.
369
+
370
+ Returns
371
+ --------------------
372
+ None
373
+
374
+ Examples
375
+ --------------------
376
+ >>> import gsplot as gs
377
+ >>> gs.graph_transparent(0) # Make the first axis transparent
378
+ """
379
+ GraphTransparent().set_transparent(axis_target)
380
+
381
+
382
+ def graph_transparent_axes() -> None:
383
+ """
384
+ Sets all axes in the current figure to be transparent.
385
+
386
+ This function is a wrapper for the `set_transparent_axes` method of the `GraphTransparent` class.
387
+
388
+ Returns
389
+ --------------------
390
+ None
391
+
392
+ Examples
393
+ --------------------
394
+ >>> import gsplot as gs
395
+ >>> gs.graph_transparent_axes() # Make all axes transparent
396
+ """
397
+ GraphTransparent().set_transparent_axes()
398
+
399
+
400
+ class GraphFaceColor:
401
+ """
402
+ A class to modify the face color of a Matplotlib figure.
403
+
404
+ This class provides a method to set the face color of the current figure.
405
+
406
+ Attributes
407
+ --------------------
408
+ fig : matplotlib.figure.Figure
409
+ The current figure object.
410
+
411
+ Methods
412
+ --------------------
413
+ set_facecolor(color: str = "black") -> None
414
+ Sets the face color of the current figure.
415
+
416
+ Examples
417
+ --------------------
418
+ >>> gfc = GraphFaceColor()
419
+ >>> gfc.set_facecolor("blue") # Set the face color of the figure to blue
420
+ """
421
+
422
+ def __init__(self) -> None:
423
+ self.fig: Figure = plt.gcf()
424
+
425
+ def set_facecolor(self, color: str = "black") -> None:
426
+ """
427
+ Sets the face color of the current figure.
428
+
429
+ Parameters
430
+ --------------------
431
+ color : str, optional
432
+ The desired face color, specified as a color string (default is "black").
433
+
434
+ Returns
435
+ --------------------
436
+ None
437
+
438
+ Examples
439
+ --------------------
440
+ >>> gfc = GraphFaceColor()
441
+ >>> gfc.set_facecolor("white") # Set the face color of the figure to white
442
+ """
443
+ self.fig.patch.set_facecolor(color)
444
+
445
+
446
+ def graph_facecolor(color: str = "black") -> None:
447
+ """
448
+ Sets the face color of the current figure.
449
+
450
+ This function is a wrapper for the `set_facecolor` method of the `GraphFaceColor` class.
451
+
452
+ Parameters
453
+ --------------------
454
+ color : str, optional
455
+ The desired face color, specified as a color string (default is "black").
456
+
457
+ Returns
458
+ --------------------
459
+ None
460
+
461
+ Examples
462
+ --------------------
463
+ >>> import gsplot as gs
464
+ >>> gs.graph_facecolor("black") # Set the face color of the figure to black
465
+ """
466
+ GraphFaceColor().set_facecolor(color)