Skip to content

Commit d8a9b0a

Browse files
Tutorial "Plotting datetime charts": Improve docs and fix typos (#2669)
Co-authored-by: Michael Grund <[email protected]>
1 parent d1e8f2f commit d8a9b0a

File tree

1 file changed

+78
-84
lines changed

1 file changed

+78
-84
lines changed

examples/tutorials/advanced/date_time_charts.py

Lines changed: 78 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
========================
44
55
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.
1314
"""
1415

1516
# %%
@@ -24,15 +25,12 @@
2425
# Using Python's ``datetime``
2526
# ---------------------------
2627
#
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.
3634

3735
x = [
3836
datetime.date(2010, 6, 1),
@@ -55,17 +53,17 @@
5553
fig.show()
5654

5755
# %%
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
5957
# which the data points were recorded. Using :meth:`datetime.datetime` the
6058
# ``region`` parameter as well as data points can be created with both date and
6159
# time information.
6260
#
63-
# Some notable differences to the previous example include
61+
# Some notable differences to the previous example include:
6462
#
6563
# - Modifying ``frame`` to only include West (left) and South (bottom) borders,
6664
# 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
6967

7068
x = [
7169
datetime.datetime(2021, 1, 1, 3, 45, 1),
@@ -98,16 +96,16 @@
9896
# Using ISO Format
9997
# ----------------
10098
#
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.
105103
#
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
107105
# can be seen in the following example. This character is immediately followed
108106
# 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
111109
# over a horizontal range of one year from 2016-01-01 to 2017-01-01.
112110

113111
x = ["2016-02-01", "2016-06-04T14", "2016-10-04T00:00:15", "2016-12-01T05:00:15"]
@@ -126,10 +124,12 @@
126124
fig.show()
127125

128126
# %%
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::
132128
#
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.
133133

134134

135135
# %%
@@ -162,12 +162,12 @@
162162
# -------------------------------
163163
#
164164
# 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.
167167
# Specifically ``x`` contains 7 different :class:`pandas.DatetimeIndex`
168168
# objects, with the number being manipulated by the ``periods`` parameter. Each
169169
# 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
171171
# that is passed to :func:`pandas.date_range` and it marks the first data point
172172
# in the list ``x`` that will be plotted.
173173

@@ -192,13 +192,13 @@
192192
# Using :class:`xarray.DataArray`
193193
# -------------------------------
194194
#
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.
202202

203203
x = xr.DataArray(data=pd.date_range(start="2020-01-01", periods=4, freq="Q"))
204204
y = [4, 7, 5, 6]
@@ -221,11 +221,11 @@
221221
# Using :class:`numpy.datetime64`
222222
# -------------------------------
223223
#
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
225225
# 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,
227227
# ``np.array`` objects use less memory and allow developers to specify
228-
# datatypes.
228+
# data types.
229229

230230
x = np.array(["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype="datetime64")
231231
y = [2, 7, 5]
@@ -248,13 +248,12 @@
248248
# Generating an automatic region
249249
# ------------------------------
250250
#
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.
258257

259258
data = [
260259
["20200712", 1000],
@@ -291,27 +290,25 @@
291290
# Setting Primary and Secondary Time Axes
292291
# ---------------------------------------
293292
#
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.
298297
#
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>`.
303302
#
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`.
315312

316313
x = pd.date_range("2013-05-02", periods=10, freq="2D")
317314
y = [4, 5, 6, 8, 9, 5, 8, 9, 4, 2]
@@ -332,22 +329,19 @@
332329
fig.show()
333330

334331
# %%
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.
341337
#
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`.
351345

352346
x = pd.date_range("2021-04-15", periods=8, freq="6H")
353347
y = [2, 5, 3, 1, 5, 7, 9, 6]

0 commit comments

Comments
 (0)