File: matplotlib_integration.rst

package info (click to toggle)
astropy 7.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 33,816 kB
  • sloc: python: 237,392; ansic: 55,195; lex: 8,621; sh: 3,317; xml: 2,399; makefile: 191
file content (132 lines) | stat: -rw-r--r-- 4,272 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Plotting Astropy objects in Matplotlib
**************************************

.. _plotting-quantities:

Plotting quantities
===================

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

.. doctest-requires:: matplotlib

    >>> from astropy.visualization import quantity_support
    >>> quantity_support()  # doctest: +IGNORE_OUTPUT
    <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:

.. plot::
   :include-source:
   :context: reset

    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)

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``:

.. plot::
   :include-source:
   :context:

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

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:

.. plot::
    :include-source:
    :context:

    ax.yaxis.set_units(u.cm)

This keeps the axis units fixed, regardless of the data's units or plot order.
For more information, see the Matplotlib documentation for :meth:`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
`~astropy.visualization.quantity_support` with a ``with`` statement::

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

.. _plotting-times:

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 <https://matplotlib.org/stable/gallery/text_labels_and_annotations/date.html>`_.
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 `~astropy.visualization.time_support` function. This feature needs to
be explicitly turned on:

.. doctest-requires:: matplotlib

    >>> from astropy.visualization import time_support
    >>> time_support()  # doctest: +IGNORE_OUTPUT
    <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:

.. plot::
   :include-source:
   :context: reset

    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])

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``:

.. plot::
   :nofigs:
   :context: reset

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

.. plot::
   :include-source:
   :context:

    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])

To make sure support for plotting times is turned off afterward, you can use
`~astropy.visualization.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'))