ggplot2-python 4.0.2.9000__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 (54) hide show
  1. ggplot2_py/__init__.py +852 -0
  2. ggplot2_py/_compat.py +475 -0
  3. ggplot2_py/_plugins.py +129 -0
  4. ggplot2_py/_utils.py +544 -0
  5. ggplot2_py/aes.py +586 -0
  6. ggplot2_py/annotation.py +540 -0
  7. ggplot2_py/coord.py +2108 -0
  8. ggplot2_py/coords/__init__.py +49 -0
  9. ggplot2_py/datasets.py +265 -0
  10. ggplot2_py/draw_key.py +454 -0
  11. ggplot2_py/facet.py +1456 -0
  12. ggplot2_py/fortify.py +95 -0
  13. ggplot2_py/geom.py +4516 -0
  14. ggplot2_py/geoms/__init__.py +12 -0
  15. ggplot2_py/ggproto.py +279 -0
  16. ggplot2_py/guide.py +2925 -0
  17. ggplot2_py/guide_axis.py +615 -0
  18. ggplot2_py/guide_colourbar.py +657 -0
  19. ggplot2_py/guide_legend.py +1061 -0
  20. ggplot2_py/guides/__init__.py +8 -0
  21. ggplot2_py/labeller.py +296 -0
  22. ggplot2_py/labels.py +309 -0
  23. ggplot2_py/layer.py +954 -0
  24. ggplot2_py/layout.py +754 -0
  25. ggplot2_py/limits.py +314 -0
  26. ggplot2_py/plot.py +1401 -0
  27. ggplot2_py/plot_render.py +866 -0
  28. ggplot2_py/position.py +1269 -0
  29. ggplot2_py/protocols.py +171 -0
  30. ggplot2_py/py.typed +0 -0
  31. ggplot2_py/qplot.py +233 -0
  32. ggplot2_py/resources/diamonds.csv +53941 -0
  33. ggplot2_py/resources/economics.csv +575 -0
  34. ggplot2_py/resources/economics_long.csv +2871 -0
  35. ggplot2_py/resources/faithfuld.csv +5626 -0
  36. ggplot2_py/resources/luv_colours.csv +658 -0
  37. ggplot2_py/resources/midwest.csv +438 -0
  38. ggplot2_py/resources/mpg.csv +235 -0
  39. ggplot2_py/resources/msleep.csv +84 -0
  40. ggplot2_py/resources/presidential.csv +13 -0
  41. ggplot2_py/resources/seals.csv +1156 -0
  42. ggplot2_py/resources/txhousing.csv +8603 -0
  43. ggplot2_py/save.py +316 -0
  44. ggplot2_py/scale.py +2727 -0
  45. ggplot2_py/scales/__init__.py +4252 -0
  46. ggplot2_py/stat.py +6071 -0
  47. ggplot2_py/stats/__init__.py +9 -0
  48. ggplot2_py/theme.py +490 -0
  49. ggplot2_py/theme_defaults.py +1350 -0
  50. ggplot2_py/theme_elements.py +2052 -0
  51. ggplot2_python-4.0.2.9000.dist-info/METADATA +179 -0
  52. ggplot2_python-4.0.2.9000.dist-info/RECORD +54 -0
  53. ggplot2_python-4.0.2.9000.dist-info/WHEEL +4 -0
  54. ggplot2_python-4.0.2.9000.dist-info/licenses/LICENSE +3 -0
@@ -0,0 +1,12 @@
1
+ """
2
+ Re-exports all geom classes and constructor functions.
3
+
4
+ This sub-package exists for organisational convenience; everything is
5
+ defined in :mod:`ggplot2_py.geom`.
6
+
7
+ Examples
8
+ --------
9
+ >>> from ggplot2_py.geoms import geom_point, GeomPoint
10
+ """
11
+
12
+ from ggplot2_py.geom import * # noqa: F401,F403
ggplot2_py/ggproto.py ADDED
@@ -0,0 +1,279 @@
1
+ """
2
+ ggproto object system for ggplot2.
3
+
4
+ In R, ggproto is a prototype-based OOP system built on top of
5
+ environments. This Python port uses standard classes with a thin
6
+ compatibility layer so that:
7
+
8
+ * ``ggproto("ClassName", ParentClass, method=..., attr=...)`` dynamically
9
+ creates a new class (like ``type()``).
10
+ * Instances can override class-level members (prototype semantics).
11
+ * ``ggproto_parent(Parent, self)`` provides explicit parent-method
12
+ dispatch (similar to ``ggproto_parent()`` in R).
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import types
18
+ from typing import Any, Callable, Dict, Optional, Type, Union
19
+
20
+ __all__ = [
21
+ "GGProto",
22
+ "ggproto",
23
+ "ggproto_parent",
24
+ "is_ggproto",
25
+ "fetch_ggproto",
26
+ ]
27
+
28
+
29
+ # ---------------------------------------------------------------------------
30
+ # Base class
31
+ # ---------------------------------------------------------------------------
32
+
33
+ class GGProtoMeta(type):
34
+ """Metaclass for GGProto that makes class-level attribute access
35
+ work like instance-level access for method binding.
36
+
37
+ This supports the R pattern where e.g. ``GeomPoint`` is used both
38
+ as a class *and* as a singleton object.
39
+ """
40
+
41
+ def __repr__(cls) -> str:
42
+ return f"<ggproto class: {cls.__name__}>"
43
+
44
+
45
+ class GGProto(metaclass=GGProtoMeta):
46
+ """Base class for ggplot2's proto-based objects.
47
+
48
+ ``GGProto`` is the foundation of ggplot2's OOP. Subclasses
49
+ represent geoms, stats, scales, coords, facets, etc. Both classes
50
+ and instances are used interchangeably in R; this Python port
51
+ preserves that duality.
52
+
53
+ Attributes
54
+ ----------
55
+ _class_name : str or None
56
+ Optional explicit name set by the ``ggproto()`` factory.
57
+
58
+ Examples
59
+ --------
60
+ Defining a geom-like object:
61
+
62
+ >>> MyGeom = ggproto(
63
+ ... "MyGeom", GGProto,
64
+ ... required_aes={"x", "y"},
65
+ ... draw_panel=lambda self, data, params: data,
66
+ ... )
67
+ """
68
+
69
+ _class_name: Optional[str] = None
70
+
71
+ def __init_subclass__(cls, **kwargs: Any) -> None:
72
+ super().__init_subclass__(**kwargs)
73
+
74
+ def __repr__(self) -> str:
75
+ cls_name = self._class_name or type(self).__name__
76
+ return f"<ggproto object: {cls_name}>"
77
+
78
+ # Allow instance-level member override (prototype semantics).
79
+ def _set(self, **members: Any) -> None:
80
+ """Override members on this instance.
81
+
82
+ Parameters
83
+ ----------
84
+ **members : Any
85
+ Name/value pairs. Callables are stored as-is (they will be
86
+ bound when accessed via ``__getattribute__``).
87
+ """
88
+ for name, value in members.items():
89
+ object.__setattr__(self, name, value)
90
+
91
+ def __getattribute__(self, name: str) -> Any:
92
+ """Attribute access with auto-binding of plain functions.
93
+
94
+ When a plain function (not already a bound method) is retrieved
95
+ from the instance ``__dict__`` or from the class, it is bound so
96
+ that ``self`` is passed automatically — matching R ggproto
97
+ behaviour.
98
+ """
99
+ value = super().__getattribute__(name)
100
+ # Only auto-bind plain functions, not built-ins, lambdas stored
101
+ # intentionally without self, classmethods, staticmethods, etc.
102
+ if isinstance(value, types.FunctionType) and not name.startswith("_"):
103
+ # Check how many arguments the function expects.
104
+ code = value.__code__
105
+ nargs = code.co_argcount
106
+ # Only bind if the first parameter is named 'self'.
107
+ varnames = code.co_varnames[:nargs]
108
+ if nargs > 0 and varnames[0] == "self":
109
+ return types.MethodType(value, self)
110
+ return value
111
+
112
+
113
+ # ---------------------------------------------------------------------------
114
+ # Factory function
115
+ # ---------------------------------------------------------------------------
116
+
117
+ def ggproto(
118
+ _class: str,
119
+ _inherit: Optional[Type[GGProto]] = None,
120
+ **members: Any,
121
+ ) -> Type[GGProto]:
122
+ """Create a new ``GGProto`` subclass dynamically.
123
+
124
+ This mirrors the R ``ggproto()`` call. The returned object is a
125
+ **class** (not an instance), but thanks to the metaclass it can be
126
+ used in both class-like and instance-like ways.
127
+
128
+ Parameters
129
+ ----------
130
+ _class : str
131
+ Name for the new class.
132
+ _inherit : type, optional
133
+ Parent class. Defaults to ``GGProto``.
134
+ **members : Any
135
+ Attributes and methods to set on the class. Functions whose
136
+ first parameter is named ``self`` will behave as instance
137
+ methods.
138
+
139
+ Returns
140
+ -------
141
+ type
142
+ A new ``GGProto`` subclass.
143
+
144
+ Examples
145
+ --------
146
+ >>> Geom = ggproto("Geom", GGProto, draw_panel=lambda self, data: data)
147
+ >>> Point = ggproto("Point", Geom, shape="circle")
148
+ """
149
+ if _inherit is None:
150
+ _inherit = GGProto
151
+
152
+ # Build the class namespace.
153
+ namespace: Dict[str, Any] = {"_class_name": _class}
154
+ namespace.update(members)
155
+
156
+ # Create the class via the metaclass.
157
+ new_cls = GGProtoMeta(_class, (_inherit,), namespace)
158
+ return new_cls
159
+
160
+
161
+ # ---------------------------------------------------------------------------
162
+ # Parent-method dispatch
163
+ # ---------------------------------------------------------------------------
164
+
165
+ class _GGProtoParentProxy:
166
+ """Proxy object for calling parent-class methods with a bound *self*.
167
+
168
+ Returned by ``ggproto_parent()``. Attribute access on this proxy
169
+ retrieves the method from the parent class and binds it to the
170
+ original *self* instance (or class).
171
+ """
172
+
173
+ __slots__ = ("_parent", "_self")
174
+
175
+ def __init__(self, parent: Type[GGProto], self_obj: Any) -> None:
176
+ object.__setattr__(self, "_parent", parent)
177
+ object.__setattr__(self, "_self", self_obj)
178
+
179
+ def __repr__(self) -> str:
180
+ parent = object.__getattribute__(self, "_parent")
181
+ return f"<ggproto parent proxy: {parent.__name__}>"
182
+
183
+ def __getattr__(self, name: str) -> Any:
184
+ parent = object.__getattribute__(self, "_parent")
185
+ self_obj = object.__getattribute__(self, "_self")
186
+
187
+ # Walk the parent's MRO to find the attribute.
188
+ for cls in parent.__mro__:
189
+ if name in cls.__dict__:
190
+ value = cls.__dict__[name]
191
+ # Bind functions to self_obj.
192
+ if isinstance(value, types.FunctionType):
193
+ return types.MethodType(value, self_obj)
194
+ return value
195
+ raise AttributeError(
196
+ f"'{parent.__name__}' ggproto object has no member '{name}'"
197
+ )
198
+
199
+
200
+ def ggproto_parent(
201
+ parent: Type[GGProto],
202
+ self: Any,
203
+ ) -> _GGProtoParentProxy:
204
+ """Get a proxy for calling parent-class methods.
205
+
206
+ This is the Python equivalent of R's ``ggproto_parent(Parent, self)``.
207
+ Methods accessed through the proxy will be bound to *self*.
208
+
209
+ Parameters
210
+ ----------
211
+ parent : type
212
+ The parent ``GGProto`` class whose methods should be called.
213
+ self : GGProto
214
+ The current object (``self`` in the calling method).
215
+
216
+ Returns
217
+ -------
218
+ _GGProtoParentProxy
219
+ A proxy that dispatches attribute access to *parent*'s methods.
220
+
221
+ Examples
222
+ --------
223
+ Inside a ggproto method:
224
+
225
+ >>> def draw_panel(self, data, params):
226
+ ... data = ggproto_parent(Geom, self).draw_panel(data, params)
227
+ ... return data
228
+ """
229
+ return _GGProtoParentProxy(parent, self)
230
+
231
+
232
+ # ---------------------------------------------------------------------------
233
+ # Predicates / accessors
234
+ # ---------------------------------------------------------------------------
235
+
236
+ def is_ggproto(x: Any) -> bool:
237
+ """Check whether *x* is a ``GGProto`` class or instance.
238
+
239
+ Parameters
240
+ ----------
241
+ x : Any
242
+ Object to test.
243
+
244
+ Returns
245
+ -------
246
+ bool
247
+ ``True`` if *x* is a ``GGProto`` instance **or** a subclass of
248
+ ``GGProto``.
249
+ """
250
+ if isinstance(x, GGProto):
251
+ return True
252
+ if isinstance(x, type) and issubclass(x, GGProto):
253
+ return True
254
+ return False
255
+
256
+
257
+ def fetch_ggproto(x: Any, name: str) -> Any:
258
+ """Retrieve a member from a ``GGProto`` object.
259
+
260
+ Parameters
261
+ ----------
262
+ x : GGProto
263
+ A ``GGProto`` class or instance.
264
+ name : str
265
+ Member name.
266
+
267
+ Returns
268
+ -------
269
+ Any
270
+ The member value.
271
+
272
+ Raises
273
+ ------
274
+ AttributeError
275
+ If the member does not exist.
276
+ """
277
+ if not is_ggproto(x):
278
+ raise TypeError(f"Expected a GGProto object, got {type(x).__name__}")
279
+ return getattr(x, name)