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.
- ggplot2_py/__init__.py +852 -0
- ggplot2_py/_compat.py +475 -0
- ggplot2_py/_plugins.py +129 -0
- ggplot2_py/_utils.py +544 -0
- ggplot2_py/aes.py +586 -0
- ggplot2_py/annotation.py +540 -0
- ggplot2_py/coord.py +2108 -0
- ggplot2_py/coords/__init__.py +49 -0
- ggplot2_py/datasets.py +265 -0
- ggplot2_py/draw_key.py +454 -0
- ggplot2_py/facet.py +1456 -0
- ggplot2_py/fortify.py +95 -0
- ggplot2_py/geom.py +4516 -0
- ggplot2_py/geoms/__init__.py +12 -0
- ggplot2_py/ggproto.py +279 -0
- ggplot2_py/guide.py +2925 -0
- ggplot2_py/guide_axis.py +615 -0
- ggplot2_py/guide_colourbar.py +657 -0
- ggplot2_py/guide_legend.py +1061 -0
- ggplot2_py/guides/__init__.py +8 -0
- ggplot2_py/labeller.py +296 -0
- ggplot2_py/labels.py +309 -0
- ggplot2_py/layer.py +954 -0
- ggplot2_py/layout.py +754 -0
- ggplot2_py/limits.py +314 -0
- ggplot2_py/plot.py +1401 -0
- ggplot2_py/plot_render.py +866 -0
- ggplot2_py/position.py +1269 -0
- ggplot2_py/protocols.py +171 -0
- ggplot2_py/py.typed +0 -0
- ggplot2_py/qplot.py +233 -0
- ggplot2_py/resources/diamonds.csv +53941 -0
- ggplot2_py/resources/economics.csv +575 -0
- ggplot2_py/resources/economics_long.csv +2871 -0
- ggplot2_py/resources/faithfuld.csv +5626 -0
- ggplot2_py/resources/luv_colours.csv +658 -0
- ggplot2_py/resources/midwest.csv +438 -0
- ggplot2_py/resources/mpg.csv +235 -0
- ggplot2_py/resources/msleep.csv +84 -0
- ggplot2_py/resources/presidential.csv +13 -0
- ggplot2_py/resources/seals.csv +1156 -0
- ggplot2_py/resources/txhousing.csv +8603 -0
- ggplot2_py/save.py +316 -0
- ggplot2_py/scale.py +2727 -0
- ggplot2_py/scales/__init__.py +4252 -0
- ggplot2_py/stat.py +6071 -0
- ggplot2_py/stats/__init__.py +9 -0
- ggplot2_py/theme.py +490 -0
- ggplot2_py/theme_defaults.py +1350 -0
- ggplot2_py/theme_elements.py +2052 -0
- ggplot2_python-4.0.2.9000.dist-info/METADATA +179 -0
- ggplot2_python-4.0.2.9000.dist-info/RECORD +54 -0
- ggplot2_python-4.0.2.9000.dist-info/WHEEL +4 -0
- ggplot2_python-4.0.2.9000.dist-info/licenses/LICENSE +3 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Coordinate systems for ggplot2.
|
|
3
|
+
|
|
4
|
+
Re-exports everything from :mod:`ggplot2_py.coord`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from ggplot2_py.coord import ( # noqa: F401
|
|
8
|
+
Coord,
|
|
9
|
+
CoordCartesian,
|
|
10
|
+
CoordFixed,
|
|
11
|
+
CoordFlip,
|
|
12
|
+
CoordPolar,
|
|
13
|
+
CoordRadial,
|
|
14
|
+
CoordTrans,
|
|
15
|
+
CoordTransform,
|
|
16
|
+
coord_cartesian,
|
|
17
|
+
coord_equal,
|
|
18
|
+
coord_fixed,
|
|
19
|
+
coord_flip,
|
|
20
|
+
coord_polar,
|
|
21
|
+
coord_radial,
|
|
22
|
+
coord_trans,
|
|
23
|
+
coord_transform,
|
|
24
|
+
coord_munch,
|
|
25
|
+
is_coord,
|
|
26
|
+
is_Coord,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"Coord",
|
|
31
|
+
"CoordCartesian",
|
|
32
|
+
"CoordFixed",
|
|
33
|
+
"CoordFlip",
|
|
34
|
+
"CoordPolar",
|
|
35
|
+
"CoordRadial",
|
|
36
|
+
"CoordTrans",
|
|
37
|
+
"CoordTransform",
|
|
38
|
+
"coord_cartesian",
|
|
39
|
+
"coord_equal",
|
|
40
|
+
"coord_fixed",
|
|
41
|
+
"coord_flip",
|
|
42
|
+
"coord_polar",
|
|
43
|
+
"coord_radial",
|
|
44
|
+
"coord_trans",
|
|
45
|
+
"coord_transform",
|
|
46
|
+
"coord_munch",
|
|
47
|
+
"is_coord",
|
|
48
|
+
"is_Coord",
|
|
49
|
+
]
|
ggplot2_py/datasets.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Built-in datasets for ggplot2_py.
|
|
3
|
+
|
|
4
|
+
Provides lazy-loaded access to the classic ggplot2 example datasets.
|
|
5
|
+
CSV files are stored in the ``resources/`` subdirectory and loaded on
|
|
6
|
+
first access via module-level ``__getattr__``.
|
|
7
|
+
|
|
8
|
+
Examples
|
|
9
|
+
--------
|
|
10
|
+
>>> from ggplot2_py.datasets import mpg
|
|
11
|
+
>>> mpg.head()
|
|
12
|
+
|
|
13
|
+
>>> from ggplot2_py.datasets import load_dataset
|
|
14
|
+
>>> df = load_dataset("diamonds")
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import importlib.resources as _resources
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Dict, Optional
|
|
22
|
+
|
|
23
|
+
import pandas as pd
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"load_dataset",
|
|
27
|
+
"diamonds",
|
|
28
|
+
"economics",
|
|
29
|
+
"economics_long",
|
|
30
|
+
"faithfuld",
|
|
31
|
+
"luv_colours",
|
|
32
|
+
"midwest",
|
|
33
|
+
"mpg",
|
|
34
|
+
"msleep",
|
|
35
|
+
"presidential",
|
|
36
|
+
"seals",
|
|
37
|
+
"txhousing",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
# ---------------------------------------------------------------------------
|
|
41
|
+
# Dataset names recognised by this module
|
|
42
|
+
# ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
_DATASET_NAMES = frozenset(
|
|
45
|
+
{
|
|
46
|
+
"diamonds",
|
|
47
|
+
"economics",
|
|
48
|
+
"economics_long",
|
|
49
|
+
"faithfuld",
|
|
50
|
+
"luv_colours",
|
|
51
|
+
"midwest",
|
|
52
|
+
"mpg",
|
|
53
|
+
"msleep",
|
|
54
|
+
"presidential",
|
|
55
|
+
"seals",
|
|
56
|
+
"txhousing",
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# Cache: dataset name -> DataFrame (populated on first access)
|
|
61
|
+
_cache: Dict[str, pd.DataFrame] = {}
|
|
62
|
+
|
|
63
|
+
# ---------------------------------------------------------------------------
|
|
64
|
+
# Resource path helper
|
|
65
|
+
# ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
def _resources_dir() -> Path:
|
|
68
|
+
"""Return the path to the ``resources/`` directory shipped with the package.
|
|
69
|
+
|
|
70
|
+
Returns
|
|
71
|
+
-------
|
|
72
|
+
Path
|
|
73
|
+
Absolute path to the resources directory.
|
|
74
|
+
"""
|
|
75
|
+
return Path(__file__).resolve().parent / "resources"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ---------------------------------------------------------------------------
|
|
79
|
+
# Post-load fixups
|
|
80
|
+
# ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
def _fixup_diamonds(df: pd.DataFrame) -> pd.DataFrame:
|
|
83
|
+
"""Apply ordered-categorical dtypes to the *diamonds* dataset.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
df : pd.DataFrame
|
|
88
|
+
Raw diamonds data.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
-------
|
|
92
|
+
pd.DataFrame
|
|
93
|
+
DataFrame with ``cut``, ``color``, and ``clarity`` as ordered
|
|
94
|
+
categoricals.
|
|
95
|
+
"""
|
|
96
|
+
cut_order = ["Fair", "Good", "Very Good", "Premium", "Ideal"]
|
|
97
|
+
color_order = ["D", "E", "F", "G", "H", "I", "J"]
|
|
98
|
+
clarity_order = ["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"]
|
|
99
|
+
|
|
100
|
+
df["cut"] = pd.Categorical(df["cut"], categories=cut_order, ordered=True)
|
|
101
|
+
df["color"] = pd.Categorical(df["color"], categories=color_order, ordered=True)
|
|
102
|
+
df["clarity"] = pd.Categorical(
|
|
103
|
+
df["clarity"], categories=clarity_order, ordered=True
|
|
104
|
+
)
|
|
105
|
+
return df
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _fixup_economics(df: pd.DataFrame) -> pd.DataFrame:
|
|
109
|
+
"""Parse the ``date`` column in *economics* as ``datetime64``.
|
|
110
|
+
|
|
111
|
+
Parameters
|
|
112
|
+
----------
|
|
113
|
+
df : pd.DataFrame
|
|
114
|
+
Raw economics data.
|
|
115
|
+
|
|
116
|
+
Returns
|
|
117
|
+
-------
|
|
118
|
+
pd.DataFrame
|
|
119
|
+
DataFrame with ``date`` parsed as datetime.
|
|
120
|
+
"""
|
|
121
|
+
df["date"] = pd.to_datetime(df["date"])
|
|
122
|
+
return df
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _fixup_economics_long(df: pd.DataFrame) -> pd.DataFrame:
|
|
126
|
+
"""Parse the ``date`` column in *economics_long* as ``datetime64``.
|
|
127
|
+
|
|
128
|
+
Parameters
|
|
129
|
+
----------
|
|
130
|
+
df : pd.DataFrame
|
|
131
|
+
Raw economics_long data.
|
|
132
|
+
|
|
133
|
+
Returns
|
|
134
|
+
-------
|
|
135
|
+
pd.DataFrame
|
|
136
|
+
DataFrame with ``date`` parsed as datetime.
|
|
137
|
+
"""
|
|
138
|
+
df["date"] = pd.to_datetime(df["date"])
|
|
139
|
+
return df
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _fixup_presidential(df: pd.DataFrame) -> pd.DataFrame:
|
|
143
|
+
"""Parse ``start`` and ``end`` columns in *presidential* as ``datetime64``.
|
|
144
|
+
|
|
145
|
+
Parameters
|
|
146
|
+
----------
|
|
147
|
+
df : pd.DataFrame
|
|
148
|
+
Raw presidential data.
|
|
149
|
+
|
|
150
|
+
Returns
|
|
151
|
+
-------
|
|
152
|
+
pd.DataFrame
|
|
153
|
+
DataFrame with ``start`` and ``end`` parsed as datetime.
|
|
154
|
+
"""
|
|
155
|
+
df["start"] = pd.to_datetime(df["start"])
|
|
156
|
+
df["end"] = pd.to_datetime(df["end"])
|
|
157
|
+
return df
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _fixup_txhousing(df: pd.DataFrame) -> pd.DataFrame:
|
|
161
|
+
"""Ensure ``year`` and ``month`` are integer columns in *txhousing*.
|
|
162
|
+
|
|
163
|
+
Parameters
|
|
164
|
+
----------
|
|
165
|
+
df : pd.DataFrame
|
|
166
|
+
Raw txhousing data.
|
|
167
|
+
|
|
168
|
+
Returns
|
|
169
|
+
-------
|
|
170
|
+
pd.DataFrame
|
|
171
|
+
DataFrame with ``year`` and ``month`` as integers.
|
|
172
|
+
"""
|
|
173
|
+
df["year"] = df["year"].astype(int)
|
|
174
|
+
df["month"] = df["month"].astype(int)
|
|
175
|
+
return df
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
# Registry of fixup functions keyed by dataset name.
|
|
179
|
+
_FIXUPS = {
|
|
180
|
+
"diamonds": _fixup_diamonds,
|
|
181
|
+
"economics": _fixup_economics,
|
|
182
|
+
"economics_long": _fixup_economics_long,
|
|
183
|
+
"presidential": _fixup_presidential,
|
|
184
|
+
"txhousing": _fixup_txhousing,
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
# ---------------------------------------------------------------------------
|
|
188
|
+
# Public API
|
|
189
|
+
# ---------------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def load_dataset(name: str) -> pd.DataFrame:
|
|
193
|
+
"""Load a built-in ggplot2 dataset by name.
|
|
194
|
+
|
|
195
|
+
The dataset is read from CSV on the first call and cached for subsequent
|
|
196
|
+
requests.
|
|
197
|
+
|
|
198
|
+
Parameters
|
|
199
|
+
----------
|
|
200
|
+
name : str
|
|
201
|
+
Name of the dataset. Must be one of: ``diamonds``, ``economics``,
|
|
202
|
+
``economics_long``, ``faithfuld``, ``luv_colours``, ``midwest``,
|
|
203
|
+
``mpg``, ``msleep``, ``presidential``, ``seals``, ``txhousing``.
|
|
204
|
+
|
|
205
|
+
Returns
|
|
206
|
+
-------
|
|
207
|
+
pd.DataFrame
|
|
208
|
+
The requested dataset as a pandas DataFrame.
|
|
209
|
+
|
|
210
|
+
Raises
|
|
211
|
+
------
|
|
212
|
+
ValueError
|
|
213
|
+
If *name* is not a recognised dataset name.
|
|
214
|
+
|
|
215
|
+
Examples
|
|
216
|
+
--------
|
|
217
|
+
>>> df = load_dataset("mpg")
|
|
218
|
+
>>> df.shape
|
|
219
|
+
(234, 11)
|
|
220
|
+
"""
|
|
221
|
+
if name not in _DATASET_NAMES:
|
|
222
|
+
raise ValueError(
|
|
223
|
+
f"Unknown dataset {name!r}. Available datasets: "
|
|
224
|
+
f"{sorted(_DATASET_NAMES)}"
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
if name not in _cache:
|
|
228
|
+
csv_path = _resources_dir() / f"{name}.csv"
|
|
229
|
+
df = pd.read_csv(csv_path)
|
|
230
|
+
|
|
231
|
+
fixup = _FIXUPS.get(name)
|
|
232
|
+
if fixup is not None:
|
|
233
|
+
df = fixup(df)
|
|
234
|
+
|
|
235
|
+
_cache[name] = df
|
|
236
|
+
|
|
237
|
+
return _cache[name].copy()
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
# ---------------------------------------------------------------------------
|
|
241
|
+
# Module-level lazy loading via __getattr__
|
|
242
|
+
# ---------------------------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def __getattr__(name: str) -> pd.DataFrame:
|
|
246
|
+
"""Lazily load a dataset when accessed as a module attribute.
|
|
247
|
+
|
|
248
|
+
Parameters
|
|
249
|
+
----------
|
|
250
|
+
name : str
|
|
251
|
+
Attribute name (must match a dataset name).
|
|
252
|
+
|
|
253
|
+
Returns
|
|
254
|
+
-------
|
|
255
|
+
pd.DataFrame
|
|
256
|
+
The requested dataset.
|
|
257
|
+
|
|
258
|
+
Raises
|
|
259
|
+
------
|
|
260
|
+
AttributeError
|
|
261
|
+
If *name* is not a recognised dataset name.
|
|
262
|
+
"""
|
|
263
|
+
if name in _DATASET_NAMES:
|
|
264
|
+
return load_dataset(name)
|
|
265
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|