Skip to content

Figure.shift_origin: Support shifting origins temporarily when used as a context manager #2509

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 7 commits into from
Feb 24, 2025

Conversation

seisman
Copy link
Member

@seisman seisman commented Apr 21, 2023

See #2401 for context.

In the old versions, Figure.shift_origin(...) can only shift origins permanently, but sometimes users may want to shift origins temporarily (i.e., shifting the origins to a new location, plotting and then reverting back to the previous origins).

This PR refactors Figure.shift_origin into a context manager so that it can shift origins permanently when used as a regular method or temporarily when used as a context manager.

Old documentation: https://www.pygmt.org/v0.14.0/api/generated/pygmt.Figure.shift_origin.html
Preview the new documentation: https://pygmt-dev--2509.org.readthedocs.build/en/2509/api/generated/pygmt.Figure.shift_origin.html

Enhanced syntax

The new syntax is:

# Shift origins permanently
Figure.shift_origin(...)

# Shift origins temporarily
with Figure.shift_origin(...):
    ...

Examples

Use as a regular method:

import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 5, 0, 5], projection="X5c/5c", frame=True)
# Shift the plot origin in x direction by 6 cm
fig.shift_origin(xshift=6)
fig.basemap(region=[0, 7, 0, 5], projection="X7c/5c", frame=True)
# Shift the plot origin in x direction based on the previous plot width.
# Here, the width is 7 cm, and xshift is 8 cm.
fig.shift_origin(xshift="w+1c")
fig.basemap(region=[0, 10, 0, 5], projection="X10c/5c", frame=True)
fig.show()

Use as a context manager:

fig = pygmt.Figure()
fig.basemap(region=[0, 5, 0, 5], projection="X5c/5c", frame=True)
# Shift the plot origin in x direction by 6 cm temporarily. The plot origin will
# revert back to the original plot origin after the block of code is executed.
with fig.shift_origin(xshift=6):
    fig.basemap(region=[0, 5, 0, 5], projection="X5c/5c", frame=True)
# Shift the plot origin in y direction by 6 cm temporarily.
with fig.shift_origin(yshift=6):
    fig.basemap(region=[0, 5, 0, 5], projection="X5c/5c", frame=True)
# Shift the plot origin in x and y directions by 6 cm temporarily.
with fig.shift_origin(xshift=6, yshift=6):
    fig.basemap(region=[0, 5, 0, 5], projection="X5c/5c", frame=True)
fig.show()

@seisman seisman added the enhancement Improving an existing feature label Apr 21, 2023
@seisman seisman added this to the 0.10.0 milestone Apr 21, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jul 4, 2023

Summary of changed images

This is an auto-generated report of images that have changed on the DVC remote

Status Path
added pygmt/tests/baseline/test_shift_origin_context_manager.png
added pygmt/tests/baseline/test_shift_origin_mixed_modes.png
added pygmt/tests/baseline/test_shift_origin_nested_context_manager.png

Image diff(s)

Added images

  • test_shift_origin_context_manager.png

  • test_shift_origin_mixed_modes.png

  • test_shift_origin_nested_context_manager.png

Modified images

Path Old New

Report last updated at commit a75a440

@seisman seisman modified the milestones: 0.10.0, 0.11.0 Sep 1, 2023
@seisman seisman modified the milestones: 0.11.0, 0.12.0 Dec 11, 2023
@seisman seisman removed this from the 0.12.0 milestone Feb 26, 2024
@seisman seisman self-assigned this Oct 30, 2024
@GenericMappingTools GenericMappingTools deleted a comment from github-actions bot Feb 19, 2025
@seisman seisman force-pushed the shift_origin-context-manager branch 2 times, most recently from cda4b8c to 3c57bf5 Compare February 19, 2025 14:19
@seisman seisman added this to the 0.15.0 milestone Feb 19, 2025
@seisman seisman marked this pull request as ready for review February 19, 2025 14:21
@seisman seisman force-pushed the shift_origin-context-manager branch from 3c57bf5 to c15f549 Compare February 19, 2025 14:27
@seisman seisman added feature Brand new feature needs review This PR has higher priority and needs review. and removed enhancement Improving an existing feature labels Feb 19, 2025
@seisman seisman changed the title Figure.shift_origin: Revert back to the original plotting origin using a context manager Figure.shift_origin: Support shifting origins temporarily when used as a context manager Feb 19, 2025
@seisman seisman added enhancement Improving an existing feature and removed feature Brand new feature labels Feb 19, 2025
@seisman
Copy link
Member Author

seisman commented Feb 19, 2025

Finally make it work as expected. @GenericMappingTools/pygmt-maintainers Please try the new Figure.shift_origin method and see if it works well.

@seisman seisman requested a review from a team February 20, 2025 05:13
Copy link
Member

@michaelgrund michaelgrund left a comment

Choose a reason for hiding this comment

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

Looks good!

Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Feb 20, 2025
@seisman
Copy link
Member Author

seisman commented Feb 20, 2025

It would be better if @yvonnefroehlich and @weiji14 could give this PR a review, since it's a big change to the Figure.shift_origin method.

Shift plot origin in x and/or y directions.
Shift the plot origin in x and/or y directions.

The shifts can be temporary or permanent. If used as a context manager, the shifts
Copy link
Member

@yvonnefroehlich yvonnefroehlich Feb 21, 2025

Choose a reason for hiding this comment

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

Wonder if it is better to change the order to first mention permanent and then temporary, as in the examples at the end. At least I would have this always in the same order.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. Done in 0d489dc.

Comment on lines 42 to 43
For *xshift*, a special character **w** can also be used, which represents the
bounding box **width** of the previous plot. The full syntax is
Copy link
Member

Choose a reason for hiding this comment

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

How do w (x direction) and h (y direction) work for the temporary option? Is this also refering to the previous plot, or actually the current plot? In case it refers to the previous plot, what is used if the current plot is the first plot?

Copy link
Member Author

Choose a reason for hiding this comment

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

How do w (x direction) and h (y direction) work for the temporary option? Is this also refering to the previous plot, or actually the current plot?

It always refers to the previous plot.

In case it refers to the previous plot, what is used if the current plot is the first plot?

Figure.shift_origin has no effects if it is called before any plotting methods:

import pygmt

fig = pygmt.Figure()
with fig.shift_origin(xshift="w+2c"):
    fig.basemap(region=[0, 1, 0, 1], projection="X3c", frame=0)
fig.show()

seisman and others added 3 commits February 22, 2025 10:20
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
@seisman
Copy link
Member Author

seisman commented Feb 24, 2025

Will merge this PR in 24 hours if no further comments.

Copy link
Member

@yvonnefroehlich yvonnefroehlich left a comment

Choose a reason for hiding this comment

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

Should be fine from my side!

@seisman seisman merged commit fb718b2 into main Feb 24, 2025
24 checks passed
@seisman seisman deleted the shift_origin-context-manager branch February 24, 2025 13:58
@seisman
Copy link
Member Author

seisman commented Feb 24, 2025

Thanks for all your reviews. I've merged this PR.

@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants