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.legend: Support passing a StringIO object as the legend specification #3438

Merged
merged 43 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5e22d55
Add the Session.virtualfile_from_stringio function to support StringI…
seisman Jul 11, 2024
f75844e
Let virtualfile_in support the stringio kind
seisman Jul 11, 2024
50467b5
Make data_kind support stringio
seisman Jul 11, 2024
14a14e9
Refactor Figure.legend to support stringio
seisman Jul 11, 2024
37c3c2f
Add a legend test for StringIO input
seisman Jul 11, 2024
4e2e545
Merge branch 'main' into api/virtualfile-from-stringio
seisman Jul 17, 2024
8e1a609
Some updates
seisman Jul 17, 2024
55d6e81
Merge branch 'main' into api/virtualfile-from-stringio
seisman Jul 20, 2024
b3b2cb9
Merge branch 'main' into api/virtualfile-from-stringio
seisman Jul 21, 2024
89757ec
Fix styling issue
seisman Jul 21, 2024
0489783
Merge branch 'main' into api/virtualfile-from-stringio
seisman Jul 22, 2024
791e4f6
Merge branch 'main' into api/virtualfile-from-stringio
seisman Jul 24, 2024
5f4d21f
Updates
seisman Jul 24, 2024
1a2d336
Merge branch 'main' into api/virtualfile-from-stringio
seisman Jul 26, 2024
3682098
Improve Figure.legend
seisman Jul 26, 2024
21b2496
Merge branch 'main' into api/virtualfile-from-stringio
seisman Sep 11, 2024
146e430
Fix legend
seisman Sep 11, 2024
c589a40
Updates
seisman Sep 11, 2024
ff90b2e
Figure.legend: Refactor to simplify the logic of checking legend spec…
seisman Sep 11, 2024
90455c1
Merge remote-tracking branch 'origin/refactor/legend' into api/virtua…
seisman Sep 12, 2024
4e4bd2d
FIx
seisman Sep 12, 2024
deb917d
Revert changes in legend
seisman Sep 12, 2024
49fa805
Merge branch 'main' into api/virtualfile-from-stringio
seisman Sep 12, 2024
97bbe05
Merge branch 'main' into api/virtualfile-from-stringio
seisman Sep 13, 2024
dece315
Improve docstrings
seisman Sep 13, 2024
486fce7
Support mutli-segment stringio input
seisman Sep 13, 2024
021a97a
Revert "Support mutli-segment stringio input"
seisman Sep 13, 2024
f6da405
Remove the leading '>' from header
seisman Sep 13, 2024
824d861
Also need to set the header pointer to None
seisman Sep 13, 2024
640e9a9
Support mutli-segment stringio input
seisman Sep 13, 2024
3161963
Update docstrings
seisman Sep 13, 2024
026f6e4
Fix a bug in n_rows
seisman Sep 13, 2024
850337e
Add some tests
seisman Sep 13, 2024
ed20118
Fix static type checking
seisman Sep 13, 2024
f1b5f08
Improve the tests
seisman Sep 13, 2024
dfab3c7
Figure.legend: Support passing a StringIO object as the legend specif…
seisman Sep 12, 2024
e10cef6
Simplify the checking of segment header
seisman Sep 13, 2024
ba631c3
Merge branch 'api/virtualfile-from-stringio' into legend/stringio
seisman Sep 14, 2024
045d4b9
Update pygmt/tests/test_legend.py
seisman Sep 16, 2024
22d4d55
Merge branch 'main' into api/virtualfile-from-stringio
seisman Sep 16, 2024
9770e52
Merge branch 'api/virtualfile-from-stringio' into legend/stringio
seisman Sep 16, 2024
8cc5804
Merge branch 'main' into legend/stringio
seisman Sep 18, 2024
c4e9593
Merge branch 'main' into legend/stringio
seisman Sep 19, 2024
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
9 changes: 6 additions & 3 deletions pygmt/src/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
legend - Plot a legend.
"""

import io
import pathlib

from pygmt.clib import Session
Expand Down Expand Up @@ -30,7 +31,7 @@
@kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence")
def legend(
self,
spec: str | pathlib.PurePath | None = None,
spec: str | pathlib.PurePath | io.StringIO | None = None,
position="JTR+jTR+o0.2c",
box="+gwhite+p1p",
**kwargs,
Expand All @@ -57,6 +58,7 @@ def legend(
file
- A string or a :class:`pathlib.PurePath` object pointing to the legend
specification file
- A :class:`io.StringIO` object containing the legend specification.

See :gmt-docs:`legend.html` for the definition of the legend specification.
{projection}
Expand Down Expand Up @@ -89,10 +91,11 @@ def legend(
kwargs["F"] = box

kind = data_kind(spec)
if kind not in {"vectors", "file"}: # kind="vectors" means spec is None
if kind not in {"vectors", "file", "stringio"}: # kind="vectors" means spec is None
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:
lib.call_module(module="legend", args=build_arg_list(kwargs, infile=spec))
with lib.virtualfile_in(data=spec, required_data=False) as vintbl:
lib.call_module(module="legend", args=build_arg_list(kwargs, infile=vintbl))
13 changes: 13 additions & 0 deletions pygmt/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Test Figure.legend.
"""

import io
from pathlib import Path

import pytest
Expand Down Expand Up @@ -100,6 +101,18 @@ def test_legend_specfile(legend_spec):
fig = Figure()
fig.basemap(projection="x6i", region=[0, 1, 0, 1], frame=True)
fig.legend(specfile.name, position="JTM+jCM+w5i")
return fig


@pytest.mark.mpl_image_compare(filename="test_legend_specfile.png")
def test_legend_stringio(legend_spec):
"""
Test passing a legend specification via an io.StringIO object.
"""
spec = io.StringIO(legend_spec)
fig = Figure()
fig.basemap(projection="x6i", region=[0, 1, 0, 1], frame=True)
fig.legend(spec, position="JTM+jCM+w5i")
return fig


Expand Down