unxts.interop.matplotlib 2.0.0__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.
- unxts_interop_matplotlib-2.0.0/.gitignore +166 -0
- unxts_interop_matplotlib-2.0.0/LICENSE +29 -0
- unxts_interop_matplotlib-2.0.0/PKG-INFO +44 -0
- unxts_interop_matplotlib-2.0.0/README.md +17 -0
- unxts_interop_matplotlib-2.0.0/docs/api.md +31 -0
- unxts_interop_matplotlib-2.0.0/docs/guide.md +58 -0
- unxts_interop_matplotlib-2.0.0/docs/index.md +75 -0
- unxts_interop_matplotlib-2.0.0/pyproject.toml +78 -0
- unxts_interop_matplotlib-2.0.0/src/unxts/interop/matplotlib/__init__.py +13 -0
- unxts_interop_matplotlib-2.0.0/src/unxts/interop/matplotlib/_src/__init__.py +0 -0
- unxts_interop_matplotlib-2.0.0/src/unxts/interop/matplotlib/_src/converter.py +87 -0
- unxts_interop_matplotlib-2.0.0/src/unxts/interop/matplotlib/_version.py +24 -0
- unxts_interop_matplotlib-2.0.0/src/unxts/interop/matplotlib/py.typed +0 -0
- unxts_interop_matplotlib-2.0.0/tests/test_converter.py +49 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# -----------------------------------------------
|
|
2
|
+
# Project-Specific
|
|
3
|
+
|
|
4
|
+
# Version files
|
|
5
|
+
_version.py
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# -----------------------------------------------
|
|
9
|
+
# General
|
|
10
|
+
|
|
11
|
+
# Byte-compiled / optimized / DLL files
|
|
12
|
+
__pycache__/
|
|
13
|
+
*.py[cod]
|
|
14
|
+
*$py.class
|
|
15
|
+
|
|
16
|
+
# C extensions
|
|
17
|
+
*.so
|
|
18
|
+
|
|
19
|
+
# Distribution / packaging
|
|
20
|
+
.Python
|
|
21
|
+
build/
|
|
22
|
+
develop-eggs/
|
|
23
|
+
dist/
|
|
24
|
+
downloads/
|
|
25
|
+
eggs/
|
|
26
|
+
.eggs/
|
|
27
|
+
lib/
|
|
28
|
+
lib64/
|
|
29
|
+
parts/
|
|
30
|
+
sdist/
|
|
31
|
+
var/
|
|
32
|
+
wheels/
|
|
33
|
+
share/python-wheels/
|
|
34
|
+
*.egg-info/
|
|
35
|
+
.installed.cfg
|
|
36
|
+
*.egg
|
|
37
|
+
MANIFEST
|
|
38
|
+
|
|
39
|
+
# PyInstaller
|
|
40
|
+
# Usually these files are written by a python script from a template
|
|
41
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
42
|
+
*.manifest
|
|
43
|
+
*.spec
|
|
44
|
+
|
|
45
|
+
# Installer logs
|
|
46
|
+
pip-log.txt
|
|
47
|
+
pip-delete-this-directory.txt
|
|
48
|
+
|
|
49
|
+
# Unit test / coverage reports
|
|
50
|
+
htmlcov/
|
|
51
|
+
.tox/
|
|
52
|
+
.nox/
|
|
53
|
+
.coverage
|
|
54
|
+
.coverage.*
|
|
55
|
+
.cache
|
|
56
|
+
nosetests.xml
|
|
57
|
+
coverage.xml
|
|
58
|
+
*.cover
|
|
59
|
+
*.py,cover
|
|
60
|
+
.hypothesis/
|
|
61
|
+
.pytest_cache/
|
|
62
|
+
cover/
|
|
63
|
+
|
|
64
|
+
# Translations
|
|
65
|
+
*.mo
|
|
66
|
+
*.pot
|
|
67
|
+
|
|
68
|
+
# Django stuff:
|
|
69
|
+
*.log
|
|
70
|
+
local_settings.py
|
|
71
|
+
db.sqlite3
|
|
72
|
+
db.sqlite3-journal
|
|
73
|
+
|
|
74
|
+
# Flask stuff:
|
|
75
|
+
instance/
|
|
76
|
+
.webassets-cache
|
|
77
|
+
|
|
78
|
+
# Scrapy stuff:
|
|
79
|
+
.scrapy
|
|
80
|
+
|
|
81
|
+
# Sphinx documentation
|
|
82
|
+
docs/_build/
|
|
83
|
+
docs/guides/perf.ipynb
|
|
84
|
+
|
|
85
|
+
# PyBuilder
|
|
86
|
+
.pybuilder/
|
|
87
|
+
target/
|
|
88
|
+
|
|
89
|
+
# Jupyter Notebook
|
|
90
|
+
.ipynb_checkpoints
|
|
91
|
+
|
|
92
|
+
# IPython
|
|
93
|
+
profile_default/
|
|
94
|
+
ipython_config.py
|
|
95
|
+
|
|
96
|
+
# pyenv
|
|
97
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
98
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
99
|
+
# .python-version
|
|
100
|
+
|
|
101
|
+
# pipenv
|
|
102
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
103
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
104
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
105
|
+
# install all needed dependencies.
|
|
106
|
+
#Pipfile.lock
|
|
107
|
+
|
|
108
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
109
|
+
__pypackages__/
|
|
110
|
+
|
|
111
|
+
# Celery stuff
|
|
112
|
+
celerybeat-schedule
|
|
113
|
+
celerybeat.pid
|
|
114
|
+
|
|
115
|
+
# SageMath parsed files
|
|
116
|
+
*.sage.py
|
|
117
|
+
|
|
118
|
+
# Environments
|
|
119
|
+
.env
|
|
120
|
+
.venv
|
|
121
|
+
env/
|
|
122
|
+
venv/
|
|
123
|
+
ENV/
|
|
124
|
+
env.bak/
|
|
125
|
+
venv.bak/
|
|
126
|
+
|
|
127
|
+
# Spyder project settings
|
|
128
|
+
.spyderproject
|
|
129
|
+
.spyproject
|
|
130
|
+
|
|
131
|
+
# Rope project settings
|
|
132
|
+
.ropeproject
|
|
133
|
+
|
|
134
|
+
# mkdocs documentation
|
|
135
|
+
/site
|
|
136
|
+
|
|
137
|
+
# mypy
|
|
138
|
+
.mypy_cache/
|
|
139
|
+
.dmypy.json
|
|
140
|
+
dmypy.json
|
|
141
|
+
|
|
142
|
+
# Pyre type checker
|
|
143
|
+
.pyre/
|
|
144
|
+
|
|
145
|
+
# pytype static type analyzer
|
|
146
|
+
.pytype/
|
|
147
|
+
|
|
148
|
+
# Cython debug symbols
|
|
149
|
+
cython_debug/
|
|
150
|
+
|
|
151
|
+
# ruff
|
|
152
|
+
.ruff_cache/
|
|
153
|
+
|
|
154
|
+
# OS specific stuff
|
|
155
|
+
.DS_Store
|
|
156
|
+
.DS_Store?
|
|
157
|
+
._*
|
|
158
|
+
.Spotlight-V100
|
|
159
|
+
.Trashes
|
|
160
|
+
ehthumbs.db
|
|
161
|
+
Thumbs.db
|
|
162
|
+
|
|
163
|
+
# Common editor files
|
|
164
|
+
*~
|
|
165
|
+
*.swp
|
|
166
|
+
/.codspeed
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Nathaniel Starkman.
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: unxts.interop.matplotlib
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: matplotlib integration for unxt (canonical: unxts.interop.matplotlib)
|
|
5
|
+
Project-URL: Bug Tracker, https://github.com/GalacticDynamics/unxt/issues
|
|
6
|
+
Project-URL: Homepage, https://github.com/GalacticDynamics/unxt
|
|
7
|
+
Author-email: GalacticDynamics <nstarman@users.noreply.github.com>, Nathaniel Starkman <nstarman@users.noreply.github.com>
|
|
8
|
+
License-Expression: BSD-3-Clause
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.12
|
|
23
|
+
Requires-Dist: matplotlib>=3.8
|
|
24
|
+
Requires-Dist: unxt>=2.0.0
|
|
25
|
+
Requires-Dist: zeroth>=1.0.0
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# unxts.interop.matplotlib
|
|
29
|
+
|
|
30
|
+
[matplotlib](https://matplotlib.org/) integration for [unxt](https://github.com/GalacticDynamics/unxt).
|
|
31
|
+
|
|
32
|
+
This is the canonical package (`unxts.interop.matplotlib`). It registers a `matplotlib.units.ConversionInterface` so `unxt.Quantity` objects can be plotted directly with `matplotlib`.
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install unxts.interop.matplotlib
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
import unxts.interop.matplotlib
|
|
44
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# unxts.interop.matplotlib
|
|
2
|
+
|
|
3
|
+
[matplotlib](https://matplotlib.org/) integration for [unxt](https://github.com/GalacticDynamics/unxt).
|
|
4
|
+
|
|
5
|
+
This is the canonical package (`unxts.interop.matplotlib`). It registers a `matplotlib.units.ConversionInterface` so `unxt.Quantity` objects can be plotted directly with `matplotlib`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install unxts.interop.matplotlib
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import unxts.interop.matplotlib
|
|
17
|
+
```
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# `unxts.interop.matplotlib` API
|
|
2
|
+
|
|
3
|
+
`unxts.interop.matplotlib` exposes the converter and a function to toggle it. Importing the package enables the converter automatically, so most users never need to call either directly.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from unxts.interop.matplotlib import (
|
|
7
|
+
UnxtConverter,
|
|
8
|
+
setup_matplotlib_support_for_unxt,
|
|
9
|
+
)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## `setup_matplotlib_support_for_unxt(*, enable=True)`
|
|
13
|
+
|
|
14
|
+
Register (or unregister) the `unxt` quantity converter with `matplotlib`. It is called with `enable=True` when the package is imported.
|
|
15
|
+
|
|
16
|
+
- `enable` (bool, keyword-only, default `True`): if `True`, register `UnxtConverter` for `unxt.quantity.AbstractQuantity` (which covers `Quantity` and the other quantity types) so quantities can be plotted; if `False`, remove the registration.
|
|
17
|
+
|
|
18
|
+
```{code-block} python
|
|
19
|
+
|
|
20
|
+
from unxts.interop.matplotlib import setup_matplotlib_support_for_unxt
|
|
21
|
+
|
|
22
|
+
# Stop matplotlib from converting Quantity objects
|
|
23
|
+
setup_matplotlib_support_for_unxt(enable=False)
|
|
24
|
+
|
|
25
|
+
# Re-enable (the default on import)
|
|
26
|
+
setup_matplotlib_support_for_unxt(enable=True)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## `UnxtConverter`
|
|
30
|
+
|
|
31
|
+
A `matplotlib.units.ConversionInterface` subclass that teaches `matplotlib` how to turn an `unxt` quantity (any `AbstractQuantity`) into plottable magnitudes (and to label axes with the unit). It is registered — for `AbstractQuantity` — by `setup_matplotlib_support_for_unxt`; you rarely instantiate it yourself.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# `matplotlib` Interoperability Guide
|
|
2
|
+
|
|
3
|
+
This guide shows how to plot `unxt.Quantity` objects with [matplotlib](https://matplotlib.org/).
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
Importing `unxts.interop.matplotlib` registers a `matplotlib.units.ConversionInterface` for `unxt.quantity.AbstractQuantity` (so `Quantity` and the other quantity types work). `unxt` imports the package automatically when it is installed, so plotting usually just works once the package is present:
|
|
8
|
+
|
|
9
|
+
```{code-block} python
|
|
10
|
+
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
import unxt as u
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Plotting quantities
|
|
16
|
+
|
|
17
|
+
Pass `Quantity` objects straight to `matplotlib` — the registered converter strips the units to their magnitudes for the axes:
|
|
18
|
+
|
|
19
|
+
```{code-block} python
|
|
20
|
+
|
|
21
|
+
import jax.numpy as jnp
|
|
22
|
+
|
|
23
|
+
x = u.Q(jnp.linspace(0, 360, 100), "deg")
|
|
24
|
+
y = u.Q(jnp.sin(x.ustrip("rad")), "")
|
|
25
|
+
|
|
26
|
+
plt.plot(x, y)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Using `quaxed.numpy`
|
|
30
|
+
|
|
31
|
+
With [`quaxed`](https://quaxed.readthedocs.io/)'s unit-aware `numpy` namespace, the intermediate values stay `Quantity` objects and units propagate through the computation:
|
|
32
|
+
|
|
33
|
+
```{code-block} python
|
|
34
|
+
|
|
35
|
+
import quaxed.numpy as jnp
|
|
36
|
+
|
|
37
|
+
x = u.Q(jnp.linspace(0, 360, 100), "deg")
|
|
38
|
+
y = jnp.sin(x)
|
|
39
|
+
|
|
40
|
+
plt.plot(x, y)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Disabling the converter
|
|
44
|
+
|
|
45
|
+
The converter is enabled on import. To turn it off (or back on) at runtime, use `setup_matplotlib_support_for_unxt` — see the [API reference](api):
|
|
46
|
+
|
|
47
|
+
```{code-block} python
|
|
48
|
+
|
|
49
|
+
from unxts.interop.matplotlib import setup_matplotlib_support_for_unxt
|
|
50
|
+
|
|
51
|
+
setup_matplotlib_support_for_unxt(enable=False) # stop converting Quantity
|
|
52
|
+
setup_matplotlib_support_for_unxt(enable=True) # re-enable
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## See Also
|
|
56
|
+
|
|
57
|
+
- [API reference](api) — the converter and its setup function
|
|
58
|
+
- [matplotlib units documentation](https://matplotlib.org/stable/gallery/units/index.html)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# `unxts.interop.matplotlib`
|
|
2
|
+
|
|
3
|
+
```{toctree}
|
|
4
|
+
:maxdepth: 1
|
|
5
|
+
:hidden:
|
|
6
|
+
|
|
7
|
+
guide
|
|
8
|
+
api
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`unxts.interop.matplotlib` is the canonical location for [matplotlib](https://matplotlib.org/) integration. Importing the package — or `unxt` itself, which imports it automatically when installed — registers a `matplotlib.units.ConversionInterface` for `AbstractQuantity`, so `unxt.Quantity` objects (and the other quantity types) can be plotted directly.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
The recommended install adds `unxts.interop.matplotlib` alongside `unxt` via the `interop-mpl` [extra](https://peps.python.org/pep-0508/#extras), so it, `unxt`, and `matplotlib` are resolved together as a compatible set:
|
|
16
|
+
|
|
17
|
+
::::{tab-set}
|
|
18
|
+
|
|
19
|
+
:::{tab-item} uv
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
uv add "unxt[interop-mpl]"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
:::
|
|
26
|
+
|
|
27
|
+
:::{tab-item} pip
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install "unxt[interop-mpl]"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
:::
|
|
34
|
+
|
|
35
|
+
::::
|
|
36
|
+
|
|
37
|
+
Or install the package directly:
|
|
38
|
+
|
|
39
|
+
::::{tab-set}
|
|
40
|
+
|
|
41
|
+
:::{tab-item} uv
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
uv add unxts.interop.matplotlib
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
:::
|
|
48
|
+
|
|
49
|
+
:::{tab-item} pip
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install unxts.interop.matplotlib
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
:::
|
|
56
|
+
|
|
57
|
+
::::
|
|
58
|
+
|
|
59
|
+
## Quick example
|
|
60
|
+
|
|
61
|
+
Once installed, plot `Quantity` objects directly with `matplotlib`:
|
|
62
|
+
|
|
63
|
+
```{code-block} python
|
|
64
|
+
|
|
65
|
+
import matplotlib.pyplot as plt
|
|
66
|
+
import jax.numpy as jnp
|
|
67
|
+
import unxt as u
|
|
68
|
+
|
|
69
|
+
x = u.Q(jnp.linspace(0, 360, 100), "deg")
|
|
70
|
+
y = u.Q(jnp.sin(x.ustrip("rad")), "")
|
|
71
|
+
|
|
72
|
+
plt.plot(x, y)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
See the [guide](guide) for more plotting patterns, and the [API reference](api) for toggling the converter.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
authors = [
|
|
3
|
+
{ name = "GalacticDynamics", email = "nstarman@users.noreply.github.com" },
|
|
4
|
+
{ name = "Nathaniel Starkman", email = "nstarman@users.noreply.github.com" },
|
|
5
|
+
]
|
|
6
|
+
classifiers = [
|
|
7
|
+
"Development Status :: 4 - Beta",
|
|
8
|
+
"Intended Audience :: Developers",
|
|
9
|
+
"Intended Audience :: Science/Research",
|
|
10
|
+
"Operating System :: OS Independent",
|
|
11
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
12
|
+
"Programming Language :: Python :: 3",
|
|
13
|
+
"Programming Language :: Python :: 3.12",
|
|
14
|
+
"Programming Language :: Python :: 3.13",
|
|
15
|
+
"Programming Language :: Python :: 3.14",
|
|
16
|
+
"Programming Language :: Python",
|
|
17
|
+
"Topic :: Scientific/Engineering",
|
|
18
|
+
"Typing :: Typed",
|
|
19
|
+
]
|
|
20
|
+
dependencies = ["matplotlib>=3.8", "unxt>=2.0.0", "zeroth>=1.0.0"]
|
|
21
|
+
description = "matplotlib integration for unxt (canonical: unxts.interop.matplotlib)"
|
|
22
|
+
dynamic = ["version"]
|
|
23
|
+
license = "BSD-3-Clause"
|
|
24
|
+
license-files = ["LICENSE"]
|
|
25
|
+
name = "unxts.interop.matplotlib"
|
|
26
|
+
readme = "README.md"
|
|
27
|
+
requires-python = ">=3.12"
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
"Bug Tracker" = "https://github.com/GalacticDynamics/unxt/issues"
|
|
31
|
+
Homepage = "https://github.com/GalacticDynamics/unxt"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
build-backend = "hatchling.build"
|
|
35
|
+
requires = ["hatch-vcs", "hatchling"]
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
test = [
|
|
39
|
+
"pytest>=8.4.2",
|
|
40
|
+
"pytest-arraydiff>=0.6.1",
|
|
41
|
+
"pytest-cov>=6.2.1",
|
|
42
|
+
"pytest-env>=1.1.5",
|
|
43
|
+
"pytest-github-actions-annotate-failures>=0.3.0",
|
|
44
|
+
"sybil[pytest]>=9.2.0",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.hatch.version]
|
|
48
|
+
source = "vcs"
|
|
49
|
+
|
|
50
|
+
[tool.hatch.version.raw-options]
|
|
51
|
+
local_scheme = "no-local-version"
|
|
52
|
+
root = "../.."
|
|
53
|
+
search_parent_directories = true
|
|
54
|
+
|
|
55
|
+
[tool.hatch.version.raw-options.scm.git]
|
|
56
|
+
describe_command = [
|
|
57
|
+
"git",
|
|
58
|
+
"describe",
|
|
59
|
+
"--dirty",
|
|
60
|
+
"--tags",
|
|
61
|
+
"--long",
|
|
62
|
+
"--match",
|
|
63
|
+
"unxts-interop-matplotlib-v*",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[tool.hatch.build.hooks.vcs]
|
|
67
|
+
version-file = "src/unxts/interop/matplotlib/_version.py"
|
|
68
|
+
version-file-template = """\
|
|
69
|
+
version: str = {version!r}
|
|
70
|
+
version_tuple: tuple[int, int, int] | tuple[int, int, int, str, str]
|
|
71
|
+
version_tuple = {version_tuple!r}
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
[tool.hatch.build.targets.wheel]
|
|
75
|
+
packages = ["src/unxts"]
|
|
76
|
+
|
|
77
|
+
[tool.uv.sources]
|
|
78
|
+
unxt = { workspace = true }
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""matplotlib integration for unxt.
|
|
2
|
+
|
|
3
|
+
This is the canonical package (unxts.interop.matplotlib). It registers a
|
|
4
|
+
`matplotlib.units.ConversionInterface` so `unxt.Quantity` objects can be
|
|
5
|
+
plotted directly with `matplotlib`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__all__ = ("UnxtConverter", "setup_matplotlib_support_for_unxt")
|
|
9
|
+
|
|
10
|
+
from ._src.converter import UnxtConverter, setup_matplotlib_support_for_unxt
|
|
11
|
+
from ._version import version as __version__ # noqa: F401
|
|
12
|
+
|
|
13
|
+
setup_matplotlib_support_for_unxt(enable=True)
|
|
File without changes
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Interoperability with `matplotlib`."""
|
|
2
|
+
# pylint: disable=import-error
|
|
3
|
+
|
|
4
|
+
# This module was adapted from both the `astropy` and `pint` package's
|
|
5
|
+
# implementations.
|
|
6
|
+
|
|
7
|
+
__all__ = ("UnxtConverter", "setup_matplotlib_support_for_unxt")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
from collections.abc import Iterable, Sized
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
import matplotlib.units
|
|
15
|
+
from jaxtyping import Array
|
|
16
|
+
from matplotlib.axes import Axes
|
|
17
|
+
|
|
18
|
+
from zeroth import zeroth
|
|
19
|
+
|
|
20
|
+
from unxt.quantity import AbstractQuantity, Quantity, ustrip
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class UnxtConverter(matplotlib.units.ConversionInterface): # type: ignore[misc]
|
|
25
|
+
"""Support `unxt` in `matplotlib`'s unit conversion framework.
|
|
26
|
+
|
|
27
|
+
This class is a subclass of `matplotlib.units.ConversionInterface`
|
|
28
|
+
and is used to convert `unxt.Quantity` instances for use with
|
|
29
|
+
`matplotlib`.
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
axisinfo_kw: dict[str, Any] = field(
|
|
34
|
+
default_factory=lambda: {"format": "latex_inline"}
|
|
35
|
+
)
|
|
36
|
+
"""Keyword arguments to use when making the :meth:`matplotlib.units.AxisInfo`."""
|
|
37
|
+
|
|
38
|
+
def convert(self, obj: Any, unit: Any, axis: Axes) -> Array | list[Array]:
|
|
39
|
+
"""Convert *obj* using *unit* for the specified *axis*."""
|
|
40
|
+
# Hot-path Quantity
|
|
41
|
+
if isinstance(obj, AbstractQuantity):
|
|
42
|
+
return ustrip(unit, obj)
|
|
43
|
+
# Need to recurse (singly) into iterables, but a 0-d array is nominally
|
|
44
|
+
# `Iterable` yet raises when iterated, so treat it as a scalar value.
|
|
45
|
+
if isinstance(obj, Iterable) and getattr(obj, "ndim", None) != 0:
|
|
46
|
+
return [self._convert_value(v, unit, axis) for v in obj]
|
|
47
|
+
|
|
48
|
+
return self._convert_value(obj, unit, axis)
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def _convert_value(obj: Any, unit: Any, axis: Axes) -> Array:
|
|
52
|
+
"""Handle converting using attached unit or falling back to axis units."""
|
|
53
|
+
if isinstance(obj, AbstractQuantity):
|
|
54
|
+
return ustrip(unit, obj)
|
|
55
|
+
|
|
56
|
+
return Quantity.from_(obj, axis.get_units()).ustrip(unit)
|
|
57
|
+
|
|
58
|
+
def axisinfo(self, unit: Any, _: Axes) -> matplotlib.units.AxisInfo:
|
|
59
|
+
"""Return axis information for this particular unit."""
|
|
60
|
+
# matplotlib may query axisinfo before any unit has been set on the axis.
|
|
61
|
+
if unit is None:
|
|
62
|
+
return matplotlib.units.AxisInfo()
|
|
63
|
+
fmt = self.axisinfo_kw.get("format", "latex_inline")
|
|
64
|
+
return matplotlib.units.AxisInfo(label=unit.to_string(fmt))
|
|
65
|
+
|
|
66
|
+
@staticmethod
|
|
67
|
+
def default_units(x: Any, _: Axes) -> Any:
|
|
68
|
+
"""Get the default unit to use for the given combination of unit and axis."""
|
|
69
|
+
if hasattr(x, "unit"):
|
|
70
|
+
return x.unit
|
|
71
|
+
if isinstance(x, Iterable) and isinstance(x, Sized):
|
|
72
|
+
x = zeroth(x)
|
|
73
|
+
# `unxt` quantities expose the singular `.unit` (not the astropy/pint
|
|
74
|
+
# `.units`), so an unwrapped element must be checked with `.unit`.
|
|
75
|
+
return getattr(x, "unit", getattr(x, "units", None))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def setup_matplotlib_support_for_unxt(*, enable: bool = True) -> None:
|
|
79
|
+
"""Set up matplotlib's unit support for `unxt`.
|
|
80
|
+
|
|
81
|
+
:param enable: Whether support should be enabled or disabled.
|
|
82
|
+
|
|
83
|
+
"""
|
|
84
|
+
if enable:
|
|
85
|
+
matplotlib.units.registry[AbstractQuantity] = UnxtConverter()
|
|
86
|
+
else:
|
|
87
|
+
matplotlib.units.registry.pop(AbstractQuantity, None)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '2.0.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (2, 0, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = None
|
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Tests for unxts.interop.matplotlib converter registration."""
|
|
2
|
+
|
|
3
|
+
import matplotlib
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import matplotlib.units
|
|
6
|
+
import numpy as np
|
|
7
|
+
import unxts.interop.matplotlib as uimpl
|
|
8
|
+
|
|
9
|
+
import unxt as u
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_converter_registered_on_import():
|
|
13
|
+
assert u.quantity.AbstractQuantity in matplotlib.units.registry
|
|
14
|
+
assert isinstance(
|
|
15
|
+
matplotlib.units.registry[u.quantity.AbstractQuantity], uimpl.UnxtConverter
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_converter_strips_units():
|
|
20
|
+
converter = uimpl.UnxtConverter()
|
|
21
|
+
q = u.Q([1.0, 2.0, 3.0], "km")
|
|
22
|
+
converted = list(converter.convert(q, u.unit("m"), axis=None))
|
|
23
|
+
assert converted == [1000.0, 2000.0, 3000.0]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_default_units_from_list_of_scalar_quantities():
|
|
27
|
+
"""Matplotlib splits array-likes into lists of scalar quantities."""
|
|
28
|
+
converter = uimpl.UnxtConverter()
|
|
29
|
+
q = u.Q([-1.0, 1.0, -1.0, 1.0], "mas")
|
|
30
|
+
assert converter.default_units([q[0], q[1]], None) == u.unit("mas")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_axisinfo_handles_none_unit():
|
|
34
|
+
"""Matplotlib may query axisinfo before a unit is set on the axis."""
|
|
35
|
+
converter = uimpl.UnxtConverter()
|
|
36
|
+
info = converter.axisinfo(None, None)
|
|
37
|
+
assert isinstance(info, matplotlib.units.AxisInfo)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_imshow_with_quantity_extent():
|
|
41
|
+
"""End-to-end: a Quantity ``extent`` should plot and label the axis."""
|
|
42
|
+
fig, ax = plt.subplots()
|
|
43
|
+
try:
|
|
44
|
+
ax.imshow(np.zeros((4, 4)), extent=u.Q([-8.0, 8.0, -8.0, 8.0], "mas"))
|
|
45
|
+
fig.canvas.draw()
|
|
46
|
+
assert ax.get_xlim() == (-8.0, 8.0)
|
|
47
|
+
assert "mas" in ax.get_xlabel()
|
|
48
|
+
finally:
|
|
49
|
+
plt.close(fig)
|