Skip to content

Commit 82d7f76

Browse files
authored
Migrate basics frames tutorial to use the Axis/Frame class (#4596)
1 parent abc292d commit 82d7f76

1 file changed

Lines changed: 50 additions & 57 deletions

File tree

examples/tutorials/basics/frames.py

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,107 @@
22
Frames, ticks, titles, and labels
33
=================================
44
5-
Setting frame, ticks, title, etc., of the plot is handled by the ``frame``
6-
parameter that most plotting methods of the :class:`pygmt.Figure` class
7-
contain.
5+
Setting frame, ticks, titles, and labels is handled by the ``frame`` parameter that
6+
many plotting methods of the :class:`pygmt.Figure` class accept. This tutorial focuses
7+
on setting the ``frame`` parameter using the :class:`pygmt.params.Axis` and
8+
:class:`pygmt.params.Frame` classes.
89
"""
910

1011
# %%
1112
import pygmt
12-
from pygmt.params import Axis
13+
from pygmt.params import Axis, Frame
1314

1415
# %%
1516
# Plot frame
1617
# ----------
1718
#
18-
# By default, PyGMT does not add a frame to your plot. For example, we can plot
19-
# the coastlines of the world with a Mercator projection:
19+
# By default, PyGMT does not add a frame to your plot. For example, we can plot the
20+
# coastlines of the world with a Mercator projection:
2021

2122
fig = pygmt.Figure()
22-
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M25c")
23+
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M15c")
2324
fig.show()
2425

2526
# %%
26-
# To add the default GMT frame style to the plot, use ``frame="f"`` in
27-
# :meth:`pygmt.Figure.basemap` or another plotting method (which has the
28-
# ``frame`` parameter, with the exception of :meth:`pygmt.Figure.colorbar`):
27+
# To add the default GMT frame style to the plot, use ``frame=True`` in
28+
# :meth:`pygmt.Figure.basemap` or another plotting method that accepts a ``frame``
29+
# parameter:
2930

3031
fig = pygmt.Figure()
31-
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M25c")
32-
fig.basemap(frame="f")
32+
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M15c")
33+
fig.basemap(frame=True)
3334
fig.show()
3435

3536

3637
# %%
37-
# Ticks and grid lines
38-
# --------------------
38+
# Annotations, tick marks and grid lines
39+
# --------------------------------------
3940
#
40-
# The automatic frame (``frame=True`` or ``frame="af"``) adds the default GMT
41-
# frame style and automatically determines tick labels from the plot region.
42-
# In GMT the tick labels are called **a**\ nnotations.
43-
44-
fig = pygmt.Figure()
45-
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M25c")
46-
fig.basemap(frame="af")
47-
fig.show()
48-
49-
# %%
50-
# Add automatic grid lines to the plot by passing ``g`` through the ``frame``
51-
# parameter:
41+
# The default frame style includes tick labels (called annotations in GMT) and tick
42+
# marks at intervals determined by GMT, but doesn't include grid lines. To control
43+
# the presence and intervals of annotations, tick marks, and grid lines, you can use the
44+
# :class:`pygmt.params.Axis` class. The ``annot``, ``tick``, and ``grid`` attributes of
45+
# :class:`pygmt.params.Axis` control the presence and intervals of annotations, tick
46+
# marks, and grid lines, respectively. Setting them to ``True`` adds them at intervals
47+
# determined by GMT:
5248

5349
fig = pygmt.Figure()
54-
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M25c")
55-
fig.basemap(frame=Axis(annot=True, grid=True))
50+
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M15c")
51+
fig.basemap(frame=Axis(annot=True, tick=True, grid=True))
5652
fig.show()
5753

5854
# %%
59-
# To adjust the step widths of annotations, frame, and grid lines we can
60-
# add the desired step widths after ``a``, ``f``, or ``g``. In the example
61-
# below, the step widths are set to 30°, 7.5°, and 15°, respectively.
55+
# To set specific intervals, pass values to ``annot``, ``tick``, and ``grid``. In the
56+
# example below, the annotation, tick, and gridline intervals are set to 30, 7.5, and
57+
# 15 degrees, respectively.
6258

6359
fig = pygmt.Figure()
64-
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M25c")
65-
fig.basemap(frame="a30f7.5g15")
60+
fig.coast(shorelines="1/0.5p", region=[-180, 180, -60, 60], projection="M15c")
61+
fig.basemap(frame=Axis(annot=30, tick=7.5, grid=15))
6662
fig.show()
6763

68-
6964
# %%
7065
# Title
7166
# -----
7267
#
73-
# The figure title can be set by passing **+t**\ *title* to the ``frame``
74-
# parameter of :meth:`pygmt.Figure.basemap`. Passing multiple arguments to
75-
# ``frame`` can be done by using a list, as shown in the example below.
68+
# The :class:`pygmt.params.Frame` class lets us configure frame-wide settings such as
69+
# titles. Combine it with an :class:`Axis` object to keep automatic annotations.
7670

7771
fig = pygmt.Figure()
7872
# region="TT" specifies Trinidad and Tobago using the ISO country code
79-
fig.coast(shorelines="1/0.5p", region="TT", projection="M25c")
80-
fig.basemap(frame=["a", "+tTrinidad and Tobago"])
73+
fig.coast(shorelines="1/0.5p", region="TT", projection="M15c")
74+
fig.basemap(frame=Frame(title="Trinidad and Tobago", axis=Axis(annot=True)))
8175
fig.show()
8276

8377

8478
# %%
8579
# Axis labels
8680
# -----------
8781
#
88-
# Axis labels, in GMT simply called labels, can be set by passing
89-
# **x+l**\ *label* (or starting with **y** if
90-
# labeling the y-axis) to the ``frame`` parameter of
91-
# :meth:`pygmt.Figure.basemap`. The map boundaries (or plot axes) are named as
92-
# West/west/left (**W**, **w**, **l**), South/south/bottom
93-
# (**S**, **s**, **b**), North/north/top (**N**, **n**, **t**), and
94-
# East/east/right (**E**, **e**, **r**) sides of a figure. If an uppercase
95-
# letter (**W**, **S**, **N**, **E**) is passed, the axis is plotted with
96-
# tick marks and annotations. The lowercase version
97-
# (**w**, **s**, **n**, **e**) plots the axis only with tick marks.
98-
# To only plot the axis pass **l**, **b**, **t**, **r**. By default
99-
# (``frame=True`` or ``frame="af"``), the West and the South axes are
100-
# plotted with both tick marks and annotations.
82+
# Axis labels, in GMT simply called labels, can be set through the ``xaxis`` and
83+
# ``yaxis`` parameters of :class:`Frame`. The map boundaries (or plot axes) are named as
84+
# West/west/left (**W**, **w**, **l**), South/south/bottom (**S**, **s**, **b**),
85+
# North/north/top (**N**, **n**, **t**), and East/east/right (**E**, **e**, **r**) sides
86+
# of a figure. Uppercase letters (**W**, **S**, **N**, **E**) draw axes with annotations
87+
# and tick marks, lowercase letters (**w**, **s**, **n**, **e**) draw axes with tick
88+
# marks only, and **l**, **b**, **t**, **r** draw plain axis lines without ticks or
89+
# annotations. A frame like ``Frame(axes="WS")`` draws annotated west and south axes
90+
# only.
10191
#
102-
# The example below uses a Cartesian projection, as GMT does not allow
103-
# labels to be set for geographic maps.
92+
# The example below uses a Cartesian projection, as GMT does not allow labels to be set
93+
# for geographic maps.
10494

10595
fig = pygmt.Figure()
10696
fig.basemap(
10797
region=[0, 10, 0, 20],
10898
projection="X10c/8c",
109-
# Plot axis with tick marks, annotations, and labels on the
110-
# West and South axes
99+
# Plot axis with tick marks, annotations, and labels on the West and South axes.
111100
# Plot axis with tick marks on the north and east axes
112-
frame=["WSne", "xaf+lx-axis", "yaf+ly-axis"],
101+
frame=Frame(
102+
axes="WSne",
103+
xaxis=Axis(annot=True, tick=True, label="x-axis"),
104+
yaxis=Axis(annot=True, tick=True, label="y-axis"),
105+
),
113106
)
114107
fig.show()
115108

0 commit comments

Comments
 (0)