|
3 | 3 | ============================= |
4 | 4 |
|
5 | 5 | To plot an inset figure inside another larger figure, we can use the |
6 | | -:meth:`pygmt.Figure.inset` method. After a large figure has been created, |
7 | | -call ``inset`` using a ``with`` statement, and new plot elements will be |
8 | | -added to the inset figure instead of the larger figure. |
| 6 | +:meth:`pygmt.Figure.inset` method. After a large figure has been created, call ``inset`` |
| 7 | +using a ``with`` statement, and new plot elements will be added to the inset figure |
| 8 | +instead of the larger figure. |
9 | 9 | """ |
10 | 10 |
|
11 | 11 | # %% |
12 | 12 | import pygmt |
13 | | -from pygmt.params import Box |
| 13 | +from pygmt.params import Box, Position |
14 | 14 |
|
15 | 15 | # %% |
16 | | -# Prior to creating an inset figure, a larger figure must first be plotted. In |
17 | | -# the example below, :meth:`pygmt.Figure.coast` is used to create a map of the |
18 | | -# US state of Massachusetts. |
| 16 | +# Prior to creating an inset figure, a larger figure must first be plotted. In the |
| 17 | +# example below, :meth:`pygmt.Figure.coast` is used to create a map of the US state of |
| 18 | +# Massachusetts. |
19 | 19 |
|
20 | 20 | fig = pygmt.Figure() |
21 | 21 | fig.coast( |
|
30 | 30 | fig.show() |
31 | 31 |
|
32 | 32 | # %% |
33 | | -# The :meth:`pygmt.Figure.inset` method uses a context manager, and is called |
34 | | -# using a ``with`` statement. The ``position`` parameter, including the inset |
35 | | -# width, is required to plot the inset. Using the **j** modifier, the location |
36 | | -# of the inset is set to one of the 9 anchors (Top - Middle - Bottom and Left - |
37 | | -# Center - Right). In the example below, ``BL`` places the inset at the Bottom |
38 | | -# Left corner. The ``box`` parameter can set the fill and border of the inset. |
39 | | -# In the example below, ``+pblack`` sets the border color to black and |
40 | | -# ``+glightred`` sets the fill to light red. |
41 | | - |
| 33 | +# The :meth:`pygmt.Figure.inset` method uses a context manager, and is called using a |
| 34 | +# ``with`` statement. The ``position`` parameter, including the inset width, is required |
| 35 | +# to plot the inset. In the example below, the inset is placed at the Bottom Left |
| 36 | +# (``BL``) inside the plot. The ``box`` parameter can set the fill and border of the |
| 37 | +# inset. |
42 | 38 | fig = pygmt.Figure() |
43 | 39 | fig.coast( |
44 | 40 | region=[-74, -69.5, 41, 43], |
|
49 | 45 | water="lightblue", |
50 | 46 | frame="a", |
51 | 47 | ) |
52 | | -with fig.inset(position="jBL+w3c", box=Box(pen="black", fill="lightred")): |
53 | | - # pass is used to exit the with statement as no plotting methods are |
54 | | - # called |
| 48 | +with fig.inset(position=Position("BL"), width=3, box=Box(pen="black", fill="lightred")): |
| 49 | + # pass is used to exit the with statement as no plotting methods are called |
55 | 50 | pass |
56 | 51 | fig.show() |
57 | 52 |
|
58 | 53 | # %% |
59 | | -# When using **j** to set the anchor of the inset, the default location is in |
60 | | -# contact with the nearby axis or axes. The offset of the inset can be set with |
61 | | -# **+o**, followed by the offsets along the x- and y-axes. If only one offset |
62 | | -# is passed, it is applied to both axes. Each offset can have its own unit. In |
63 | | -# the example below, the inset is shifted 0.5 centimeters on the x-axis and |
64 | | -# 0.2 centimeters on the y-axis. |
| 54 | +# When placed at the Bottom Left corner inside the plot, the default location is in |
| 55 | +# contact with the nearby axis or axes. The offsets along the x- and y-axes can be set |
| 56 | +# with the ``offset`` parameter of the ``Position`` class. If only one offset is passed, |
| 57 | +# it is applied to both axes. Each offset can have its own unit. In the example below, |
| 58 | +# the inset is shifted 0.5 centimeters on the x-axis and 0.2 centimeters on the y-axis. |
65 | 59 |
|
66 | 60 | fig = pygmt.Figure() |
67 | 61 | fig.coast( |
|
73 | 67 | water="lightblue", |
74 | 68 | frame="a", |
75 | 69 | ) |
76 | | -with fig.inset(position="jBL+w3c+o0.5c/0.2c", box=Box(pen="black", fill="lightred")): |
| 70 | +with fig.inset( |
| 71 | + position=Position("BL", offset=(0.5, 0.2)), |
| 72 | + width=3, |
| 73 | + box=Box(pen="black", fill="lightred"), |
| 74 | +): |
77 | 75 | pass |
78 | 76 | fig.show() |
79 | 77 |
|
80 | 78 | # %% |
81 | | -# Standard plotting methods can be called from within the ``inset`` context |
82 | | -# manager. The example below uses :meth:`pygmt.Figure.coast` to plot a zoomed |
83 | | -# out map that selectively paints the state of Massachusetts to show its |
84 | | -# location relative to other states. |
| 79 | +# Standard plotting methods can be called from within the ``inset`` context manager. The |
| 80 | +# example below uses :meth:`pygmt.Figure.coast` to plot a zoomed out map that |
| 81 | +# selectively paints the state of Massachusetts to show its location relative to other |
| 82 | +# states. |
85 | 83 |
|
86 | 84 | fig = pygmt.Figure() |
87 | 85 | fig.coast( |
|
93 | 91 | water="lightblue", |
94 | 92 | frame="a", |
95 | 93 | ) |
96 | | -# This does not include an inset fill as it is covered by the inset figure |
97 | | -# Inset width/height are determined by the ``region`` and ``projection`` |
98 | | -# parameters. |
| 94 | +# This does not include an inset fill as it is covered by the inset figure. Inset |
| 95 | +# width/height are determined by the ``region`` and ``projection`` parameters. |
99 | 96 | with fig.inset( |
100 | | - position="jBL+o0.5c/0.2c", |
| 97 | + position=Position("BL", offset=(0.5, 0.2)), |
101 | 98 | box=Box(pen="black"), |
102 | 99 | region=[-80, -65, 35, 50], |
103 | 100 | projection="M3c", |
|
108 | 105 | borders=[1, 2], |
109 | 106 | shorelines="1/thin", |
110 | 107 | water="white", |
111 | | - # Use dcw to selectively highlight an area |
112 | | - dcw="US.MA+gred", |
| 108 | + dcw="US.MA+gred", # Use dcw to selectively highlight an area |
113 | 109 | ) |
114 | 110 | fig.show() |
115 | 111 |
|
|
0 commit comments