Skip to content

Commit 072bc16

Browse files
committed
Initial subplot tutorial example
Translated and adapted from https://github.com/gmt-china/GMT_Docs/blob/master/source/tutorial/subplot.rst. Covers basics of using pygmt subplot begin, set and end. Still need to handle gmt "set" using index instead of just row and col.
1 parent 78efa6c commit 072bc16

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
projections/index.rst
3333
tutorials/coastlines.rst
3434
tutorials/plot.rst
35+
tutorials/subplots.rst
3536

3637
.. toctree::
3738
:maxdepth: 2

examples/tutorials/subplots.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
Subplots
3+
========
4+
5+
When you're preparing a figure for a paper, there will often be times when
6+
you'll need to put many individual plots into one large figure, and label them
7+
'abcd'. These individual plots are called subplots.
8+
9+
There are two main ways to handle subplots in GMT:
10+
11+
- Use :meth:`pygmt.Figure.shift_origin` to manually move each individual plot
12+
to the right position.
13+
- Use :meth:`pygmt.Figure.subplot` to define the layout of the subplots.
14+
15+
The first method is easier to use and should handle simple cases involving a
16+
couple of subplots. For more advanced subplot layouts however, we recommend the
17+
use of :meth:`pygmt.Figure.subplot` which offers finer grained control, and
18+
this is what the tutorial below will cover.
19+
"""
20+
21+
###############################################################################
22+
# Let's start by importing the PyGMT library and initiating a figure.
23+
24+
import pygmt
25+
26+
fig = pygmt.Figure()
27+
28+
###############################################################################
29+
# Define subplot layout
30+
# ---------------------
31+
#
32+
# The ``fig.subplot(directive="begin")`` command is used to setup the layout,
33+
# size, and other attributes of the figure. It divides the whole canvas into
34+
# regular grid areas with n rows and m columns. Each grid area can contain an
35+
# individual subplot. For example:
36+
37+
fig.subplot(directive="begin", row=2, col=3, dimensions="s5c/3c", frame="lrtb")
38+
39+
###############################################################################
40+
# will define our figure to have a 2 row and 3 column grid layout.
41+
# ``dimensions="s5c/3c"`` specifies that each 's'ubplot will have a width of
42+
# 5cm and height of 3cm. Alternatively, you can set ``dimensions="f15c/6c"`` to
43+
# define the overall size of the 'f'igure to be 15cm wide by 6cm high. Using
44+
# ``frame="lrtb"`` allows us to customize the map frame for all subplots. The
45+
# figure layout will look like the following:
46+
47+
for index in range(2 * 3):
48+
i = index // 3 # row
49+
j = index % 3 # column
50+
fig.subplot(directive="set", row=i, col=j)
51+
fig.text(
52+
x=0.5, y=0.5, text=f"index: {index}, row: {i}, col: {j}", region=[0, 1, 0, 1],
53+
)
54+
fig.subplot(directive="end")
55+
fig.show()
56+
57+
###############################################################################
58+
# The ``fig.subplot(directive="set")`` command activates a specified subplot,
59+
# and all subsequent plotting commands will take place in that subplot. In
60+
# order to specify a subplot, you will need to know the identifier for each
61+
# subplot. This can be done by setting the ``row`` and ``col`` arguments.
62+
63+
###############################################################################
64+
# .. note::
65+
#
66+
# The row and column numbering starts from 0. So for a subplot layout with
67+
# N rows and M columns, row numbers will go from 0 to N-1, and column
68+
# numbers will go from 0 to M-1.
69+
70+
###############################################################################
71+
# For example, to activate the subplot on the top right corner (index: 2) so
72+
# that all subsequent plotting commands happen there, you can use the following
73+
# command:
74+
75+
###############################################################################
76+
# .. code-block:: default
77+
#
78+
# fig.subplot(directive="set", row=0, col=2)
79+
80+
###############################################################################
81+
# Finally, remember to use ``fig.subplot(directive="end")`` to exit the subplot
82+
# mode.
83+
84+
###############################################################################
85+
# .. code-block:: default
86+
#
87+
# fig.subplot(directive="end")

pygmt/base_plotting.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,8 @@ def subplot(self, directive: str, row: int = None, col: int = None, **kwargs):
911911
The number of columns if using the 'begin' directive, or the column
912912
number if using the 'set' directive. First column is 0, not 1.
913913
dimensions : str
914+
``[f|s]width(s)/height(s)[+fwfracs/hfracs][+cdx/dy][+gfill][+ppen]
915+
[+wpen]``
914916
Specify the dimensions of the figure when using the 'begin'
915917
directive. There are two different ways to do this: (f) Specify
916918
overall figure dimensions or (s) specify the dimensions of a single

0 commit comments

Comments
 (0)