Units and Quantities


  • Use units
  • Create functions that accept quantities as arguments
  • Create new units


How do we define a Quantity and which parts does it have?

In [1]:
from astropy import units as u
In [2]:
# Define a quantity length
# print it
In [3]:
# Type of quantity
In [4]:
# Type of unit
In [5]:
# Quantity
In [6]:
# value
In [7]:
# unit
In [8]:
# information

Quantities can be converted to other units systems or factors by using to()

In [9]:
# Convert it to: km, lyr

We can do arithmetic operations when the quantities have the compatible units:

In [10]:
# arithmetic with distances

Quantities can also be combined, for example to measure speed

In [11]:
# calculate a speed
In [12]:
# decompose it


  1. Convert the speed in imperial units (miles/hour) using:
    ```from astropy.units import imperial```
  2. Calculate whether a pint is more than half litre
    You can compare quantities as comparing variables.
    Something strange? Check what deffinition of pint astropy is using.
  3. Does units work with areas? calculate the area of a rectangle of 3 km of side and 5 meter of width. Show them in m^2 and convert them to yards^2
In [13]:
In [14]:
In [15]:

Composed units

Many units are compositions of others, for example, one could create new combinationes for ease of use:

In [16]:
# create a composite unit
In [17]:
# and in the imperial system

and others are already a composition:

In [18]:
# what can be converted from s-1?
In [19]:
# or Jules?
In [20]:
# Unity of R

Sometime we get no units quantitites

In [21]:
# no units

What happen if we add a number to this?

In [22]:
# arithmetic with no units
In [23]:
# final value of a no unit quantity


Some conversions are not done by a conversion factor as between miles and kilometers, for example converting between wavelength and frequency.

In [24]:
# converting spectral quantities
In [25]:
# but doing it right

Other built-in equivalencies are:

  • parallax()
  • Doppler (dopplr_radio, doppler_optical, doppler_relativistic)
  • spectral flux density
  • brigthness temperature
  • temperature energy
  • and you can build your own
In [26]:
# finding the equivalencies
In [27]:
# but also using other systems

Printing the quantities

In [28]:
# Printing values with different formats


Quantities can also be applied to arrays

In [29]:
# different ways of defining a quantity for a single value
In [30]:
# now with lists
# and arrays
# and its arithmetics
In [31]:
# angles are smart!

Plotting quantities

To work nicely with matplotlib we need to do as follows:

In [32]:
# allowing for plotting
from astropy.visualization import quantity_support

# loading matplotlib
%matplotlib inline
from matplotlib import pyplot as plt
In [33]:
# Ploting the previous array

Creating functions with quantities as units

We want to have functions that contain the information of the untis, and with them we can be sure that we will be always have the right result.

In [34]:
# Create a function for the Kinetic energy
In [35]:
# run with and without units


  1. Create a function that calculates potential energy where *g* defaults to Earth value, but could be used for different planets. Test it for any of the *g* values for any other planet.
In [36]:
In [37]:
# run it for some values
In [38]:
# on Mars:

Create your own units

Some times we want to create our own units:

In [39]:
# Create units for a laugh scale


  1. Convert the area calculated before `rectangle_area` in Hectare (1 hectare = 100 ares; 1 are = 100 m2).
In [40]: