|
1 | 1 | """
|
2 | 2 | shift_origin - Shift plot origin in x and/or y directions.
|
3 | 3 | """
|
| 4 | +from __future__ import annotations |
4 | 5 |
|
5 | 6 | from pygmt.clib import Session
|
6 | 7 |
|
7 | 8 |
|
8 |
| -def shift_origin(self, xshift=None, yshift=None): |
9 |
| - """ |
| 9 | +def shift_origin( |
| 10 | + self, xshift: float | str | None = None, yshift: float | str | None = None |
| 11 | +): |
| 12 | + r""" |
10 | 13 | Shift plot origin in x and/or y directions.
|
11 | 14 |
|
12 |
| - This method shifts the plot origin relative to the current origin |
13 |
| - by (*xshift*, *yshift*). Optionally, append the length unit (**c**, |
14 |
| - **i**, or **p**). Default unit if not given is **c** for centimeters. |
| 15 | + This method shifts the plot origin relative to the current origin by *xshift* and |
| 16 | + *yshift* in x and y directions, respectively. Optionally, append the length unit |
| 17 | + (**c** for centimeters, **i** for inches, or **p** for points) to the shifts. |
| 18 | + Default unit if not explicitly given is **c**, but can be changed to other units via |
| 19 | + :gmt-term:`PROJ_LENGTH_UNIT`. |
| 20 | +
|
| 21 | + For *xshift*, a special character **w** can also be used, which represents the |
| 22 | + bounding box **width** of the previous plot. The full syntax is |
| 23 | + [[±][*f*]\ **w**\ [/\ *d*\ ]±]\ *xoff*, where optional signs, factor *f* and divisor |
| 24 | + *d* can be used to compute an offset that may be adjusted further by ±\ *xoff*. |
| 25 | + Assuming that the previous plot has a width of 10 centimeters, here are some example |
| 26 | + values for *xshift*: |
15 | 27 |
|
16 |
| - Prepend **a** to shift the origin back to the original position after |
17 |
| - plotting, prepend **c** to center the plot on the center of the paper |
18 |
| - (optionally add shift), prepend **f** to shift the origin relative to |
19 |
| - the fixed lower left corner of the page, or prepend **r** [Default] to |
20 |
| - move the origin relative to its current location. |
| 28 | + - ``"w"``: x-shift is 10 cm |
| 29 | + - ``"w+2c"``: x-shift is 10+2=12 cm |
| 30 | + - ``"2w+3c"``: x-shift is 2*10+3=23 cm |
| 31 | + - ``"w/2-2c"``: x-shift is 10/2-2=3 cm |
21 | 32 |
|
22 |
| - Detailed usage at |
23 |
| - :gmt-docs:`reference/options.html#plot-positioning-and-layout-the-x-y-options` |
| 33 | + Similarly, for *yshift*, a special character **h** can also be used, which is the |
| 34 | + bounding box **height** of the previous plot. |
| 35 | +
|
| 36 | + **Note**: The previous plot bounding box refers to the last object plotted, which |
| 37 | + may be a basemap, image, logo, legend, colorbar, etc. |
24 | 38 |
|
25 | 39 | Parameters
|
26 | 40 | ----------
|
27 |
| - xshift : str |
| 41 | + xshift |
28 | 42 | Shift plot origin in x direction.
|
29 |
| - yshift : str |
| 43 | + yshift |
30 | 44 | Shift plot origin in y direction.
|
| 45 | +
|
| 46 | + Examples |
| 47 | + -------- |
| 48 | + >>> import pygmt |
| 49 | + >>> fig = pygmt.Figure() |
| 50 | + >>> fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True) |
| 51 | + >>> # Shift the plot origin in x direction by 12 cm |
| 52 | + >>> fig.shift_origin(xshift=12) |
| 53 | + >>> fig.basemap(region=[0, 10, 0, 10], projection="X14c/10c", frame=True) |
| 54 | + >>> # Shift the plot origin in x direction based on the previous plot width |
| 55 | + >>> # Here, the width is 14 cm, and xshift is 16 cm |
| 56 | + >>> fig.shift_origin(xshift="w+2c") |
| 57 | + >>> fig.show() |
31 | 58 | """
|
32 | 59 | self._preprocess()
|
33 | 60 | args = ["-T"]
|
|
0 commit comments