Plotting Quantities with Astropy

Using this example from the Astropy documentation:
https://docs.astropy.org/en/stable/visualization/matplotlib_integration.html#plotting-quantities
if I modify the quantity specification so that it is specified like this:
plt.plot([1* u.m, 2* u.m, 3* u.m] )
rather than this:
plt.plot([1, 2, 3] * u.m)
I get a unit conversion error. Any idea how I can get this to work? (My real data is like the former rather than the latter.)

Assuming that your quantities are all the same type of unit (e.g., length), you can instantiate a single Quantity object with your array, e.g.,:

plt.plot(u.Quantity([1* u.m, 2* u.m, 3* u.m]))

Hope that helps!

1 Like

Thanks, that worked!