Skip to content

Commit 0374786

Browse files
seismanmichaelgrundyvonnefroehlich
authored
Figure.shift_origin: Improve docstrings, add inline examples and add type hints (#2879)
Co-authored-by: Michael Grund <[email protected]> Co-authored-by: Yvonne Fröhlich <[email protected]>
1 parent 89a51c0 commit 0374786

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

examples/gallery/3d_plots/scatter3d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
)
9494

9595
# Shift plot origin in x direction
96-
fig.shift_origin(xshift=3.1)
96+
fig.shift_origin(xshift="3.1c")
9797
# Add colorbar legend
9898
fig.colorbar()
9999

pygmt/src/shift_origin.py

+41-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
11
"""
22
shift_origin - Shift plot origin in x and/or y directions.
33
"""
4+
from __future__ import annotations
45

56
from pygmt.clib import Session
67

78

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"""
1013
Shift plot origin in x and/or y directions.
1114
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*:
1527
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
2132
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.
2438
2539
Parameters
2640
----------
27-
xshift : str
41+
xshift
2842
Shift plot origin in x direction.
29-
yshift : str
43+
yshift
3044
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()
3158
"""
3259
self._preprocess()
3360
args = ["-T"]

0 commit comments

Comments
 (0)