Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure.shift_origin: Improve docstrings, add inline examples and add type hints #2879

Merged
merged 23 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b786c1c
Figure.shift_origin: Improve docstrings, add inline examples and add …
seisman Dec 14, 2023
187f39f
Apply suggestions from code review
seisman Dec 14, 2023
8ba1353
Apply suggestions from code review
seisman Dec 15, 2023
dafd034
fix
seisman Dec 15, 2023
466c4b4
Merge branch 'main' into docs/shift_origin
seisman Dec 15, 2023
5562b19
Merge branch 'main' into docs/shift_origin
seisman Dec 16, 2023
63ea24b
Merge branch 'main' into docs/shift_origin
seisman Dec 17, 2023
40b08f9
Merge branch 'main' into docs/shift_origin
seisman Dec 21, 2023
0f8744c
Merge branch 'main' into docs/shift_origin
seisman Dec 25, 2023
e5e63ca
Merge branch 'main' into docs/shift_origin
seisman Jan 2, 2024
c325925
Rewrap to 88 characters
seisman Jan 2, 2024
9b6eb9a
Merge branch 'main' into docs/shift_origin
seisman Jan 2, 2024
7ece697
Explicitly set shift unit
seisman Jan 2, 2024
07108e5
Merge branch 'main' into docs/shift_origin
seisman Jan 4, 2024
9c8c878
Merge branch 'main' into docs/shift_origin
seisman Jan 4, 2024
c3dad67
Merge branch 'main' into docs/shift_origin
seisman Jan 5, 2024
6d6a789
Apply suggestions from code review
seisman Jan 5, 2024
1148c88
Rewrap to 88 characters
seisman Jan 5, 2024
677b7c0
Update pygmt/src/shift_origin.py
seisman Jan 6, 2024
8bb1768
Update pygmt/src/shift_origin.py
seisman Jan 6, 2024
69901b6
formatting
seisman Jan 6, 2024
6c2ff6c
Merge branch 'main' into docs/shift_origin
seisman Jan 6, 2024
699450a
Merge branch 'main' into docs/shift_origin
seisman Jan 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/gallery/3d_plots/scatter3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
)

# Shift plot origin in x direction
fig.shift_origin(xshift=3.1)
fig.shift_origin(xshift="3.1c")
# Add colorbar legend
fig.colorbar()

Expand Down
55 changes: 41 additions & 14 deletions pygmt/src/shift_origin.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
"""
shift_origin - Shift plot origin in x and/or y directions.
"""
from __future__ import annotations

from pygmt.clib import Session


def shift_origin(self, xshift=None, yshift=None):
"""
def shift_origin(
self, xshift: float | str | None = None, yshift: float | str | None = None
) -> None:
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
seisman marked this conversation as resolved.
Show resolved Hide resolved
r"""
Shift plot origin in x and/or y directions.

This method shifts the plot origin relative to the current origin
by (*xshift*, *yshift*). Optionally, append the length unit (**c**,
**i**, or **p**). Default unit if not given is **c** for centimeters.
This method shifts the plot origin relative to the current origin by *xshift* and
*yshift* in x and y directions, respectively. Optionally, append the length unit
(**c** for centimeters, **i** for inches, or **p** for points) to the shifts.
Default unit if not explicitly given is **c**, but can be changed to other units via
:gmt-term:`PROJ_LENGTH_UNIT`.

For *xshift*, a special character **w** can also be used, which represents the
bounding box **width** of the previous plot. The full syntax is
[[±][*f*]\ **w**\ [/\ *d*\ ]±]\ *xoff*, where optional signs, factor *f* and divisor
*d* can be used to compute an offset that may be adjusted further by ±\ *xoff*.
Assuming that the previous plot has a width of 10 centimeters, here are some example
values for *xshift*:

Prepend **a** to shift the origin back to the original position after
plotting, prepend **c** to center the plot on the center of the paper
(optionally add shift), prepend **f** to shift the origin relative to
the fixed lower left corner of the page, or prepend **r** [Default] to
move the origin relative to its current location.
- ``"w"``: x-shift is 10 cm
- ``"w+2c"``: x-shift is 10+2=12 cm
- ``"2w+3c"``: x-shift is 2*10+3=23 cm
- ``"w/2-2c"``: x-shift is 10/2-2=3 cm

Detailed usage at
:gmt-docs:`cookbook/options.html#plot-positioning-and-layout-the-x-y-options`
Similarly, for *yshift*, a special character **h** can also be used, which is the
bounding box **height** of the previous plot.

**Note**: The previous plot bounding box refers to the last object plotted, which
may be a basemap, image, logo, legend, colorbar, etc.

Parameters
----------
xshift : str
xshift
Shift plot origin in x direction.
yshift : str
yshift
Shift plot origin in y direction.

Examples
--------
>>> import pygmt
>>> fig = pygmt.Figure()
>>> fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True)
>>> # Shift the plot origin in x direction by 12 cm
>>> fig.shift_origin(xshift=12)
>>> fig.basemap(region=[0, 10, 0, 10], projection="X14c/10c", frame=True)
>>> # Shift the plot origin in x direction based on the previous plot width
>>> # Here, the width is 14 cm, and xshift is 16 cm
>>> fig.shift_origin(xshift="w+2c")
>>> fig.show()
"""
self._preprocess()
args = ["-T"]
Expand Down
Loading