-
Notifications
You must be signed in to change notification settings - Fork 235
Wrap subplot using with statement #822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
f2ac98b
Wrap subplot - version 3
weiji14 683ff57
Add fig.subplot and fig.sca to API docs
weiji14 0742da7
Merge branch 'master' into subplot_v3
weiji14 95e3a64
Alias fixedlabel (A), clearance (C), verbose (V) for sca
weiji14 8283f65
Turn fig.sca into a context manager
weiji14 92b7fbd
Alias projection (J), region (R), verbose (V), x/yshift (X/Y) for sub…
weiji14 6cb2e59
Merge branch 'master' into subplot_v3
weiji14 9d6f20b
Fix subplot end -V
weiji14 3f0c008
Improve docstring of fig.subplot and fig.sca
weiji14 967fba3
Small tweaks to subplot tutorial
weiji14 3de8489
Merge branch 'master' into subplot_v3
weiji14 14ae58c
Allow list or tuple inputs to ax (c) argument in basemap
weiji14 ddee738
Use sequence_comma instead of sequence
weiji14 7a2662a
Merge branch 'master' into subplot_v3
weiji14 dbd4677
Remove use of matplotlib-like Axes (axs) object
weiji14 4126c16
Allow for spaces in title and labels without needing double quotes
weiji14 df9f5f0
Merge branch 'master' into subplot_v3
weiji14 0521851
Ensure that a list can be passed into region (R)
weiji14 6eb58ea
Allow for list inputs into fig.sca(ax=...)
weiji14 2d042eb
Merge branch 'master' into subplot_v3
weiji14 d70cce0
Merge branch 'master' into subplot_v3
weiji14 b03a5dd
Rename sca to set_panel and ax to panel
weiji14 1e46b71
Merge branch 'master' into subplot_v3
weiji14 6ba4d49
Update docstring for layout (S) to say +w is for the figsize/subsize arg
weiji14 d3c0a50
Apply formatting suggestions from code review
weiji14 eadb847
Fix bug that prevented boolean to -A from working
weiji14 f568982
Add note that subplot panel is activated until further notice
weiji14 2c239c2
Merge branch 'master' into subplot_v3
weiji14 6c1ed41
Remove arg_str strip() for subplot set and end
weiji14 fab1b9d
Split layout (S) into sharex (SC) and sharey (SR)
weiji14 3f042d0
Merge branch 'master' into subplot_v3
weiji14 ad34871
Fix small typo
weiji14 ef9ee2d
Proofread edits to subplot and set_panel docstring
weiji14 c6b0242
Update fig.set_panel clearance docstring with code-block example
weiji14 c475861
Validate subplot nrows/ncols and figsize/subsize argument inputs
weiji14 a48b600
Proof edits to subplot round 2
weiji14 2f18fe7
Merge branch 'master' into subplot_v3
weiji14 f4499a8
Revise advanced subplot layout subsection to use two subplots calls
weiji14 e0900e9
Proofread edits to subplot round 3
weiji14 ebc807e
Replace argument with parameter as per #886
weiji14 d16de28
Merge branch 'master' into subplot_v3
weiji14 30ef9c4
Proofread edits to subplot round 4
weiji14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -441,6 +441,7 @@ function-naming-style=snake_case | |
| good-names=i, | ||
| j, | ||
| k, | ||
| ax, | ||
| ex, | ||
| Run, | ||
| _, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| """ | ||
| Subplots | ||
| ======== | ||
|
|
||
| When you're preparing a figure for a paper, there will often be times when | ||
| you'll need to put many individual plots into one large figure, and label them | ||
| 'abcd'. These individual plots are called subplots. | ||
|
|
||
| There are two main ways to create subplots in GMT: | ||
|
|
||
| - Use :meth:`pygmt.Figure.shift_origin` to manually move each individual plot | ||
| to the right position. | ||
| - Use :meth:`pygmt.Figure.subplot` to define the layout of the subplots. | ||
|
|
||
| The first method is easier to use and should handle simple cases involving a | ||
| couple of subplots. For more advanced subplot layouts however, we recommend the | ||
| use of :meth:`pygmt.Figure.subplot` which offers finer grained control, and | ||
| this is what the tutorial below will cover. | ||
| """ | ||
|
|
||
| ############################################################################### | ||
| # Let's start by importing the PyGMT library and initializing a Figure | ||
|
|
||
| import pygmt | ||
|
|
||
| fig = pygmt.Figure() | ||
|
|
||
| ############################################################################### | ||
| # Define subplot layout | ||
| # --------------------- | ||
| # | ||
| # The :meth:`pygmt.Figure.subplot` command is used to setup the layout, size, | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # and other attributes of the figure. It divides the whole canvas into regular | ||
| # grid areas with n rows and m columns. Each grid area can contain an | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # individual subplot. For example: | ||
|
|
||
| ############################################################################### | ||
| # .. code-block:: default | ||
| # | ||
| # with fig.subplot( | ||
| # nrows=2, ncols=3, figsize=("15c", "6c"), frame="lrtb" | ||
| # ) as axs: | ||
| # ... | ||
|
|
||
| ############################################################################### | ||
| # will define our figure to have a 2 row and 3 column grid layout. | ||
| # ``figsize=("15c", "6c")`` defines the overall size of the figure to be 15cm | ||
| # wide by 6cm high. Using ``frame="lrtb"`` allows us to customize the map frame | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # for all subplots instead of setting them individually. The figure layout will | ||
| # look like the following: | ||
|
|
||
| with fig.subplot(nrows=2, ncols=3, figsize=("15c", "6c"), frame="lrtb") as axs: | ||
| for index in axs.flatten(): | ||
| i = index // axs.shape[1] # row | ||
| j = index % axs.shape[1] # column | ||
| with fig.sca(ax=axs[i, j]): # sets the current Axes | ||
| fig.text( | ||
| position="MC", | ||
| text=f"index: {index}, row: {i}, col: {j}", | ||
| region=[0, 1, 0, 1], | ||
| ) | ||
| fig.show() | ||
|
|
||
| ############################################################################### | ||
| # The :meth:`pygmt.Figure.sca` command activates a specified subplot, and all | ||
| # subsequent plotting commands will take place in that subplot. This is similar | ||
| # to matplotlib's ``plt.sca`` method. In order to specify a subplot, you will | ||
| # need to provide the identifier for that subplot via the ``ax`` argument. This | ||
| # can be found in the ``axs`` variable referenced by the ``row`` and ``col`` | ||
| # number. | ||
|
|
||
| ############################################################################### | ||
| # .. note:: | ||
| # | ||
| # The row and column numbering starts from 0. So for a subplot layout with | ||
| # N rows and M columns, row numbers will go from 0 to N-1, and column | ||
| # numbers will go from 0 to M-1. | ||
|
|
||
| ############################################################################### | ||
| # For example, to activate the subplot on the top right corner (index: 2) at | ||
| # ``row=0`` and ``col=2``, so that all subsequent plotting commands happen | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # there, you can use the following command: | ||
|
|
||
| ############################################################################### | ||
| # .. code-block:: default | ||
| # | ||
| # with fig.sca(ax=axs[0, 2]): | ||
| # ... | ||
|
|
||
| ############################################################################### | ||
| # Making your first subplot | ||
| # ------------------------- | ||
| # Next, let's use what we learned above to make a 2 row by 2 column subplot | ||
| # figure. We'll also pick up on some new parameters to configure our subplot. | ||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fig = pygmt.Figure() | ||
| with fig.subplot( | ||
| nrows=2, | ||
| ncols=2, | ||
| figsize=("15c", "6c"), | ||
| autolabel=True, | ||
| margins=["0.1c", "0.2c"], | ||
| title='"My Subplot Heading"', | ||
| ) as axs: | ||
| fig.basemap( | ||
| region=[0, 10, 0, 10], projection="X?", frame=["af", "WSne"], ax=axs[0, 0] | ||
| ) | ||
| fig.basemap( | ||
| region=[0, 20, 0, 10], projection="X?", frame=["af", "WSne"], ax=axs[0, 1] | ||
| ) | ||
| fig.basemap( | ||
| region=[0, 10, 0, 20], projection="X?", frame=["af", "WSne"], ax=axs[1, 0] | ||
| ) | ||
| fig.basemap( | ||
| region=[0, 20, 0, 20], projection="X?", frame=["af", "WSne"], ax=axs[1, 1] | ||
| ) | ||
| fig.show() | ||
|
|
||
| ############################################################################### | ||
| # In this example, we define a 2-row, 2-column (2x2) subplot layout using | ||
| # :meth:`pygmt.Figure.subplot`. The overall figure dimensions is set to be 15cm | ||
| # wide and 6cm high (``figsize=["15c", "6c"]``). In addition, we used some | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # optional parameters to fine tune some details of the figure creation: | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
| # - ``autolabel=True``: Each subplot is automatically labelled abcd | ||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # - ``margins=["0.1c", "0.2c"]``: adjusts the space between adjacent subplots. | ||
| # In this case, it is set as 0.1 cm in the X direction and 0.2 cm in the Y | ||
| # direction. | ||
| # - ``title="My Subplot Heading"``: adds a title on top of the whole figure. | ||
| # | ||
| # Notice that each subplot was set to use a linear projection ``"X?"``. | ||
| # Usually, we need to specify the width and height of the map frame, but it is | ||
| # also possible to use a question mark ``"?"`` to let GMT decide automatically | ||
| # on what is the most appropriate width/height for the each subplot's map | ||
| # frame. | ||
|
|
||
| ############################################################################### | ||
| # .. tip:: | ||
| # | ||
| # In the above example, we used the following commands to activate the | ||
| # four subplots explicitly one after another:: | ||
| # | ||
| # fig.basemap(..., ax=axs[0, 0]) | ||
| # fig.basemap(..., ax=axs[0, 1]) | ||
| # fig.basemap(..., ax=axs[1, 0]) | ||
| # fig.basemap(..., ax=axs[1, 1]) | ||
| # | ||
| # In fact, we can just use ``fig.basemap(..., ax=True)`` without specifying | ||
| # any subplot index number, and GMT will automatically activate the next | ||
| # subplot. | ||
|
|
||
| ############################################################################### | ||
| # Shared X and Y axis labels | ||
| # -------------------------- | ||
| # In the example above with the four subplots, the two subplots for each row | ||
| # have the same Y-axis range, and the two subplots for each column have the | ||
| # same X-axis range. You can use the **layout** option to set a common X and/or | ||
weiji14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Y axis between subplots. | ||
|
|
||
| fig = pygmt.Figure() | ||
| with fig.subplot( | ||
| nrows=2, | ||
| ncols=2, | ||
| figsize=("15c", "6c"), | ||
| autolabel=True, | ||
| margins=["0.3c", "0.2c"], | ||
| title='"My Subplot Heading"', | ||
| layout=["Rl", "Cb"], | ||
| frame="WSrt", | ||
| ) as axs: | ||
| fig.basemap(region=[0, 10, 0, 10], projection="X?", ax=True) | ||
| fig.basemap(region=[0, 20, 0, 10], projection="X?", ax=True) | ||
| fig.basemap(region=[0, 10, 0, 20], projection="X?", ax=True) | ||
| fig.basemap(region=[0, 20, 0, 20], projection="X?", ax=True) | ||
| fig.show() | ||
|
|
||
| ############################################################################### | ||
| # **Rl** indicates that subplots within a **R**\ ow will share the y-axis, and | ||
| # only the **l**\ eft axis is displayed. **Cb** indicates that subplots in | ||
| # a column will share the x-axis, and only the **b**\ ottom axis is displayed. | ||
| # | ||
| # Of course, instead of using the **layout** option, you can also set a | ||
| # different **frame** for each subplot to control the axis properties | ||
| # individually for each subplot. | ||
|
|
||
| ############################################################################### | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Advanced subplot layouts | ||
| # ------------------------ | ||
| # | ||
| # Nested subplot are currently not supported. If you want to create more | ||
| # complex subplot layouts, some manual adjustments are needed. | ||
| # | ||
| # The following example draws three subplots in a 2-row, 2-column layout, with | ||
| # the first subplot occupying the first row. | ||
|
|
||
| fig = pygmt.Figure() | ||
| with fig.subplot(nrows=2, ncols=2, figsize=("15c", "6c"), autolabel=True): | ||
| fig.basemap( | ||
| region=[0, 10, 0, 10], projection="X15c/3c", frame=["af", "WSne"], ax=axs[0, 0] | ||
| ) | ||
| fig.text(text="TEXT", x=5, y=5, projection="X15c/3c") | ||
| fig.basemap( | ||
| region=[0, 5, 0, 5], projection="X?", frame=["af", "WSne"], ax=axs[1, 0] | ||
| ) | ||
| fig.basemap( | ||
| region=[0, 5, 0, 5], projection="X?", frame=["af", "WSne"], ax=axs[1, 1] | ||
| ) | ||
| fig.show() | ||
|
|
||
| ############################################################################### | ||
| # | ||
| # When drawing the three basemaps, the last two basemaps use | ||
| # ``projection="X?"``, so GMT will automatically determine the size of the | ||
| # subplot according to the size of the subplot area. In order for the first | ||
| # subplot to fill up the entire top row space, we use manually adjusted the | ||
| # subplot width to 15cm using ``projection="X15c/3c"``. | ||
|
|
||
| ############################################################################### | ||
| # .. note:: | ||
| # | ||
| # There are bugs that have not been fixed in the above example. | ||
| # | ||
| # In subplot mode, the size of each subgraph is controlled by the | ||
| # ``figsize`` option of :meth:`pygmt.Figure.subplot`. Users can override | ||
| # this and use``projection`` to specify the size of an individual subplot, | ||
| # but this size will not be remembered. If the next command does not | ||
| # specify``projection``, the default size of the subplot mode will be used, | ||
| # and the resulting plot will be inccorect. | ||
| # | ||
| # The current workaround is to use the same ``projection`` option in all | ||
| # commands for the subplot. For example, we forced subplot (a) to have a | ||
| # different size using ``projection="15c/3c``. The next command within the | ||
| # subplot (e.g. ``text``) must also use ``projection="x15c/3c"``, otherwise | ||
| # the placement will be wrong. | ||
|
|
||
| ############################################################################### | ||
| # Since we skipped the second subplot, the auto label function will name the | ||
| # three subplots as a, c and d, which is not what we want, so we have to use | ||
| # ``fig.sca(fixedlabel="(a)"`` to manually set the subplot label. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.