Skip to content

WIP: Figure.logo: Refactor using the new alias system #4014

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

Draft
wants to merge 3 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
69 changes: 62 additions & 7 deletions pygmt/src/logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@
logo - Plot the GMT logo.
"""

from collections.abc import Sequence
from typing import Literal

from pygmt._typing import AnchorCode
from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias


@fmt_docstring
@use_alias(
R="region",
J="projection",
D="position",
F="box",
S="style",
V="verbose",
c="panel",
t="transparency",
)
@kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence")
def logo(self, **kwargs):
def logo(
self,
position: Sequence[str | float] | AnchorCode,
position_type: Literal[
"mapcoords", "boxcoords", "plotcoords", "inside", "outside"
] = "mapcoords",
height=None,
width=None,
justify=None,
anchor_offset=None,
**kwargs,
):
r"""
Plot the GMT logo.

Expand All @@ -35,10 +51,24 @@ def logo(self, **kwargs):
----------
{projection}
{region}
position : str
[**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\
**+w**\ *width*\ [**+j**\ *justify*]\ [**+o**\ *dx*\ [/*dy*]].
Set reference point on the map for the image.
position/position_type
Location of the GMT logo. The actual meaning of this parameter depends on the
``position_type`` parameter.
- ``position_type="mapcoords"``: *position* is given as (x, y) in user
coordinates.
- ``position_type="boxcoords"``: *position* is given as (nx, ny) in normalized
coordinates, where (0, 0) is the lower-left corner and (1, 1) is the
upper-right corner of the plot.
- ``position_type="plotcoords"``: *position* is given as (x, y) in plot
coordinates, i.e., the distances in inches, centimeters, or points from the
lower left plot origin.
- ``position_type="inside"``: *position* is given as a two-character
justification code, meaning the anchor point of the rose is inside the plot
bounding box.
- ``position_type="outside"``: *position* is given as a two-character
justification code, but the rose is outside the plot bounding box.
width/height
Width or height of the GMT logo.
box : bool or str
If set to ``True``, draw a rectangular border around the
GMT logo.
Expand All @@ -55,5 +85,30 @@ def logo(self, **kwargs):
{transparency}
"""
self._activate_figure()
if width is not None and height is not None:
msg = "Cannot specify both width and height."
raise GMTInvalidInput(msg)

aliasdict = AliasSystem(
D=[
Alias(
position_type,
name="position_type",
mapping={
"mapcoords": "g",
"boxcoords": "n",
"plotcoords": "x",
"inside": "j",
"outside": "J",
},
),
Alias(position, name="position", sep="/"),
Alias(height, name="height", prefix="+h"),
Alias(width, name="width", prefix="+w"),
Alias(justify, name="justify", prefix="+j"),
Alias(anchor_offset, name="anchor_offset", prefix="+o", sep="/", size=2),
]
).merge(kwargs)

with Session() as lib:
lib.call_module(module="logo", args=build_arg_list(kwargs))
lib.call_module(module="logo", args=build_arg_list(aliasdict))
8 changes: 7 additions & 1 deletion pygmt/tests/test_logo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ def test_logo_on_a_map():
"""
fig = Figure()
fig.basemap(region=[-90, -70, 0, 20], projection="M15c", frame=True)
fig.logo(position="jTR+o0.25c/0.25c+w7.5c", box=True)
fig.logo(
position_type="justify",
position="TR",
offset=(0.25, 0.25),
width="7.5c",
box=True,
)
return fig