|
| 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") |
0 commit comments