Skip to content

Commit f948ee4

Browse files
authored
DOC: Update Figure.inset in tutorials/gallery examples to the Pythonic syntax (#4320)
1 parent cf17087 commit f948ee4

File tree

3 files changed

+68
-83
lines changed

3 files changed

+68
-83
lines changed

examples/gallery/embellishments/inset.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,32 @@
22
Inset
33
=====
44
5-
The :meth:`pygmt.Figure.inset` method adds an inset figure inside a larger
6-
figure. The method is called using a ``with`` statement, and its ``position``,
7-
``box``, ``offset``, and ``clearance`` parameters are set. Plotting methods
8-
called within the ``with`` statement are applied to the inset figure.
5+
The :meth:`pygmt.Figure.inset` method adds an inset figure inside a larger figure. The
6+
method is called using a ``with`` statement. Plotting methods called within the ``with``
7+
statement are applied to the inset figure.
98
"""
109

1110
# %%
1211
import pygmt
13-
from pygmt.params import Box
12+
from pygmt.params import Box, Position
1413

1514
fig = pygmt.Figure()
16-
# Create the primary figure, setting the region to Madagascar, the land color
17-
# to "brown", the water to "lightblue", the shorelines width to "thin", and
18-
# adding a frame
15+
# Create the primary figure, setting the region to Madagascar, the land color to
16+
# "brown", the water to "lightblue", the shorelines width to "thin", and adding a frame
1917
fig.coast(region="MG+r2", land="brown", water="lightblue", shorelines="thin", frame="a")
2018
# Create an inset, placing it in the Top Left (TL) corner with a width of 3.5 cm and
2119
# x- and y-offsets of 0.2 cm. The clearance is set to 0, and the border is "gold" with a
22-
# pen size of 1.5 points.
23-
with fig.inset(position="jTL+w3.5c+o0.2c", clearance=0, box=Box(pen="1.5p,gold")):
20+
# pen thickness of 1.5 points.
21+
with fig.inset(
22+
position=Position("TL", offset=0.2),
23+
width=3.5,
24+
clearance=0,
25+
box=Box(pen="1.5p,gold"),
26+
):
2427
# Create a figure in the inset using coast. This example uses the azimuthal
25-
# orthogonal projection centered at 47E, 20S. The land color is set to
26-
# "gray" and Madagascar is highlighted in "red3".
28+
# orthogonal projection centered at 47E, 20S. The land color is set to "gray" and
29+
# Madagascar is highlighted in "red3".
2730
fig.coast(
28-
region="g",
29-
projection="G47/-20/?",
30-
land="gray",
31-
water="white",
32-
dcw="MG+gred3",
31+
region="g", projection="G47/-20/?", land="gray", water="white", dcw="MG+gred3"
3332
)
3433
fig.show()

examples/gallery/embellishments/inset_rectangle_region.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,44 @@
22
Inset map showing a rectangular region
33
======================================
44
5-
The :meth:`pygmt.Figure.inset` method adds an inset figure inside a larger
6-
figure. The method is called using a ``with`` statement, and its ``position``,
7-
``box``, ``offset``, and ``clearance`` can be customized. Plotting methods
8-
called within the ``with`` statement plot into the inset figure.
5+
The :meth:`pygmt.Figure.inset` method adds an inset figure inside a larger figure. The
6+
method is called using a ``with`` statement. Plotting methods called within the ``with``
7+
statement plot into the inset figure.
98
"""
109

1110
# %%
1211
import pygmt
13-
from pygmt.params import Box
12+
from pygmt.params import Box, Position
1413

1514
# Set the region of the main figure
1615
region = [137.5, 141, 34, 37]
1716

1817
fig = pygmt.Figure()
19-
20-
# Plot the base map of the main figure. Universal Transverse Mercator (UTM)
21-
# projection is used and the UTM zone is set to be "54S".
18+
# Plot the base map of the main figure. Universal Transverse Mercator (UTM) projection
19+
# is used and the UTM zone is set to be "54S".
2220
fig.basemap(region=region, projection="U54S/12c", frame=["WSne", "af"])
2321

24-
# Set the land color to "lightbrown", the water color to "azure1", the
25-
# shoreline width to "2p", and the area threshold to 1000 km^2 for the main
26-
# figure
22+
# Set the land color to "lightbrown", the water color to "azure1", the shoreline width
23+
# to "2p", and the area threshold to 1000 km^2 for the main figure.
2724
fig.coast(land="lightbrown", water="azure1", shorelines="2p", area_thresh=1000)
2825

29-
# Create an inset map, placing it in the Bottom Right (BR) corner with x- and
30-
# y-offsets of 0.1 cm, respectively.
31-
# The inset map contains the Japan main land. "U54S/3c" means UTM projection
32-
# with a map width of 3 cm. The inset width and height are automatically
33-
# calculated from the specified ``region`` and ``projection`` parameters.
34-
# Draws a rectangular box around the inset with a fill color of "white" and
35-
# a pen of "1p".
26+
# Create an inset map, placing it in the Bottom Right (BR) corner with x- and y-offsets
27+
# of 0.1 cm, respectively. The inset map contains the Japan main land. "U54S/3c" means
28+
# UTM projection with a map width of 3 cm. The inset width and height are automatically
29+
# calculated from the specified ``region`` and ``projection`` parameters. Draws a
30+
# rectangular box around the inset with a fill color of "white" and a pen of "1p".
3631
with fig.inset(
37-
position="jBR+o0.1c",
32+
position=Position("BR", offset=0.1),
3833
box=Box(fill="white", pen="1p"),
3934
region=[129, 146, 30, 46],
4035
projection="U54S/3c",
4136
):
42-
# Highlight the Japan area in "lightbrown"
43-
# and draw its outline with a pen of "0.2p".
44-
fig.coast(
45-
dcw="JP+glightbrown+p0.2p",
46-
area_thresh=10000,
47-
)
48-
# Plot a rectangle ("r") in the inset map to show the area of the main
49-
# figure. "+s" means that the first two columns are the longitude and
50-
# latitude of the bottom left corner of the rectangle, and the last two
51-
# columns the longitude and latitude of the upper right corner.
37+
# Highlight the Japan area in "lightbrown" and draw its outline with a pen of "0.2p"
38+
fig.coast(dcw="JP+glightbrown+p0.2p", area_thresh=10000)
39+
# Plot a rectangle ("r") in the inset map to show the area of the main figure.
40+
# "+s" means that the first two columns are the longitude and latitude of the bottom
41+
# left corner of the rectangle, and the last two columns the longitude and latitude
42+
# of the upper right corner.
5243
rectangle = [[region[0], region[2], region[1], region[3]]]
5344
fig.plot(data=rectangle, style="r+s", pen="2p,blue")
54-
5545
fig.show()

examples/tutorials/advanced/insets.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
=============================
44
55
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.
99
"""
1010

1111
# %%
1212
import pygmt
13-
from pygmt.params import Box
13+
from pygmt.params import Box, Position
1414

1515
# %%
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.
1919

2020
fig = pygmt.Figure()
2121
fig.coast(
@@ -30,15 +30,11 @@
3030
fig.show()
3131

3232
# %%
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.
4238
fig = pygmt.Figure()
4339
fig.coast(
4440
region=[-74, -69.5, 41, 43],
@@ -49,19 +45,17 @@
4945
water="lightblue",
5046
frame="a",
5147
)
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
5550
pass
5651
fig.show()
5752

5853
# %%
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.
6559

6660
fig = pygmt.Figure()
6761
fig.coast(
@@ -73,15 +67,19 @@
7367
water="lightblue",
7468
frame="a",
7569
)
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+
):
7775
pass
7876
fig.show()
7977

8078
# %%
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.
8583

8684
fig = pygmt.Figure()
8785
fig.coast(
@@ -93,11 +91,10 @@
9391
water="lightblue",
9492
frame="a",
9593
)
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.
9996
with fig.inset(
100-
position="jBL+o0.5c/0.2c",
97+
position=Position("BL", offset=(0.5, 0.2)),
10198
box=Box(pen="black"),
10299
region=[-80, -65, 35, 50],
103100
projection="M3c",
@@ -108,8 +105,7 @@
108105
borders=[1, 2],
109106
shorelines="1/thin",
110107
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
113109
)
114110
fig.show()
115111

0 commit comments

Comments
 (0)