How to plot quantities with matplotlib#
This guide shows you 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")), "")
fig, ax = plt.subplots()
ax.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)
fig, ax = plt.subplots()
ax.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#
Plot data that knows its units โ the lesson, if you have not plotted a quantity before.
API โ the converter and its setup function.