Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary secondary axis examples #2

Merged
merged 7 commits into from
Apr 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 99 additions & 18 deletions examples/tutorials/date_time_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
fig.show()

########################################################################################
# Using ``xarray.DataArray``
# Using :meth:`xarray.DataArray`
# -------------------------------------

# xarray.DataArray
Expand All @@ -178,34 +178,115 @@
# Code

########################################################################################
# Python built-in: `datetime.datetime`
# Generating Region Using :meth:`pygmt.info`
# ----------------------
#
# Explanation of supported parameters + bug at #597.

# the Python built-in datetime and date
x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 6, 1, 20, 5, 45)]
y = [6.5, 4.5]
fig.plot(x, y, style="i0.4c", pen="1p", color="seagreen")
data = [['20200712',1000],
['20200714',1235],
['20200716',1336],
['20200719',1176],
['20200721',1573],
['20200724',1893],
['20200729',1634]]

fig.show()
df = pd.DataFrame(
data,columns = ['Date','Score'])

# Code
df['Date'] = pd.to_datetime(
df['Date'],
format='%Y%m%d')

########################################################################################
# Python built-in: `datetime.date`
fig = pygmt.Figure()
region = pygmt.info(
table=df[["Date", "Score"]],
per_column=True,
spacing=(5000, 1200),
)

# Code
fig.plot(
region=region,
projection="X15c/10c",
frame=['WSen', "afg"],
x=df.Date,
y=df.Score,
style="c0.4c",
pen="1p",
color="green3",
)
fig.show()

########################################################################################
# Passing Min/Max Time into `region` parameter using `pygmt.info`
# Setting Primary and Secondary Time Axes
# ----------------------
#
# Explanation of supported parameters + bug at #597.
# This example focuses on labeling the axes and setting intervals
# at which the labels are expected to appear. All of these modification
# are added to the ``frame`` argument and each item in that list modifies
# a specific section of the plot.
#
# Starting off with ``WS``, adding this string means that only
# Western/Left **(W)** and Southern/Bottom **(S)** borders of
# the plot will be shown. For more information on this, please
# refer to :docs:`pygmt.Frames`.
#
# The other important item in the ``frame`` list is
# ``sxa1Of1D``. This string modifies the secondary
# labeling **(s)** of the x-axis **(x)**. Specifically,
# it sets the main annotation and major tick spacing interval
# to one month **(O)** (capital letter o, not zero). Additionally,
# it sets the minor tick spacing interval to 1 day **(D)**.
# The labeling of this axis is also modified using
# ``pygmt.config(FORMAT_DATE_MAP="o")`` to use the month's
# name instead of its number.

x = pd.date_range("2013-05-02", periods=10, freq="2D")
y = [4, 5, 6, 8, 9, 5, 8, 9, 4, 2]

# Code
fig = pygmt.Figure()
with pygmt.config(FORMAT_DATE_MAP="o"):
fig.plot(projection="X15c/10c",
region=[datetime.datetime(2013, 5, 1), datetime.datetime(2013, 5, 25), 0, 10],
frame=["WS", "sxa1Of1D", "pxa5d", "pya1+ucm", "sy+lLength"],
x=x,
y=y,
style="c0.4c",
pen="1p",
color="green3",
)

fig.show()

########################################################################################
# Setting Primary and Secondary Time Axes
# ----------------------
# The same concept shown above can be applied to smaller
# as well as larger intervals. In this example,
# data is plotted for different times throughout two days.
# Primary x-axis labels are modified to repeat every 6 hours
# and secondary x-axis label repeats every day and shows
# the day of the week.
#
# Explanation.
# Other notable mentions in this example is
# ``pygmt.config(FORMAT_CLOCK_MAP="-hhAM")``
# which specifies the used format for time.
# In this case, leading zeros are removed
# using **(-)**, and only hours are displayed.
# Additionally, an AM/PM system is being used
# instead of a 24-hour system.

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

fig = pygmt.Figure()
with pygmt.config(FORMAT_CLOCK_MAP="-hhAM"):
fig.plot(projection="X15c/10c",
region=[datetime.datetime(2021, 4, 14, 23, 0, 0), datetime.datetime(2021, 4, 17), 0, 10],
frame=["WS", "sxa1K", "pxa6H", "pya1+ukm/h", "sy+lSpeed"],
x=x,
y=y,
style="n0.4c",
pen="1p",
color="lightseagreen",
)

fig.show()