|
2 | 2 | Frames, ticks, titles, and labels |
3 | 3 | ================================= |
4 | 4 |
|
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. |
8 | 9 | """ |
9 | 10 |
|
10 | 11 | # %% |
11 | 12 | import pygmt |
12 | | -from pygmt.params import Axis |
| 13 | +from pygmt.params import Axis, Frame |
13 | 14 |
|
14 | 15 | # %% |
15 | 16 | # Plot frame |
16 | 17 | # ---------- |
17 | 18 | # |
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: |
20 | 21 |
|
21 | 22 | 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") |
23 | 24 | fig.show() |
24 | 25 |
|
25 | 26 | # %% |
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: |
29 | 30 |
|
30 | 31 | 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) |
33 | 34 | fig.show() |
34 | 35 |
|
35 | 36 |
|
36 | 37 | # %% |
37 | | -# Ticks and grid lines |
38 | | -# -------------------- |
| 38 | +# Annotations, tick marks and grid lines |
| 39 | +# -------------------------------------- |
39 | 40 | # |
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: |
52 | 48 |
|
53 | 49 | 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)) |
56 | 52 | fig.show() |
57 | 53 |
|
58 | 54 | # %% |
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. |
62 | 58 |
|
63 | 59 | 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)) |
66 | 62 | fig.show() |
67 | 63 |
|
68 | | - |
69 | 64 | # %% |
70 | 65 | # Title |
71 | 66 | # ----- |
72 | 67 | # |
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. |
76 | 70 |
|
77 | 71 | fig = pygmt.Figure() |
78 | 72 | # 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))) |
81 | 75 | fig.show() |
82 | 76 |
|
83 | 77 |
|
84 | 78 | # %% |
85 | 79 | # Axis labels |
86 | 80 | # ----------- |
87 | 81 | # |
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. |
101 | 91 | # |
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. |
104 | 94 |
|
105 | 95 | fig = pygmt.Figure() |
106 | 96 | fig.basemap( |
107 | 97 | region=[0, 10, 0, 20], |
108 | 98 | 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. |
111 | 100 | # 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 | + ), |
113 | 106 | ) |
114 | 107 | fig.show() |
115 | 108 |
|
|
0 commit comments