{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Making subplots\n\nWhen you're preparing a figure for a paper, there will often be times when\nyou'll need to put many individual plots into one large figure, and label them\n'abcd'. These individual plots are called subplots.\n\nThere are two main ways to create subplots in GMT:\n\n- Use :meth:`pygmt.Figure.shift_origin` to manually move each individual plot\n to the right position.\n- Use :meth:`pygmt.Figure.subplot` to define the layout of the subplots.\n\nThe first method is easier to use and should handle simple cases involving a\ncouple of subplots. For more advanced subplot layouts, however, we recommend\nthe use of :meth:`pygmt.Figure.subplot` which offers finer grained control, and\nthis is what the tutorial below will cover.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pygmt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start by initializing a :class:`pygmt.Figure` instance.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = pygmt.Figure()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define subplot layout\n\nThe :meth:`pygmt.Figure.subplot` function is used to set up the layout, size,\nand other attributes of the figure. It divides the whole canvas into regular\ngrid areas with *n* rows and *m* columns. Each grid area can contain an\nindividual subplot. For example:\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. code-block:: default\n\n with fig.subplot(nrows=2, ncols=3, figsize=(\"15c\", \"6c\"), frame=\"lrtb\"):\n ...\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "will define our figure to have a 2 row and 3 column grid layout.\n``figsize=(\"15c\", \"6c\")`` defines the overall size of the figure to be 15 cm\nwide by 6 cm high. Using ``frame=\"lrtb\"`` allows us to customize the map\nframe for all subplots instead of setting them individually. The figure\nlayout will look like the following:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with fig.subplot(nrows=2, ncols=3, figsize=(\"15c\", \"6c\"), frame=\"lrtb\"):\n for i in range(2): # row number starting from 0\n for j in range(3): # column number starting from 0\n index = i * 3 + j # index number starting from 0\n with fig.set_panel(panel=index): # sets the current panel\n fig.text(\n position=\"MC\",\n text=f\"index: {index}; row: {i}, col: {j}\",\n region=[0, 1, 0, 1],\n )\nfig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The :meth:`pygmt.Figure.set_panel` function activates a specified subplot,\nand all subsequent plotting functions will take place in that subplot panel.\nThis is similar to matplotlib's ``plt.sca`` method. In order to specify a\nsubplot, you will need to provide the identifier for that subplot via the\n``panel`` parameter. Pass in either the *index* number, or a tuple/list like\n(*row*, *col*) to ``panel``.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
The row and column numbering starts from 0. So for a subplot layout with\n N rows and M columns, row numbers will go from 0 to N-1, and column\n numbers will go from 0 to M-1.
All plotting functions (e.g. :meth:`pygmt.Figure.coast`,\n :meth:`pygmt.Figure.text`, etc) are able to use ``panel`` parameter when\n in subplot mode. Once a panel is activated using ``panel`` or\n :meth:`pygmt.Figure.set_panel`, subsequent plotting commands that don't\n set a ``panel`` will have their elements added to the same panel as\n before.