unxts.interop.matplotlib 2.0.1__tar.gz → 2.1.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.1 → unxts_interop_matplotlib-2.1.0}/.gitignore +4 -1
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/PKG-INFO +1 -1
- unxts_interop_matplotlib-2.1.0/docs/api.md +74 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/docs/guide.md +10 -7
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/docs/index.md +18 -4
- unxts_interop_matplotlib-2.1.0/docs/tutorial-first-plot.md +142 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/src/unxts/interop/matplotlib/_src/converter.py +28 -6
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/src/unxts/interop/matplotlib/_version.py +2 -2
- unxts_interop_matplotlib-2.1.0/tests/smoke/test_public_api.py +14 -0
- {unxts_interop_matplotlib-2.0.1/tests → unxts_interop_matplotlib-2.1.0/tests/unit}/test_converter.py +60 -1
- unxts_interop_matplotlib-2.0.1/docs/api.md +0 -31
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/LICENSE +0 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/README.md +0 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/pyproject.toml +0 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/src/unxts/interop/matplotlib/__init__.py +0 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/src/unxts/interop/matplotlib/_src/__init__.py +0 -0
- {unxts_interop_matplotlib-2.0.1 → unxts_interop_matplotlib-2.1.0}/src/unxts/interop/matplotlib/py.typed +0 -0
|
@@ -80,7 +80,7 @@ instance/
|
|
|
80
80
|
|
|
81
81
|
# Sphinx documentation
|
|
82
82
|
docs/_build/
|
|
83
|
-
docs/
|
|
83
|
+
docs/how-to/optimize-performance.ipynb
|
|
84
84
|
|
|
85
85
|
# PyBuilder
|
|
86
86
|
.pybuilder/
|
|
@@ -164,3 +164,6 @@ Thumbs.db
|
|
|
164
164
|
*~
|
|
165
165
|
*.swp
|
|
166
166
|
/.codspeed
|
|
167
|
+
|
|
168
|
+
# graphify knowledge graph (generated; `nox -s graph`)
|
|
169
|
+
/graphify-out/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: unxts.interop.matplotlib
|
|
3
|
-
Version: 2.0
|
|
3
|
+
Version: 2.1.0
|
|
4
4
|
Summary: matplotlib integration for unxt (canonical: unxts.interop.matplotlib)
|
|
5
5
|
Project-URL: Bug Tracker, https://github.com/GalacticDynamics/unxt/issues
|
|
6
6
|
Project-URL: Homepage, https://github.com/GalacticDynamics/unxt
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# 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.
|
|
32
|
+
|
|
33
|
+
| Field | Type | Default | Purpose |
|
|
34
|
+
| --- | --- | --- | --- |
|
|
35
|
+
| `unit_format` | `str` | `"latex_inline"` | Format spec passed to `Unit.to_string` when building the axis label. |
|
|
36
|
+
| `axisinfo_kw` | `dict \| None` | `None` | **Deprecated.** See below. |
|
|
37
|
+
|
|
38
|
+
```{code-block} python
|
|
39
|
+
>>> import unxt as u
|
|
40
|
+
>>> from unxts.interop.matplotlib import UnxtConverter
|
|
41
|
+
|
|
42
|
+
>>> UnxtConverter().unit_format
|
|
43
|
+
'latex_inline'
|
|
44
|
+
|
|
45
|
+
>>> UnxtConverter(unit_format="latex").unit_format
|
|
46
|
+
'latex'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The format controls how the unit is rendered on the axis:
|
|
50
|
+
|
|
51
|
+
```{code-block} python
|
|
52
|
+
>>> print(UnxtConverter().axisinfo(u.unit("km"), None).label)
|
|
53
|
+
$\mathrm{km}$
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
:::{deprecated} 2.0.0
|
|
57
|
+
|
|
58
|
+
`axisinfo_kw` is deprecated in favour of `unit_format` and will be removed in a future release. Passing it emits a `DeprecationWarning` and its `"format"` key is copied into `unit_format`:
|
|
59
|
+
|
|
60
|
+
```{code-block} python
|
|
61
|
+
>>> import warnings
|
|
62
|
+
|
|
63
|
+
>>> with warnings.catch_warnings(record=True) as caught:
|
|
64
|
+
... warnings.simplefilter("always")
|
|
65
|
+
... converter = UnxtConverter(axisinfo_kw={"format": "latex"})
|
|
66
|
+
>>> caught[0].category.__name__
|
|
67
|
+
'DeprecationWarning'
|
|
68
|
+
>>> converter.unit_format
|
|
69
|
+
'latex'
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Use `UnxtConverter(unit_format="latex")` instead.
|
|
73
|
+
|
|
74
|
+
:::
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# How to plot quantities with matplotlib
|
|
2
2
|
|
|
3
|
-
This guide shows how to plot `unxt.Quantity` objects with [matplotlib](https://matplotlib.org/).
|
|
3
|
+
This guide shows you how to plot `unxt.Quantity` objects with [matplotlib](https://matplotlib.org/).
|
|
4
4
|
|
|
5
5
|
## Setup
|
|
6
6
|
|
|
@@ -23,12 +23,13 @@ import jax.numpy as jnp
|
|
|
23
23
|
x = u.Q(jnp.linspace(0, 360, 100), "deg")
|
|
24
24
|
y = u.Q(jnp.sin(x.ustrip("rad")), "")
|
|
25
25
|
|
|
26
|
-
plt.
|
|
26
|
+
fig, ax = plt.subplots()
|
|
27
|
+
ax.plot(x, y)
|
|
27
28
|
```
|
|
28
29
|
|
|
29
30
|
## Using `quaxed.numpy`
|
|
30
31
|
|
|
31
|
-
With [`quaxed`](https://
|
|
32
|
+
With [`quaxed`](https://github.com/GalacticDynamics/quaxed)'s unit-aware `numpy` namespace, the intermediate values stay `Quantity` objects and units propagate through the computation:
|
|
32
33
|
|
|
33
34
|
```{code-block} python
|
|
34
35
|
|
|
@@ -37,7 +38,8 @@ import quaxed.numpy as jnp
|
|
|
37
38
|
x = u.Q(jnp.linspace(0, 360, 100), "deg")
|
|
38
39
|
y = jnp.sin(x)
|
|
39
40
|
|
|
40
|
-
plt.
|
|
41
|
+
fig, ax = plt.subplots()
|
|
42
|
+
ax.plot(x, y)
|
|
41
43
|
```
|
|
42
44
|
|
|
43
45
|
## Disabling the converter
|
|
@@ -52,7 +54,8 @@ setup_matplotlib_support_for_unxt(enable=False) # stop converting Quantity
|
|
|
52
54
|
setup_matplotlib_support_for_unxt(enable=True) # re-enable
|
|
53
55
|
```
|
|
54
56
|
|
|
55
|
-
## See
|
|
57
|
+
## See also
|
|
56
58
|
|
|
57
|
-
- [
|
|
59
|
+
- [Plot data that knows its units](tutorial-first-plot) — the lesson, if you have not plotted a quantity before.
|
|
60
|
+
- [API](api) — the converter and its setup function.
|
|
58
61
|
- [matplotlib units documentation](https://matplotlib.org/stable/gallery/units/index.html)
|
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
:maxdepth: 1
|
|
5
5
|
:hidden:
|
|
6
6
|
|
|
7
|
+
tutorial-first-plot
|
|
7
8
|
guide
|
|
8
9
|
api
|
|
9
10
|
```
|
|
10
11
|
|
|
11
12
|
`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
|
|
|
13
|
-
##
|
|
14
|
+
## Install
|
|
14
15
|
|
|
15
16
|
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
|
|
|
@@ -56,7 +57,7 @@ pip install unxts.interop.matplotlib
|
|
|
56
57
|
|
|
57
58
|
::::
|
|
58
59
|
|
|
59
|
-
##
|
|
60
|
+
## At a glance
|
|
60
61
|
|
|
61
62
|
Once installed, plot `Quantity` objects directly with `matplotlib`:
|
|
62
63
|
|
|
@@ -69,7 +70,20 @@ import unxt as u
|
|
|
69
70
|
x = u.Q(jnp.linspace(0, 360, 100), "deg")
|
|
70
71
|
y = u.Q(jnp.sin(x.ustrip("rad")), "")
|
|
71
72
|
|
|
72
|
-
plt.
|
|
73
|
+
fig, ax = plt.subplots()
|
|
74
|
+
ax.plot(x, y)
|
|
73
75
|
```
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
## Pages
|
|
78
|
+
|
|
79
|
+
**Tutorial**
|
|
80
|
+
|
|
81
|
+
- [Plot data that knows its units](tutorial-first-plot) — start here: plot two quantities, watch the axis labels write themselves, and see a dataset in the wrong unit land in the right place.
|
|
82
|
+
|
|
83
|
+
**How-to**
|
|
84
|
+
|
|
85
|
+
- [How to plot quantities with matplotlib](guide) — plotting directly, keeping units through the computation with `quaxed`, and turning the converter off.
|
|
86
|
+
|
|
87
|
+
**Reference**
|
|
88
|
+
|
|
89
|
+
- [API](api) — the converter and its setup function.
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Plot data that knows its units
|
|
2
|
+
|
|
3
|
+
We are going to plot two `unxt` quantities and never once tell `matplotlib` what the axes are measured in. The units come from the data, the axis labels write themselves, and a second dataset in a different unit still lands in the right place on the same axes.
|
|
4
|
+
|
|
5
|
+
You need `unxt`, `unxts.interop.matplotlib` and `matplotlib` installed, and nothing else.
|
|
6
|
+
|
|
7
|
+
## Set up
|
|
8
|
+
|
|
9
|
+
```{code-block} python
|
|
10
|
+
>>> import matplotlib
|
|
11
|
+
>>> matplotlib.use("Agg") # draw to memory, so no window opens
|
|
12
|
+
|
|
13
|
+
>>> import matplotlib.pyplot as plt
|
|
14
|
+
>>> import quaxed.numpy as jnp
|
|
15
|
+
>>> import unxt as u
|
|
16
|
+
>>> import unxts.interop.matplotlib # registers the converter
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
That last import is the whole setup. It registers a converter with `matplotlib`, and from then on `matplotlib` knows what to do with a `Quantity`. (`unxt` imports it for you when the package is installed, so in your own code you will usually not write that line at all.)
|
|
21
|
+
|
|
22
|
+
`matplotlib.use("Agg")` just keeps this page from trying to open a window.
|
|
23
|
+
|
|
24
|
+
## Make some unitful data
|
|
25
|
+
|
|
26
|
+
Let's plot a journey: distances in kilometres against times in hours.
|
|
27
|
+
|
|
28
|
+
```{code-block} python
|
|
29
|
+
>>> t = u.Q(jnp.asarray([0.0, 1.0, 2.0]), "hr")
|
|
30
|
+
>>> d = u.Q(jnp.asarray([0.0, 90.0, 180.0]), "km")
|
|
31
|
+
|
|
32
|
+
>>> t
|
|
33
|
+
Quantity(Array([0., 1., 2.], dtype=float32), unit='h')
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Notice `matplotlib` has not been mentioned yet. These are ordinary quantities.
|
|
38
|
+
|
|
39
|
+
## Plot them
|
|
40
|
+
|
|
41
|
+
```{code-block} python
|
|
42
|
+
>>> fig, ax = plt.subplots()
|
|
43
|
+
>>> _ = ax.plot(t, d)
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Now look at the axes:
|
|
48
|
+
|
|
49
|
+
```{code-block} python
|
|
50
|
+
>>> print(ax.get_xlabel())
|
|
51
|
+
$\mathrm{h}$
|
|
52
|
+
|
|
53
|
+
>>> print(ax.get_ylabel())
|
|
54
|
+
$\mathrm{km}$
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
We never called `set_xlabel`. The converter read the unit off each quantity and labelled the axis with it, typeset as LaTeX so it renders properly in the figure.
|
|
59
|
+
|
|
60
|
+
The axis has also _remembered_ its unit:
|
|
61
|
+
|
|
62
|
+
```{code-block} python
|
|
63
|
+
>>> ax.xaxis.get_units()
|
|
64
|
+
Unit("h")
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Add data in a different unit
|
|
69
|
+
|
|
70
|
+
Now for the interesting bit. Let's add a second journey: same distances, but the times given in **minutes** instead of hours.
|
|
71
|
+
|
|
72
|
+
```{code-block} python
|
|
73
|
+
>>> t_minutes = u.Q(jnp.asarray([0.0, 60.0, 120.0]), "min")
|
|
74
|
+
>>> _ = ax.plot(t_minutes, d)
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The x-axis is in hours and we just handed it minutes. Check what happened to the label:
|
|
79
|
+
|
|
80
|
+
```{code-block} python
|
|
81
|
+
>>> print(ax.get_xlabel())
|
|
82
|
+
$\mathrm{h}$
|
|
83
|
+
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Still hours. And check where the data landed:
|
|
87
|
+
|
|
88
|
+
```{code-block} python
|
|
89
|
+
>>> [round(float(v), 1) for v in ax.get_xlim()]
|
|
90
|
+
[-0.1, 2.1]
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The axis still spans about 0 to 2, not 0 to 120. Our 120 minutes was **converted to 2 hours** and drawn on top of the first line, because the axis already had a unit and the converter reconciled the new data with it.
|
|
95
|
+
|
|
96
|
+
Had these been plain arrays, 120 would have been plotted at 120 and the figure would have been silently wrong.
|
|
97
|
+
|
|
98
|
+
## Let units flow through a computation
|
|
99
|
+
|
|
100
|
+
Because the data is unitful all the way, you can compute with it and plot the result. Here is speed, derived rather than typed in:
|
|
101
|
+
|
|
102
|
+
```{code-block} python
|
|
103
|
+
>>> speed = d / t_minutes.uconvert("hr")
|
|
104
|
+
>>> speed.unit
|
|
105
|
+
Unit("km / h")
|
|
106
|
+
|
|
107
|
+
>>> fig2, ax2 = plt.subplots()
|
|
108
|
+
>>> _ = ax2.plot(t, speed)
|
|
109
|
+
>>> print(ax2.get_ylabel())
|
|
110
|
+
$\mathrm{km\,h^{-1}}$
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The `km / h` label came out of the division. Nothing in the plotting code knows what a speed is.
|
|
115
|
+
|
|
116
|
+
Using [`quaxed`](https://github.com/GalacticDynamics/quaxed)'s `numpy` keeps units through maths functions too:
|
|
117
|
+
|
|
118
|
+
```{code-block} python
|
|
119
|
+
>>> angle = u.Q(jnp.linspace(0.0, 360.0, 100), "deg")
|
|
120
|
+
>>> wave = jnp.sin(angle)
|
|
121
|
+
>>> wave.unit
|
|
122
|
+
Unit(dimensionless)
|
|
123
|
+
|
|
124
|
+
>>> fig3, ax3 = plt.subplots()
|
|
125
|
+
>>> _ = ax3.plot(angle, wave)
|
|
126
|
+
>>> print(ax3.get_xlabel())
|
|
127
|
+
$\mathrm{{}^{\circ}}$
|
|
128
|
+
|
|
129
|
+
>>> plt.close("all")
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`jnp.sin` took degrees and returned a dimensionless result, and the x-axis picked up the degree symbol on its own.
|
|
134
|
+
|
|
135
|
+
## What we built
|
|
136
|
+
|
|
137
|
+
Three figures, no axis labels written by hand, and a dataset in the wrong unit placed correctly instead of silently misplotted. All of it came from one import.
|
|
138
|
+
|
|
139
|
+
## Where to go next
|
|
140
|
+
|
|
141
|
+
- [How to plot quantities with matplotlib](guide) — turning the converter off, and using it alongside `quaxed`.
|
|
142
|
+
- [API](api) — the converter and its setup function.
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
__all__ = ("UnxtConverter", "setup_matplotlib_support_for_unxt")
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
import warnings
|
|
10
11
|
from collections.abc import Iterable, Sized
|
|
11
12
|
from dataclasses import dataclass, field
|
|
12
13
|
from typing import Any
|
|
@@ -30,10 +31,32 @@ class UnxtConverter(matplotlib.units.ConversionInterface): # type: ignore[misc]
|
|
|
30
31
|
|
|
31
32
|
"""
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
unit_format: str = "latex_inline"
|
|
35
|
+
"""`astropy` unit format for the axis label (see ``Unit.to_string``)."""
|
|
36
|
+
|
|
37
|
+
axisinfo_kw: dict[str, Any] | None = field(default=None, init=True, repr=False)
|
|
38
|
+
"""Deprecated: use ``unit_format`` instead.
|
|
39
|
+
|
|
40
|
+
.. deprecated:: 2.0.0
|
|
41
|
+
Use ``unit_format`` directly instead of ``axisinfo_kw={"format": ...}``.
|
|
42
|
+
This parameter will be removed in a future release.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __post_init__(self) -> None:
|
|
46
|
+
"""Handle deprecated ``axisinfo_kw`` parameter."""
|
|
47
|
+
if self.axisinfo_kw is not None:
|
|
48
|
+
warnings.warn(
|
|
49
|
+
"The `axisinfo_kw` parameter is deprecated and will be removed "
|
|
50
|
+
"in a future release. Use `unit_format` directly instead. "
|
|
51
|
+
"For example, use `UnxtConverter(unit_format='latex')` instead of "
|
|
52
|
+
"`UnxtConverter(axisinfo_kw={'format': 'latex'})`.",
|
|
53
|
+
category=DeprecationWarning,
|
|
54
|
+
stacklevel=2,
|
|
55
|
+
)
|
|
56
|
+
# Extract format from the dict and set unit_format
|
|
57
|
+
if "format" in self.axisinfo_kw:
|
|
58
|
+
# Use object.__setattr__ since this is a frozen dataclass in spirit
|
|
59
|
+
object.__setattr__(self, "unit_format", self.axisinfo_kw["format"])
|
|
37
60
|
|
|
38
61
|
def convert(self, obj: Any, unit: Any, axis: Axes) -> Array | list[Array]:
|
|
39
62
|
"""Convert *obj* using *unit* for the specified *axis*."""
|
|
@@ -60,8 +83,7 @@ class UnxtConverter(matplotlib.units.ConversionInterface): # type: ignore[misc]
|
|
|
60
83
|
# matplotlib may query axisinfo before any unit has been set on the axis.
|
|
61
84
|
if unit is None:
|
|
62
85
|
return matplotlib.units.AxisInfo()
|
|
63
|
-
|
|
64
|
-
return matplotlib.units.AxisInfo(label=unit.to_string(fmt))
|
|
86
|
+
return matplotlib.units.AxisInfo(label=unit.to_string(self.unit_format))
|
|
65
87
|
|
|
66
88
|
@staticmethod
|
|
67
89
|
def default_units(x: Any, _: Axes) -> Any:
|
|
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
|
|
|
18
18
|
commit_id: str | None
|
|
19
19
|
__commit_id__: str | None
|
|
20
20
|
|
|
21
|
-
__version__ = version = '2.0
|
|
22
|
-
__version_tuple__ = version_tuple = (2,
|
|
21
|
+
__version__ = version = '2.1.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (2, 1, 0)
|
|
23
23
|
|
|
24
24
|
__commit_id__ = commit_id = None
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Smoke tests for the unxts.interop.matplotlib public API."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
import unxts.interop.matplotlib as uim
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_all_symbols_present():
|
|
9
|
+
for name in uim.__all__:
|
|
10
|
+
assert hasattr(uim, name), f"unxts.interop.matplotlib missing: {name}"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_version_exposed():
|
|
14
|
+
assert uim.__version__ == version("unxts.interop.matplotlib")
|
{unxts_interop_matplotlib-2.0.1/tests → unxts_interop_matplotlib-2.1.0/tests/unit}/test_converter.py
RENAMED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"""Tests for unxts.interop.matplotlib converter registration."""
|
|
2
2
|
|
|
3
|
-
import matplotlib
|
|
4
3
|
import matplotlib.pyplot as plt
|
|
5
4
|
import matplotlib.units
|
|
6
5
|
import numpy as np
|
|
6
|
+
import pytest
|
|
7
7
|
import unxts.interop.matplotlib as uimpl
|
|
8
8
|
|
|
9
9
|
import unxt as u
|
|
@@ -69,3 +69,62 @@ def test_setup_can_be_disabled_and_re_enabled():
|
|
|
69
69
|
finally:
|
|
70
70
|
uimpl.setup_matplotlib_support_for_unxt()
|
|
71
71
|
assert u.quantity.AbstractQuantity in matplotlib.units.registry
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_unit_format_default_latex_inline():
|
|
75
|
+
"""Default unit_format produces latex_inline format labels."""
|
|
76
|
+
converter = uimpl.UnxtConverter()
|
|
77
|
+
unit = u.unit("m/s")
|
|
78
|
+
info = converter.axisinfo(unit, None)
|
|
79
|
+
# latex_inline format should produce something like $\mathrm{m\,s^{-1}}$
|
|
80
|
+
assert info.label == unit.to_string("latex_inline")
|
|
81
|
+
assert "$" in info.label
|
|
82
|
+
assert "mathrm" in info.label
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_unit_format_custom_format():
|
|
86
|
+
"""Custom unit_format respects the specified format."""
|
|
87
|
+
converter = uimpl.UnxtConverter(unit_format="latex")
|
|
88
|
+
unit = u.unit("m/s")
|
|
89
|
+
info = converter.axisinfo(unit, None)
|
|
90
|
+
# latex format should produce something like $\mathrm{\frac{m}{s}}$
|
|
91
|
+
assert info.label == unit.to_string("latex")
|
|
92
|
+
assert "frac" in info.label
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_unit_format_console_format():
|
|
96
|
+
"""unit_format='console' produces plain text format."""
|
|
97
|
+
converter = uimpl.UnxtConverter(unit_format="console")
|
|
98
|
+
unit = u.unit("m/s")
|
|
99
|
+
info = converter.axisinfo(unit, None)
|
|
100
|
+
# console format should produce plain text like "m / s"
|
|
101
|
+
assert info.label == unit.to_string("console")
|
|
102
|
+
assert "$" not in info.label
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_axisinfo_kw_deprecated():
|
|
106
|
+
"""Using axisinfo_kw triggers a deprecation warning."""
|
|
107
|
+
with pytest.warns(DeprecationWarning, match=r"axisinfo_kw.*deprecated"):
|
|
108
|
+
converter = uimpl.UnxtConverter(axisinfo_kw={"format": "latex"})
|
|
109
|
+
# Verify the format was correctly applied
|
|
110
|
+
assert converter.unit_format == "latex"
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_axisinfo_kw_backwards_compatibility():
|
|
114
|
+
"""axisinfo_kw={'format': ...} still works and produces correct output."""
|
|
115
|
+
with pytest.warns(DeprecationWarning, match=r"axisinfo_kw.*deprecated"):
|
|
116
|
+
converter = uimpl.UnxtConverter(axisinfo_kw={"format": "latex"})
|
|
117
|
+
|
|
118
|
+
unit = u.unit("m/s")
|
|
119
|
+
info = converter.axisinfo(unit, None)
|
|
120
|
+
# Should use the format from axisinfo_kw
|
|
121
|
+
assert info.label == unit.to_string("latex")
|
|
122
|
+
assert "frac" in info.label
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def test_axisinfo_kw_empty_dict():
|
|
126
|
+
"""axisinfo_kw={} uses the default unit_format."""
|
|
127
|
+
with pytest.warns(DeprecationWarning, match=r"axisinfo_kw.*deprecated"):
|
|
128
|
+
converter = uimpl.UnxtConverter(axisinfo_kw={})
|
|
129
|
+
# Should still use default unit_format since no "format" key
|
|
130
|
+
assert converter.unit_format == "latex_inline"
|
|
@@ -1,31 +0,0 @@
|
|
|
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.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|