|
| 1 | +""" |
| 2 | +inset - Create inset figures. |
| 3 | +""" |
| 4 | +import contextlib |
| 5 | + |
| 6 | +from pygmt.clib import Session |
| 7 | +from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias |
| 8 | + |
| 9 | + |
| 10 | +@fmt_docstring |
| 11 | +@contextlib.contextmanager |
| 12 | +@use_alias(D="position", F="box", M="margin", N="no_clip", V="verbose") |
| 13 | +@kwargs_to_strings(D="sequence", M="sequence") |
| 14 | +def inset(self, **kwargs): |
| 15 | + r""" |
| 16 | + Create an inset figure to be placed within a larger figure. |
| 17 | +
|
| 18 | + This function sets the position, frame, and margins for a smaller figure |
| 19 | + inside of the larger figure. Plotting functions that are called within the |
| 20 | + context manager are added to the inset figure. |
| 21 | +
|
| 22 | + Full option list at :gmt-docs:`inset.html` |
| 23 | +
|
| 24 | + {aliases} |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + position : str or list |
| 29 | + *xmin/xmax/ymin/ymax*\ [**+r**][**+u**\ *unit*]] \ |
| 30 | + | [**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\ |
| 31 | + **+w**\ *width*\ [/*height*][**+j**\ *justify*] |
| 32 | + [**+o**\ *dx*\ [/*dy*]] |
| 33 | +
|
| 34 | + *This is the only required argument.* |
| 35 | + Define the map inset rectangle on the map. Specify the rectangle |
| 36 | + in one of three ways: |
| 37 | +
|
| 38 | + Append **g**\ *lon*/*lat* for map (user) coordinates, |
| 39 | + **j**\ *code* or **J**\ *code* for setting the *refpoint* via a |
| 40 | + 2-char justification code \ that refers to the (invisible) |
| 41 | + projected map bounding box, **n**\ *xn*/*yn* for normalized (0-1) |
| 42 | + bounding box coordinates, or **x**\ *x*/*y* for plot |
| 43 | + coordinates (inches, cm, points, append unit). |
| 44 | + All but **x** requires both ``region`` and ``projection`` to be |
| 45 | + specified. You can offset the reference point via |
| 46 | + **+o**\ *dx*/*dy* in the direction implied by *code* or |
| 47 | + **+j**\ *justify*. |
| 48 | +
|
| 49 | + Alternatively, Give *west/east/south/north* of geographic |
| 50 | + rectangle bounded by parallels and meridians; append **+r** if the |
| 51 | + coordinates instead are the lower left and upper right corners of |
| 52 | + the desired rectangle. (Or, give *xmin/xmax/ymin/ymax* of bounding |
| 53 | + rectangle in projected coordinates and optionally |
| 54 | + append **+u**\ *unit* [Default coordinate unit is meter (e)]. |
| 55 | +
|
| 56 | + Append **+w**\ *width*\ [/*height*] of bounding rectangle or box |
| 57 | + in plot coordinates (inches, cm, etc.). By default, the anchor |
| 58 | + point on the scale is assumed to be the bottom left corner (BL), |
| 59 | + but this can be changed by appending **+j** followed by a 2-char |
| 60 | + justification code *justify*. |
| 61 | + **Note**: If **j** is used then *justify* defaults to the same |
| 62 | + as *refpoint*, if **J** is used then *justify* defaults to the |
| 63 | + mirror opposite of *refpoint*. Specify inset box attributes via |
| 64 | + the ``box`` option [outline only]. |
| 65 | + box : str or bool |
| 66 | + [**+c**\ *clearances*][**+g**\ *fill*][**+i**\ [[*gap*/]\ |
| 67 | + *pen*]][**+p**\ [*pen*]][**+r**\ [*radius*]][**+s**\ |
| 68 | + [[*dx*/*dy*/][*shade*]]] |
| 69 | +
|
| 70 | + If passed ``True``, this draws a rectangular box around the map |
| 71 | + inset using the default pen; specify a different pen |
| 72 | + with **+p**\ *pen*. Add **+g**\ *fill* to fill the logo box |
| 73 | + [Default is no fill]. |
| 74 | + Append **+c**\ *clearance* where *clearance* is either |
| 75 | + *gap*, *xgap*\ /\ *ygap*, or *lgap*\ /\ *rgap*\ /\ *bgap*\ /\ |
| 76 | + *tgap* where these items are uniform, separate in x- and |
| 77 | + y-direction, or individual side spacings between logo and border. |
| 78 | + Append **+i** to draw a secondary, inner border as well. We use a |
| 79 | + uniform *gap* between borders of 2\ **p** and the default pen |
| 80 | + unless other values are specified. Append **+r** to draw rounded |
| 81 | + rectangular borders instead, with a 6\ **p** corner radius. You |
| 82 | + can override this radius by appending another value. Append |
| 83 | + **+s** to draw an offset background shaded region. Here, *dx*/*dy* |
| 84 | + indicates the shift relative to the foreground frame |
| 85 | + [4\ **p**/-4\ **p**] and *shade* sets the fill style to use for |
| 86 | + shading [Default is gray50]. |
| 87 | + margin : int or str or list |
| 88 | + This is clearance that is added around the inside of the inset. |
| 89 | + Plotting will take place within the inner region only. The margins |
| 90 | + can be a single value, a pair of values separated (for setting |
| 91 | + separate horizontal and vertical margins), or the full set of four |
| 92 | + margins (for setting separate left, right, bottom, and top |
| 93 | + margins). When passing multiple values, it can be either a list or |
| 94 | + a string with the values separated by forward |
| 95 | + slashes [Default is no margins]. |
| 96 | + no_clip : bool |
| 97 | + Do NOT clip features extruding outside map inset boundaries [Default |
| 98 | + will clip]. |
| 99 | + {V} |
| 100 | +
|
| 101 | + Examples |
| 102 | + -------- |
| 103 | + >>> import pygmt |
| 104 | + >>> |
| 105 | + >>> # Create the larger figure |
| 106 | + >>> fig = pygmt.Figure() |
| 107 | + >>> fig.coast(region="MG+r2", water="lightblue", shorelines="thin") |
| 108 | + >>> # Use a "with" statement to initialize the inset context manager |
| 109 | + >>> # Setting the position to top left and a width of 3.5 centimeters |
| 110 | + >>> with fig.inset(position="jTL+w3.5c+o0.2c", margin=0, box="+pgreen"): |
| 111 | + ... # Map elements under the "with" statement are plotted in the inset |
| 112 | + ... fig.coast( |
| 113 | + ... region="g", |
| 114 | + ... projection="G47/-20/3.5c", |
| 115 | + ... land="gray", |
| 116 | + ... water="white", |
| 117 | + ... dcw="MG+gred", |
| 118 | + ... ) |
| 119 | + ... |
| 120 | + >>> # Map elements outside the "with" block are plotted in the main figure |
| 121 | + >>> fig.logo(position="jBR+o0.2c+w3c") |
| 122 | + >>> fig.show() |
| 123 | + <IPython.core.display.Image object> |
| 124 | + """ |
| 125 | + kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access |
| 126 | + with Session() as lib: |
| 127 | + try: |
| 128 | + lib.call_module("inset", f"begin {build_arg_string(kwargs)}") |
| 129 | + yield |
| 130 | + finally: |
| 131 | + v_arg = build_arg_string({"V": kwargs.get("V")}) |
| 132 | + lib.call_module("inset", f"end {v_arg}".strip()) |
0 commit comments