matplotlib Interoperability Guide#
This guide shows how to plot unxt.Quantity objects with matplotlib.
Setup#
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:
import matplotlib.pyplot as plt
import unxt as u
Plotting quantities#
Pass Quantity objects straight to matplotlib โ the registered converter strips the units to their magnitudes for the axes:
import jax.numpy as jnp
x = u.Q(jnp.linspace(0, 360, 100), "deg")
y = u.Q(jnp.sin(x.ustrip("rad")), "")
plt.plot(x, y)
Using quaxed.numpy#
With quaxedโs unit-aware numpy namespace, the intermediate values stay Quantity objects and units propagate through the computation:
import quaxed.numpy as jnp
x = u.Q(jnp.linspace(0, 360, 100), "deg")
y = jnp.sin(x)
plt.plot(x, y)
Disabling the converter#
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:
from unxts.interop.matplotlib import setup_matplotlib_support_for_unxt
setup_matplotlib_support_for_unxt(enable=False) # stop converting Quantity
setup_matplotlib_support_for_unxt(enable=True) # re-enable
See Also#
API reference โ the converter and its setup function