Skip to main content
`Ctrl`+`K`

# Plotting Astropy objects in Matplotlib

## Plotting quantities

|Quantity| objects can be conveniently plotted using matplotlib. This feature needs to be explicitly turned on:

    >>> from astropy.visualization import quantity_support
    >>> quantity_support()
    <astropy.visualization.units.MplQuantityConverter ...>

Then |Quantity| objects can be passed to matplotlib plotting functions. The axis labels are automatically labeled with the unit of the quantity:

    from astropy import units as u
    from astropy.visualization import quantity_support
    from matplotlib import pyplot as plt
    quantity_support()
    fig, ax = plt.subplots(figsize=(5, 3))
    ax.plot([1, 2, 3] * u.m)

(`png`, `svg`, `pdf`)

Quantities are automatically converted to the first unit set on a particular axis, so in the following, the y-axis remains in `m` even though the second line is given in `cm`:

    ax.plot([1, 2, 3] * u.cm)

(`png`, `svg`, `pdf`)

If you want more control, you can explicitly set the units for the x-axis and/or y-axis by passing a unit argument to `ax.xaxis.set_units()` or `ax.yaxis.set_units()`.

For example, you can set the y-axis units to centimeters:

    ax.yaxis.set_units(u.cm)

(`png`, `svg`, `pdf`)

This keeps the axis units fixed, regardless of the data’s units or plot order. For more information, see the Matplotlib documentation for `matplotlib.axis.Axis.set_units()`.

Plotting a quantity with an incompatible unit will raise an exception. For example, calling `ax.plot([1, 2, 3] * u.kg)` (mass unit) to overplot on the plot above that is displaying length units.

To make sure unit support is turned off afterward, you can use `quantity_support` with a `with` statement:

    with quantity_support():
        ax.plot([1, 2, 3] * u.m)

## Plotting times

Matplotlib natively provides a mechanism for plotting dates and times on one or both of the axes, as described in Date tick labels. To make use of this, you can use the `plot_date` attribute of |Time| to get values in the time system used by Matplotlib.

However, in many cases, you will probably want to have more control over the precise scale and format to use for the tick labels, in which case you can make use of the `time_support` function. This feature needs to be explicitly turned on:

    >>> from astropy.visualization import time_support
    >>> time_support()
    <astropy.visualization.units.MplTimeConverter ...>

Once this is enabled, |Time| objects can be passed to matplotlib plotting functions. The axis labels are then automatically labeled with times formatted using the |Time| class:

    from matplotlib import pyplot as plt
    from astropy.time import Time
    from astropy.visualization import time_support

    time_support()

    fig, ax = plt.subplots(figsize=(5, 3))
    ax.plot(Time([58000, 59000, 62000], format='mjd'), [1.2, 3.3, 2.3])

(`png`, `svg`, `pdf`)

By default, the format and scale used for the plots is taken from the first time that Matplotlib encounters for a particular Axes instance. The format and scale can also be explicitly controlled by passing arguments to `time_support`:

    time_support(format='mjd', scale='tai')
    fig, ax = plt.subplots(figsize=(5, 3))
    ax.plot(Time([50000, 52000, 54000], format='mjd'), [1.2, 3.3, 2.3])

(`png`, `svg`, `pdf`)

To make sure support for plotting times is turned off afterward, you can use `time_support` as a context manager:

    with time_support(format='mjd', scale='tai'):
        fig, ax = plt.subplots(figsize=(5, 3))
        ax.plot(Time([50000, 52000, 54000], format='mjd'))