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: Revert back to the original plotting origin using a context manager #2509

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ def _repr_html_(self):
wiggle,
)


def set_display(method=None):
"""
Set the display method when calling :meth:`pygmt.Figure.show`.
Expand Down
45 changes: 32 additions & 13 deletions pygmt/src/shift_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
shift_origin - Shift plot origin in x and/or y directions.
"""

from __future__ import annotations

from pygmt.clib import Session
from pygmt.helpers import build_arg_list


def shift_origin(
self, xshift: float | str | None = None, yshift: float | str | None = None
):
class shift_origin: # noqa: N801
r"""
Shift plot origin in x and/or y directions.

Expand Down Expand Up @@ -57,12 +54,34 @@
>>> fig.shift_origin(xshift="w+2c")
>>> fig.show()
"""
self._preprocess()
args = ["-T"]
if xshift:
args.append(f"-X{xshift}")
if yshift:
args.append(f"-Y{yshift}")

with Session() as lib:
lib.call_module(module="plot", args=args)
def __init__(
self, xshift: float | str | None = None, yshift: float | str | None = None
):
"""
Shift the plot origin in x/y directions and store the shift values.
"""
# self._preprocess() # pylint: disable=protected-access
kwdict = {"T": True, "X": xshift, "Y": yshift}
with Session() as lib:
lib.call_module(module="plot", args=build_arg_list(kwdict))
self._xshift = lib.get_common("X") # False or xshift in inches
self._yshift = lib.get_common("Y") # False or yshift in inches

def __enter__(self):
"""
Do nothing but return self.
"""
return self

Check warning on line 75 in pygmt/src/shift_origin.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/shift_origin.py#L75

Added line #L75 was not covered by tests

def __exit__(self, exc_type, exc_value, traceback):
"""
Exit the context manager.
"""
kwdict = {

Check warning on line 81 in pygmt/src/shift_origin.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/shift_origin.py#L81

Added line #L81 was not covered by tests
"T": True,
"X": f"{-1.0 * self._xshift}i" if self._xshift else None,
"Y": f"{-1.0 * self._yshift}i" if self._yshift else None,
}
with Session() as lib:
lib.call_module(module="plot", args=build_arg_list(kwdict))

Check warning on line 87 in pygmt/src/shift_origin.py

View check run for this annotation

Codecov / codecov/patch

pygmt/src/shift_origin.py#L86-L87

Added lines #L86 - L87 were not covered by tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
outs:
- md5: 85e88836e6d32cded37f792de6439fb9
size: 9896
path: test_figure_shift_origin.png
path: test_shift_origin.png
20 changes: 0 additions & 20 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,26 +317,6 @@ def test_figure_show():
fig.show()


@pytest.mark.mpl_image_compare
def test_figure_shift_origin():
"""
Test if fig.shift_origin works.
"""
kwargs = {"region": [0, 3, 0, 5], "projection": "X3c/5c", "frame": 0}
fig = Figure()
# First call shift_origin without projection and region.
# Test issue https://github.com/GenericMappingTools/pygmt/issues/514
fig.shift_origin(xshift="2c", yshift="3c")
fig.basemap(**kwargs)
fig.shift_origin(xshift="4c")
fig.basemap(**kwargs)
fig.shift_origin(yshift="6c")
fig.basemap(**kwargs)
fig.shift_origin(xshift="-4c", yshift="6c")
fig.basemap(**kwargs)
return fig


def test_figure_show_invalid_method():
"""
Test to check if an error is raised when an invalid method is passed to show.
Expand Down
26 changes: 26 additions & 0 deletions pygmt/tests/test_shift_origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Tests Figure.shift_origin.
"""

import pytest
from pygmt import Figure


@pytest.mark.mpl_image_compare
def test_shift_origin():
"""
Test if fig.shift_origin works.
"""
kwargs = {"region": [0, 3, 0, 5], "projection": "X3c/5c", "frame": 0}
fig = Figure()
# First call shift_origin without projection and region.
# Test issue https://github.com/GenericMappingTools/pygmt/issues/514
fig.shift_origin(xshift="2c", yshift="3c")
fig.basemap(**kwargs)
fig.shift_origin(xshift="4c")
fig.basemap(**kwargs)
fig.shift_origin(yshift="6c")
fig.basemap(**kwargs)
fig.shift_origin(xshift="-4c", yshift="6c")
fig.basemap(**kwargs)
return fig
Loading