Skip to content

Commit 30a3ae8

Browse files
committed
Figure.inset: width is not required when projection and region are given
1 parent c5d10d4 commit 30a3ae8

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

pygmt/src/inset.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def inset(
7474
If not specified, defaults to the Bottom Left corner of the plot.
7575
width
7676
height
77-
Width and height of the inset. Width must be specified and height is set to be
78-
equal to width if not specified.
77+
Width and height of the inset. Width must be specified unless ``projection`` and
78+
``region`` are given, and height is set to be equal to width if not specified.
7979
box
8080
Draw a background box behind the inset. If set to ``True``, a simple rectangular
8181
box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box appearance,
@@ -132,8 +132,12 @@ def inset(
132132
default=Position((0, 0), cstype="plotcoords"), # Default to (0,0) in plotcoords
133133
)
134134

135-
# width is mandatory.
136-
if width is None and not isinstance(position, str):
135+
# width is mandatory unless both projection and region are given.
136+
if (
137+
not isinstance(position, str)
138+
and width is None
139+
and (projection is None or region is None)
140+
):
137141
msg = "Parameter 'width' must be specified."
138142
raise GMTInvalidInput(msg)
139143

pygmt/tests/test_inset.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ def test_inset_default_position():
5454
return fig
5555

5656

57+
@pytest.mark.mpl_image_compare(filename="test_inset_default_position.png")
58+
def test_inset_width_from_projection_region():
59+
"""
60+
Test that the inset can infer width from projection and region.
61+
"""
62+
fig = Figure()
63+
fig.basemap(region="MG+r2", frame="afg")
64+
with fig.inset(projection="G47/-20/3.5c", region="g", box=True):
65+
fig.basemap(region="g", projection="G47/-20/?", frame="afg")
66+
return fig
67+
68+
5769
@pytest.mark.mpl_image_compare(filename="test_inset_aliases.png")
5870
def test_inset_deprecated_position():
5971
"""

0 commit comments

Comments
 (0)