unxts.interop.matplotlib 2.0.0__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.
@@ -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,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,9 @@
1
+ unxts/interop/matplotlib/__init__.py,sha256=HYpqkgkjvsfhnyyBMQjfdebXcVduCz6YLLONMHii6YI,474
2
+ unxts/interop/matplotlib/_version.py,sha256=M8-womMCm6RC2xFuYRGDGvgpBN64psyQZImZ5vOrGmc,520
3
+ unxts/interop/matplotlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ unxts/interop/matplotlib/_src/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ unxts/interop/matplotlib/_src/converter.py,sha256=kM3n8a9ve-EqvNPqLaA05gHPuXW5tYOtTOFvzcllbsU,3253
6
+ unxts_interop_matplotlib-2.0.0.dist-info/METADATA,sha256=RZE40AK29D807fid_5poDSjoaipfIu5BfDBOH4CAb5c,1607
7
+ unxts_interop_matplotlib-2.0.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
8
+ unxts_interop_matplotlib-2.0.0.dist-info/licenses/LICENSE,sha256=dZw7glr5UgpWO9a1jlC004HhtLTytJbcQO9pQVMLk7g,1519
9
+ unxts_interop_matplotlib-2.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.