|
3 | 3 | ========================
|
4 | 4 |
|
5 | 5 | PyGMT accepts a variety of datetime objects to plot data and create charts.
|
6 |
| -Aside from the built-in Python ``datetime`` object, PyGMT supports input using |
7 |
| -ISO formatted strings, ``pandas``, ``xarray``, as well as ``numpy``. |
8 |
| -These data types can be used to plot specific points as well as get |
9 |
| -passed into the ``region`` parameter to create a range of the data on an axis. |
10 |
| -
|
11 |
| -The following examples will demonstrate how to create plots |
12 |
| -using the different datetime objects. |
| 6 | +Aside from the built-in Python ``datetime`` module, PyGMT supports inputs |
| 7 | +containing ISO formatted strings as well as objects generated with |
| 8 | +``numpy``, ``pandas``, and ``xarray``. These data types can be used to plot |
| 9 | +specific points as well as get passed into the ``region`` parameter to |
| 10 | +create a range of the data on an axis. |
| 11 | +
|
| 12 | +The following examples will demonstrate how to create plots using these |
| 13 | +different datetime objects. |
13 | 14 | """
|
14 | 15 |
|
15 | 16 | # %%
|
|
24 | 25 | # Using Python's ``datetime``
|
25 | 26 | # ---------------------------
|
26 | 27 | #
|
27 |
| -# In this example, Python's built-in ``datetime`` module is used |
28 |
| -# to create data points stored in list ``x``. Additionally, |
29 |
| -# dates are passed into the ``region`` parameter in the format |
30 |
| -# ``(x_start, x_end, y_start, y_end)``, |
31 |
| -# where the date range is plotted on the x-axis. |
32 |
| -# An additional notable parameter is ``style``, where it's specified |
33 |
| -# that data points are to be plotted in an **X** shape with a size |
34 |
| -# of 0.3 centimeters. |
35 |
| -# |
| 28 | +# In this example, Python's built-in ``datetime`` module is used to create |
| 29 | +# data points stored in the list ``x``. Additionally, dates are passed into |
| 30 | +# the ``region`` parameter in the format ``[x_start, x_end, y_start, y_end]``, |
| 31 | +# where the date range is plotted on the x-axis. An additional notable |
| 32 | +# parameter is ``style``, where it's specified that data points are to be |
| 33 | +# plotted in an **X** shape with a size of 0.3 centimeters. |
36 | 34 |
|
37 | 35 | x = [
|
38 | 36 | datetime.date(2010, 6, 1),
|
|
55 | 53 | fig.show()
|
56 | 54 |
|
57 | 55 | # %%
|
58 |
| -# In addition to specifying the date, ``datetime`` supports the exact time at |
| 56 | +# In addition to specifying the date, ``datetime`` supports the time at |
59 | 57 | # which the data points were recorded. Using :meth:`datetime.datetime` the
|
60 | 58 | # ``region`` parameter as well as data points can be created with both date and
|
61 | 59 | # time information.
|
62 | 60 | #
|
63 |
| -# Some notable differences to the previous example include |
| 61 | +# Some notable differences to the previous example include: |
64 | 62 | #
|
65 | 63 | # - Modifying ``frame`` to only include West (left) and South (bottom) borders,
|
66 | 64 | # and removing grid lines
|
67 |
| -# - Using circles to plot data points defined through ``c`` in ``style`` |
68 |
| -# parameter |
| 65 | +# - Using circles to plot data points defined by ``c`` in the argument passed |
| 66 | +# through the ``style`` parameter |
69 | 67 |
|
70 | 68 | x = [
|
71 | 69 | datetime.datetime(2021, 1, 1, 3, 45, 1),
|
|
98 | 96 | # Using ISO Format
|
99 | 97 | # ----------------
|
100 | 98 | #
|
101 |
| -# In addition to Python's ``datetime`` library, PyGMT also supports passing |
102 |
| -# times in ISO format. Basic ISO strings are formatted as ``YYYY-MM-DD`` with |
103 |
| -# each ``-`` delineated section marking the four digit year value, two digit |
104 |
| -# month value, and two digit day value respectively. |
| 99 | +# In addition to Python's ``datetime`` module, PyGMT also supports passing |
| 100 | +# dates in ISO format. Basic ISO strings are formatted as ``YYYY-MM-DD`` with |
| 101 | +# each ``-`` delineated section marking the four-digit year value, two-digit |
| 102 | +# month value, and two-digit day value, respectively. |
105 | 103 | #
|
106 |
| -# When including time of day into ISO strings, the ``T`` character is used, as |
| 104 | +# For including the time into an ISO string, the ``T`` character is used, as it |
107 | 105 | # can be seen in the following example. This character is immediately followed
|
108 | 106 | # by a string formatted as ``hh:mm:ss`` where each ``:`` delineated section
|
109 |
| -# marking the two digit hour value, two digit minute value, and two digit |
110 |
| -# second value respectively. The figure in the following example is plotted |
| 107 | +# marking the two-digit hour value, two-digit minute value, and two-digit |
| 108 | +# second value, respectively. The figure in the following example is plotted |
111 | 109 | # over a horizontal range of one year from 2016-01-01 to 2017-01-01.
|
112 | 110 |
|
113 | 111 | x = ["2016-02-01", "2016-06-04T14", "2016-10-04T00:00:15", "2016-12-01T05:00:15"]
|
|
126 | 124 | fig.show()
|
127 | 125 |
|
128 | 126 | # %%
|
129 |
| -# PyGMT doesn't recognize non-ISO datetime strings like "Jun 05, 2018". If your |
130 |
| -# data contain non-ISO datetime strings, you can convert them to a recognized |
131 |
| -# format using :func:`pandas.to_datetime` and then pass it to PyGMT. |
| 127 | +# .. note:: |
132 | 128 | #
|
| 129 | +# PyGMT doesn't recognize non-ISO datetime strings like "Jun 05, 2018". If |
| 130 | +# your data contain non-ISO datetime strings, you can convert them to a |
| 131 | +# recognized format using :func:`pandas.to_datetime` and then pass it to |
| 132 | +# PyGMT. |
133 | 133 |
|
134 | 134 |
|
135 | 135 | # %%
|
|
162 | 162 | # -------------------------------
|
163 | 163 | #
|
164 | 164 | # In the following example, :func:`pandas.date_range` produces a list of
|
165 |
| -# :class:`pandas.DatetimeIndex` objects, which gets is used to pass date |
166 |
| -# data to the PyGMT figure. |
| 165 | +# :class:`pandas.DatetimeIndex` objects, which is used to pass date data to |
| 166 | +# the PyGMT figure. |
167 | 167 | # Specifically ``x`` contains 7 different :class:`pandas.DatetimeIndex`
|
168 | 168 | # objects, with the number being manipulated by the ``periods`` parameter. Each
|
169 | 169 | # period begins at the start of a business quarter as denoted by BQS when
|
170 |
| -# passed to the ``periods`` parameter. The initial date is the first argument |
| 170 | +# passed to the ``freq`` parameter. The initial date is the first argument |
171 | 171 | # that is passed to :func:`pandas.date_range` and it marks the first data point
|
172 | 172 | # in the list ``x`` that will be plotted.
|
173 | 173 |
|
|
192 | 192 | # Using :class:`xarray.DataArray`
|
193 | 193 | # -------------------------------
|
194 | 194 | #
|
195 |
| -# In this example, instead of using a :func:`pandas.date_range`, ``x`` is |
196 |
| -# initialized as a list of :class:`xarray.DataArray` objects. This object |
197 |
| -# provides a wrapper around regular PyData formats. It also allows the data to |
198 |
| -# have labeled dimensions while supporting operations that use various pieces |
199 |
| -# of metadata.The following code uses :func:`pandas.date_range` object to |
200 |
| -# fill the DataArray with data, but this is not essential for the creation of |
201 |
| -# a valid DataArray. |
| 195 | +# In this example, instead of using a list of :class:`pandas.DatetimeIndex` |
| 196 | +# objects, ``x`` is initialized as an :class:`xarray.DataArray` object. This |
| 197 | +# object provides a wrapper around regular PyData formats. It also allows the |
| 198 | +# data to have labeled dimensions while supporting operations that use various |
| 199 | +# pieces of metadata. The following code uses :func:`pandas.date_range` to fill |
| 200 | +# the DataArray with data, but this is not essential for the creation of a |
| 201 | +# valid DataArray. |
202 | 202 |
|
203 | 203 | x = xr.DataArray(data=pd.date_range(start="2020-01-01", periods=4, freq="Q"))
|
204 | 204 | y = [4, 7, 5, 6]
|
|
221 | 221 | # Using :class:`numpy.datetime64`
|
222 | 222 | # -------------------------------
|
223 | 223 | #
|
224 |
| -# In this example, instead of using a :func:`pd.date_range`, ``x`` is |
| 224 | +# In this example, instead of using :func:`pd.date_range`, ``x`` is |
225 | 225 | # initialized as an ``np.array`` object. Similar to :class:`xarray.DataArray`
|
226 |
| -# this wraps the dataset before passing it as a parameter. However, |
| 226 | +# this wraps the dataset before passing it as an argument. However, |
227 | 227 | # ``np.array`` objects use less memory and allow developers to specify
|
228 |
| -# datatypes. |
| 228 | +# data types. |
229 | 229 |
|
230 | 230 | x = np.array(["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype="datetime64")
|
231 | 231 | y = [2, 7, 5]
|
|
248 | 248 | # Generating an automatic region
|
249 | 249 | # ------------------------------
|
250 | 250 | #
|
251 |
| -# Another way of creating charts involving datetime data can be done |
252 |
| -# by automatically generating the region of the plot. This can be done |
253 |
| -# by passing the dataframe to :func:`pygmt.info`, which will find |
254 |
| -# maximum and minimum values for each column and create a list |
255 |
| -# that could be passed as region. Additionally, the ``spacing`` argument |
256 |
| -# can be passed to increase the range past the maximum and minimum |
257 |
| -# data points. |
| 251 | +# Another way of creating charts involving datetime data can be done by |
| 252 | +# automatically generating the region of the plot. This can be done by |
| 253 | +# passing the DataFrame to :func:`pygmt.info`, which will find the maximum and |
| 254 | +# minimum values for each column and create a list that could be passed as |
| 255 | +# region. Additionally, the ``spacing`` parameter can be used to increase the |
| 256 | +# range past the maximum and minimum data points. |
258 | 257 |
|
259 | 258 | data = [
|
260 | 259 | ["20200712", 1000],
|
|
291 | 290 | # Setting Primary and Secondary Time Axes
|
292 | 291 | # ---------------------------------------
|
293 | 292 | #
|
294 |
| -# This example focuses on labeling the axes and setting intervals |
295 |
| -# at which the labels are expected to appear. All of these modifications |
296 |
| -# are added to the ``frame`` parameter and each item in that list modifies |
297 |
| -# a specific section of the plot. |
| 293 | +# This example focuses on annotating the axes and setting the interval in which |
| 294 | +# the annotations should appear. All of these modifications are passed |
| 295 | +# to the ``frame`` parameter and each item in that list modifies a specific |
| 296 | +# aspect of the frame. |
298 | 297 | #
|
299 |
| -# Starting off with ``WS``, adding this string means that only |
300 |
| -# Western/Left (**W**) and Southern/Bottom (**S**) borders of |
301 |
| -# the plot will be shown. For more information on this, please |
302 |
| -# refer to :doc:`frame instructions </tutorials/basics/frames>`. |
| 298 | +# Adding ``"WS"`` means that only the Western/Left (**W**) and Southern/Bottom |
| 299 | +# (**S**) borders of the plot are annotated. For more information on this, |
| 300 | +# please refer to the :doc:`Frames, ticks, titles, and labels tutorial |
| 301 | +# </tutorials/basics/frames>`. |
303 | 302 | #
|
304 |
| -# The other important item in the ``frame`` list is |
305 |
| -# ``"sxa1Of1D"``. This string modifies the secondary |
306 |
| -# labeling (**s**) of the x-axis (**x**). Specifically, |
307 |
| -# it sets the main annotation and major tick spacing interval |
308 |
| -# to one month (**a1O**) (capital letter o, not zero). Additionally, |
309 |
| -# it sets the minor tick spacing interval to 1 day (**f1D**). |
310 |
| -# The labeling of this axis can be modified by setting |
311 |
| -# :gmt-term:`FORMAT_DATE_MAP` to 'o' to use the month's |
312 |
| -# name instead of its number. More information about configuring |
313 |
| -# date formats can be found on the |
314 |
| -# :gmt-term:`official GMT documentation page <FORMAT_DATE_MAP>`. |
| 303 | +# Another important item in the list passed to ``frame`` is ``"sxa1Of1D"``. |
| 304 | +# This string modifies the secondary annotation (**s**) of the x-axis (**x**). |
| 305 | +# Specifically, it sets the main annotation and major tick spacing interval |
| 306 | +# to one month (**a1O**) (capital letter O, not zero). Additionally, it sets |
| 307 | +# the minor tick spacing interval to 1 day (**f1D**). To use the month's name |
| 308 | +# instead of its number set :gmt-term:`FORMAT_DATE_MAP` to **o**. More |
| 309 | +# information on configuring date formats can be found at |
| 310 | +# :gmt-term:`FORMAT_DATE_MAP`, :gmt-term:`FORMAT_DATE_IN`, and |
| 311 | +# :gmt-term:`FORMAT_DATE_OUT`. |
315 | 312 |
|
316 | 313 | x = pd.date_range("2013-05-02", periods=10, freq="2D")
|
317 | 314 | y = [4, 5, 6, 8, 9, 5, 8, 9, 4, 2]
|
|
332 | 329 | fig.show()
|
333 | 330 |
|
334 | 331 | # %%
|
335 |
| -# The same concept shown above can be applied to smaller |
336 |
| -# as well as larger intervals. In this example, |
337 |
| -# data are plotted for different times throughout two days. |
338 |
| -# Primary x-axis labels are modified to repeat every 6 hours |
339 |
| -# and secondary x-axis label repeats every day and shows |
340 |
| -# the day of the week. |
| 332 | +# The same concept shown above can be applied to smaller as well as larger |
| 333 | +# intervals. In this example, data are plotted for different times throughout |
| 334 | +# two days. The primary x-axis annotations are modified to repeat every 6 |
| 335 | +# hours, and the secondary x-axis annotations repeat every day and show the |
| 336 | +# day of the week. |
341 | 337 | #
|
342 |
| -# Another notable mention in this example is |
343 |
| -# setting :gmt-term:`FORMAT_CLOCK_MAP` to "-hhAM" |
344 |
| -# which specifies the format used for time. |
345 |
| -# In this case, leading zeros are removed |
346 |
| -# using (**-**), and only hours are displayed. |
347 |
| -# Additionally, an AM/PM system is being used |
348 |
| -# instead of a 24-hour system. More information about configuring |
349 |
| -# time formats can be found on the |
350 |
| -# :gmt-term:`official GMT documentation page <FORMAT_CLOCK_MAP>`. |
| 338 | +# Another notable mention in this example is setting |
| 339 | +# :gmt-term:`FORMAT_CLOCK_MAP` to **-hhAM** which specifies the format used |
| 340 | +# for time. In this case, leading zeros are removed using (**-**), and only |
| 341 | +# hours are displayed. Additionally, an AM/PM system is used instead of a |
| 342 | +# 24-hour system. More information on configuring time formats can be found |
| 343 | +# at :gmt-term:`FORMAT_CLOCK_MAP`, :gmt-term:`FORMAT_CLOCK_IN`, and |
| 344 | +# :gmt-term:`FORMAT_CLOCK_OUT`. |
351 | 345 |
|
352 | 346 | x = pd.date_range("2021-04-15", periods=8, freq="6H")
|
353 | 347 | y = [2, 5, 3, 1, 5, 7, 9, 6]
|
|
0 commit comments