Plotting
plotting_context()
¶
Context for plotting with Quantity objects Based on : https://docs.astropy.org/en/stable/_modules/astropy/visualization/units.html#quantity_support
Source code in physipy/quantity/_plot.py
def plotting_context():
"""Context for plotting with Quantity objects
Based on :
https://docs.astropy.org/en/stable/_modules/astropy/visualization/units.html#quantity_support
"""
from matplotlib import ticker, units
# Get all subclass for Quantity, since matplotlib checks on class,
# not subclass.
def all_issubclass(cls):
return {cls}.union(
[s for c in cls.__subclasses__() for s in all_issubclass(c)]
)
class MplQuantityConverter(QuantityConverter):
_all_issubclass_quantity = all_issubclass(Quantity)
def __init__(self):
# Keep track of original converter in case the context manager is
# used in a nested way.
self._original_converter = {}
for cls in self._all_issubclass_quantity:
self._original_converter[cls] = munits_registry.get(cls)
munits_registry[cls] = self
def __enter__(self):
return self
def __exit__(self, type, value, tb):
for cls in self._all_issubclass_quantity:
if self._original_converter[cls] is None:
del munits_registry[cls]
else:
munits_registry[cls] = self._original_converter[cls]
return MplQuantityConverter()
setup_matplotlib(enable=True)
¶
Enable unit system in Matplotlib for Quantity objects.
Parameters¶
enable : bool Weither to activate the handling of physipy in matplotlib.
Returns¶
None
Source code in physipy/quantity/_plot.py
def setup_matplotlib(enable: bool = True) -> None:
"""Enable unit system in Matplotlib for Quantity objects.
Parameters
----------
enable : bool
Weither to activate the handling of physipy in matplotlib.
Returns
-------
None
"""
if matplotlib.__version__ < "2.0":
raise RuntimeError("Matplotlib >= 2.0 required to work with units.")
if not enable:
munits_registry.pop(Quantity, None)
else:
munits_registry[Quantity] = QuantityConverter()