Question about insets in PyGMT

Hi all,

I assumed that region and projection only need to be set once within a Figure.inset() with block, but when working off the inset rectangle gallery example my student found that the output of the plot call shifts based on the region set in the coast call, unless projection and region are also set in the plot. I have copied a minimal example below. Is this to be expected? I am not sure if this is an oddity about hierarchical levels similar to the subplot question.

Thanks!

Example with correct rectangular box in inset region

import pygmt

region=[-91.25,-91,-0.95,-0.7]
inset_region = [-92,-89.5,-1.75,1]
inset_projection = "M3.5c"
data = pygmt.datasets.load_earth_relief(region=region, resolution='03s')
fig = pygmt.Figure()
pygmt.makecpt(cmap="batlow", series=[0, 1135])
fig.grdimage(
    grid=data,
    region=region,
    shading=True,
    cmap=True,
    frame=True,
    projection="M10c"
)
with fig.inset(position="jBL+w3.5c+o0.2c", box="+pblack"):
    fig.coast(
        region=inset_region,
        shorelines="thin",
        projection=inset_projection,
        land="lightyellow",
        water="lightblue",
        frame="a"
    )
    rectangle = [[region[0], region[2], region[1], region[3]]]
    fig.plot(data=rectangle, style="r+s", projection=inset_projection, region=inset_region, pen="1p,blue")
fig.show()

Example with rectangular box offset in inset

import pygmt

region=[-91.25,-91,-0.95,-0.7]
inset_region = [-92,-89.5,-1.75,1]
inset_projection = "M3.5c"
data = pygmt.datasets.load_earth_relief(region=region, resolution='03s')
fig = pygmt.Figure()
pygmt.makecpt(cmap="batlow", series=[0, 1135])
fig.grdimage(
    grid=data,
    region=region,
    shading=True,
    cmap=True,
    frame=True,
    projection="M10c"
)
with fig.inset(position="jBL+w3.5c+o0.2c", box="+pblack"):
    fig.coast(
        region=inset_region,
        shorelines="thin",
        projection=inset_projection,
        land="lightyellow",
        water="lightblue",
        frame="a"
    )
    rectangle = [[region[0], region[2], region[1], region[3]]]
    fig.plot(data=rectangle, style="r+s", pen="1p,blue")
fig.show()

I believe you just find another common use error in GMT and PyGMT.

The short answer is, in an “inset” or a “subplot”, you have to use the question sign ? in the -J option to let GMT automatically determine the projection width. Your 2nd example works well if you change inset_projection to M?.

See Wrong -J history in subplot mode · Issue #1427 · GenericMappingTools/gmt (github.com) for some discussions about the exact same problem (but in “subplot” mode).

Perhaps the GMT and PyGMT documentation should be updated to emphasize that users must use ? in subplot and inset modes?

1 Like

Yes, the offset in subplot looks like a common problem (see Hi, guys, would you help me find that why my inset pic is not complete, thanks). We should definitely have some clearer guidance around this in the documentation. Open an issue on GitHub perhaps?

Edit: Related issue at Wrong point location in inset · Issue #1930 · GenericMappingTools/pygmt · GitHub.