-
Notifications
You must be signed in to change notification settings - Fork 229
Figure.legend: Refactor to simplify the logic of checking legend specification #3437
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
legend - Plot a legend. | ||
""" | ||
|
||
import pathlib | ||
|
||
from pygmt.clib import Session | ||
from pygmt.exceptions import GMTInvalidInput | ||
from pygmt.helpers import ( | ||
|
@@ -26,7 +28,13 @@ | |
t="transparency", | ||
) | ||
@kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence") | ||
def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwargs): | ||
def legend( | ||
self, | ||
spec: str | pathlib.PurePath | None = None, | ||
position="JTR+jTR+o0.2c", | ||
box="+gwhite+p1p", | ||
**kwargs, | ||
): | ||
r""" | ||
Plot legends on maps. | ||
|
||
|
@@ -42,10 +50,14 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg | |
|
||
Parameters | ||
---------- | ||
spec : None or str | ||
Either ``None`` [Default] for using the automatically generated legend | ||
specification file, or a *filename* pointing to the legend | ||
specification file. | ||
spec | ||
The legend specification. It can be: | ||
|
||
- ``None`` means using the automatically generated legend specification file. | ||
- A string or a :class:`pathlib.PurePath` object pointing to the legend | ||
specification file. | ||
|
||
See :gmt-docs:`legend.html` for the definition of the legend specification. | ||
{projection} | ||
{region} | ||
position : str | ||
|
@@ -75,12 +87,11 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg | |
if kwargs.get("F") is None: | ||
kwargs["F"] = box | ||
|
||
kind = data_kind(spec) | ||
if kind not in {"vectors", "file"}: # kind="vectors" means spec is None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line can also be written as:
It's written in the current way so that this line can be extended to check "stringio" kind with minor changes (xref: #3326). It's not straightforward to see why |
||
raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}") | ||
if kind == "file" and is_nonstr_iter(spec): | ||
raise GMTInvalidInput("Only one legend specification file is allowed.") | ||
|
||
with Session() as lib: | ||
if spec is None: | ||
specfile = "" | ||
elif data_kind(spec) == "file" and not is_nonstr_iter(spec): | ||
# Is a file but not a list of files | ||
specfile = spec | ||
else: | ||
raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}") | ||
lib.call_module(module="legend", args=build_arg_list(kwargs, infile=specfile)) | ||
lib.call_module(module="legend", args=build_arg_list(kwargs, infile=spec)) |
Uh oh!
There was an error while loading. Please reload this page.