Skip to content

Add a new method to shift plot origins #289

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

Merged
merged 6 commits into from
Mar 12, 2019
Merged
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: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Plotting data and laying out the map:
Figure.grdimage
Figure.logo
Figure.image
Figure.shift_origin

Saving and displaying the figure:

Expand Down
3 changes: 2 additions & 1 deletion examples/tutorials/coastlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
oahu = [-158.3, -157.6, 21.2, 21.8]
fig = pygmt.Figure()
for res in ["c", "l", "i", "h", "f"]:
fig.coast(resolution=res, shorelines="1p", region=oahu, projection="M5i", X="5i")
fig.coast(resolution=res, shorelines="1p", region=oahu, projection="M5i")
fig.shift_origin(xshift="5i")
fig.show()

########################################################################################
Expand Down
32 changes: 32 additions & 0 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,38 @@ def show(self, dpi=300, width=500, method="static"):
img = Image(data=png, width=width)
return img

def shift_origin(self, xshift=None, yshift=None):
"""
Shift plot origin in x and/or y directions.

This method shifts plot origin relative to the current origin by (*xshift*,*yshift*)
and optionally append the length unit (**c**, **i**, or **p**).

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.

Detailed usage at http://gmt.soest.hawaii.edu/doc/latest/GMT_Docs.html#plot-positioning-and-layout-the-x-y-options
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the best way to reference the official GMT documentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't know of a good way to do this yet. I'm open to suggestions. It would be best to have this as a variable somewhere and input into the docstring somehow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leouieda Can we rename the gmt_module_docs decorator to something like gmt_docs, which supports following conversions:

{gmt_docs basemap} => http://gmt.soest.hawaii.edu/doc/latest/basemap.html
{gmt_docs gmt xy-full} => http://gmt.soest.hawaii.edu/doc/latest/gmt.html#xy-full

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually like to get rid of most of those decorators in favor of something simpler to maintain. But I don't think there is a good way to format the docstring without using a decorator. We could certainly add that functionality. But we should be using the https://www.generic-mapping-tools.org/gmt/latest/ domain now.

Copy link
Member Author

@seisman seisman Mar 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about putting the raw links in the docstrings, without using a decorator or any substitutions? If someday the GMT documentation site changes, it's still easy to update all the links.


Parameters
----------
xshift : str
Shift plot origin in x direction.
yshift : str
Shift plot origin in y direction.
"""
self._preprocess()
args = ["-T"]
if xshift:
args.append("-X{}".format(xshift))
if yshift:
args.append("-Y{}".format(yshift))

with Session() as lib:
lib.call_module("plot", " ".join(args))

def _preview(self, fmt, dpi, as_bytes=False, **kwargs):
"""
Grab a preview of the figure.
Expand Down
Binary file added pygmt/tests/baseline/test_shift_origin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,17 @@ def test_figure_show():
fig.basemap(R="10/70/-300/800", J="X3i/5i", B="af")
img = fig.show(width=800)
assert img.width == 800


@pytest.mark.mpl_image_compare
def test_shift_origin():
"Test if fig.shift_origin works"
fig = Figure()
fig.basemap(R="10/70/-300/300", J="X3i/5i", B="af")
fig.shift_origin(xshift="4i")
fig.basemap(R="10/70/-300/300", J="X3i/5i", B="af")
fig.shift_origin(yshift="6i")
fig.basemap(R="10/70/-300/300", J="X3i/5i", B="af")
fig.shift_origin(xshift="-4i", yshift="6i")
fig.basemap(R="10/70/-300/300", J="X3i/5i", B="af")
return fig