# Question about insets in PyGMT

**URL:** <https://forum.generic-mapping-tools.org/t/question-about-insets-in-pygmt/1807>\
**Category:** PyGMT Q&A\
**Created:** [June 18, 2021, 9:48pm UTC](https://forum.generic-mapping-tools.org/t/question-about-insets-in-pygmt/1807 "2021-06-18T21:48:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxrjones](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/maxrjones/32/2698_2.png) [@maxrjones](https://forum.generic-mapping-tools.org/u/maxrjones)\
**Post date:** [June 18, 2021, 9:48pm UTC](https://forum.generic-mapping-tools.org/t/question-about-insets-in-pygmt/1807/1 "2021-06-18T21:48:17Z")

</div>

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](https://www.pygmt.org/latest/gallery/embellishments/inset_rectangle_region.html#sphx-glr-gallery-embellishments-inset-rectangle-region-py) 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](https://flint.soest.hawaii.edu/t/different-colorbars-for-subplots/1797/9).

Thanks!

**Example with correct rectangular box in inset region**

```auto
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()

```

 ![correct](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/5/58a526fc9b25cb71fc808bc678ed6c432123022f.png)

**Example with rectangular box offset in inset**

```auto
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()

```

 ![incorrect](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/5/5ab2ace1a84be51cc9373d07625f2d9bf8b9d4b5.png)

---

<div class="post-metadata">

**Author:** ![seisman](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/seisman/32/4_2.png) [@seisman](https://forum.generic-mapping-tools.org/u/seisman)\
**Post date:** [June 19, 2021, 2:45am UTC](https://forum.generic-mapping-tools.org/t/question-about-insets-in-pygmt/1807/2 "2021-06-19T02:45:59Z")

</div>

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)](https://github.com/GenericMappingTools/gmt/issues/1427) 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?

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [June 19, 2021, 11:08am UTC](https://forum.generic-mapping-tools.org/t/question-about-insets-in-pygmt/1807/3 "2021-06-19T11:08:45Z")

</div>

> [@seisman](#):
>
> Perhaps the GMT and PyGMT documentation should be updated to emphasize that users must use `?` in subplot and inset modes?

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](https://flint.soest.hawaii.edu/t/hi-guys-would-you-help-me-find-that-why-my-inset-pic-is-not-complete-thanks/1675)). 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](https://github.com/GenericMappingTools/pygmt/issues/1930).
