Skip to content

Commit 95c8f63

Browse files
authored
Create gallery example for datetime inputs
Based on GenericMappingTools#464 and GenericMappingTools#549 I prepared a gallery example for plotting datetime inputs.
1 parent 999a3f3 commit 95c8f63

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Datetime inputs
3+
---------------
4+
5+
The :meth:`pygmt.Figure.plot` method can plot datetime inputs of individual types:
6+
7+
- numpy.datetime64: for available options see https://numpy.org/doc/stable/reference/arrays.datetime.html
8+
- :class:`pandas.DatetimeIndex`
9+
- :class:`xarray.DataArray`: datetimes included in a *xarray.DataArray*
10+
- raw datetime strings in ISO format (e.g. ``["YYYY-MM-DD", "YYYY-MM-DDTHH", "YYYY-MM-DDTHH:MM:SS"]``)
11+
- Python built-in :class:`datetime.datetime` and :class:`datetime.date`
12+
13+
We can pass datetime inputs based on one of the types listed above directly to the ``x`` and ``y`` options.
14+
15+
The ``region`` argument has to include the :math:`x` and :math:`y` axis limits as *str* in the form
16+
``"date_min/data_max/ymin/ymax"``.
17+
18+
"""
19+
20+
import datetime
21+
import numpy as np
22+
import pandas as pd
23+
import xarray as xr
24+
import pygmt
25+
26+
fig = pygmt.Figure()
27+
28+
# create a basemap with limits of 2010-01-01 to 2020-06-01 on the x axis and
29+
# 0 to 10 on the y axis
30+
fig.basemap(projection="X15c/5c", region="2010-01-01/2020-06-01/0/10", frame=True)
31+
32+
# numpy.datetime64 types
33+
x = np.array(
34+
["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], dtype="datetime64")
35+
y = [1, 2, 3]
36+
fig.plot(x, y, style="c0.4c", pen="1p", color="red3")
37+
38+
# pandas.DatetimeIndex
39+
x = pd.date_range("2013", periods=3, freq="YS")
40+
y = [4, 5, 6]
41+
fig.plot(x, y, style="t0.4c", pen="1p", color="gold")
42+
43+
# xarray.DataArray
44+
x = xr.DataArray(data=pd.date_range(start="2015-03", periods=3, freq="QS"))
45+
y = [7.5, 6, 4.5]
46+
fig.plot(x, y, style="s0.4c", pen="1p")
47+
48+
# raw datetime strings
49+
x = ["2016-02-01", "2016-06-04T14", "2016-10-04T00:00:15"]
50+
y = [7, 8, 9]
51+
fig.plot(x, y, style="a0.4c", pen="1p", color="dodgerblue")
52+
53+
# the Python built-in datetime and date
54+
x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 6, 1, 20, 5, 45)]
55+
y = [6.5, 4.5]
56+
fig.plot(x, y, style="i0.4c", pen="1p", color="seagreen")
57+
58+
fig.show()

0 commit comments

Comments
 (0)