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

Wrap legend #333

Merged
merged 28 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e708414
Add Figure.legend() wrapper
liamtoney Dec 31, 2018
60f1b73
Implement handles/labels functionality for Figure.legend()
liamtoney Jan 2, 2019
03fbff8
Merge branch 'master' of https://github.com/GenericMappingTools/gmt-p…
liamtoney Jan 18, 2019
8a19632
Merge branch 'master' of https://github.com/GenericMappingTools/gmt-p…
liamtoney Jan 28, 2019
826318e
Merge branch 'master' of https://github.com/GenericMappingTools/pygmt…
liamtoney Mar 1, 2019
ae6335b
Merge with pygmt master
liamtoney Jul 5, 2019
a6771a1
Use extlinks for legend docstring
liamtoney Jul 5, 2019
69f327e
Merge branch 'master' of https://github.com/GenericMappingTools/pygmt…
liamtoney Oct 8, 2019
95d170d
Hack to allow rc2
liamtoney Oct 8, 2019
cddf2cf
Add test for legend
liamtoney Oct 8, 2019
a57dec3
Revert "Hack to allow rc2"
liamtoney Oct 10, 2019
2ddec23
Merge branch 'master' of https://github.com/GenericMappingTools/pygmt…
liamtoney Oct 10, 2019
da4569c
Use aliases for D and F args
liamtoney Oct 10, 2019
1b07abb
Simplify data_kind() call
liamtoney Oct 10, 2019
88addf7
Flesh out documentation
liamtoney Oct 10, 2019
24330cd
Merge branch 'master' of https://github.com/GenericMappingTools/pygmt…
liamtoney Oct 24, 2019
3a18c63
Alias label (l) for plot
weiji14 Oct 25, 2019
c628f27
Merge pull request #1 from weiji14/plot/alias_label
liamtoney Oct 29, 2019
391e710
Merge branch 'master' into add-legend
liamtoney Oct 29, 2019
a514b59
Use GMT's auto-legend feature instead of regex
liamtoney Oct 29, 2019
519e383
Remove unused GMTTempFile import
liamtoney Oct 29, 2019
9130f47
Revise first legend test
liamtoney Oct 29, 2019
52ae8ee
Add legend entries test
liamtoney Oct 29, 2019
58dba31
Add legend specfile test
liamtoney Oct 29, 2019
f44e98e
Add failing test
liamtoney Oct 31, 2019
e737ca8
Add legend to index.rst
liamtoney Oct 31, 2019
557149d
Remove unneeded decorator
liamtoney Oct 31, 2019
850a96a
Merge branch 'master' into add-legend
weiji14 Oct 31, 2019
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
72 changes: 72 additions & 0 deletions pygmt/base_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
fmt_docstring,
use_alias,
kwargs_to_strings,
GMTTempFile,
)
import re
weiji14 marked this conversation as resolved.
Show resolved Hide resolved


class BasePlotting:
Expand Down Expand Up @@ -345,6 +347,8 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
arg_str = " ".join([fname, build_arg_string(kwargs)])
lib.call_module("plot", arg_str)

return kwargs

@fmt_docstring
@use_alias(
R="region",
Expand Down Expand Up @@ -530,3 +534,71 @@ def image(self, imagefile, **kwargs):
with Session() as lib:
arg_str = " ".join([imagefile, build_arg_string(kwargs)])
lib.call_module("image", arg_str)

@fmt_docstring
@use_alias(R="region", J="projection")
liamtoney marked this conversation as resolved.
Show resolved Hide resolved
@kwargs_to_strings(R="sequence")
def legend(self, spec, **kwargs):
"""
Plot legends on maps. [NEED TO UPDATE FOR HANDLES/LABELS FUNCTIONALITY]

Makes legends that can be overlaid on maps. Reads specific legend-related information from an input file. Unless otherwise noted, annotations will be made using the primary annotation font and size in effect (i.e., FONT_ANNOT_PRIMARY).

Full option list at :gmt-docs:`legend.html`

{aliases}

Parameters
----------
{J}
{R}
D: str
``'[g|j|J|n|x]refpoint+wwidth[/height][+jjustify][+lspacing][+odx[/dy]]'``
Defines the reference point on the map for the legend.
F : bool or str
``'[+cclearances][+gfill][+i[[gap/]pen]][+p[pen]][+r[radius]][+s[[dx/dy/][shade]]]'``
Without further options, draws a rectangular border around the
legend using **MAP_FRAME_PEN**.
"""
kwargs = self._preprocess(**kwargs)
with Session() as lib:
kind = data_kind(spec, None, None, None)
with GMTTempFile() as tmp:
if kind == "file":
specfile = spec
elif kind == "matrix":
specfile = tmp.name
handles = spec[0]
labels = spec[1]
with open(specfile, "w") as file:
for h, text in zip(handles, labels):

# SYMBOL and SIZE
symbol, size = re.findall("\d*\D+", h["S"])
weiji14 marked this conversation as resolved.
Show resolved Hide resolved

# FILL
if "G" not in h.keys():
fill = "-"
else:
fill = h["G"]

# PEN
if "W" not in h.keys():
pen = "-"
elif not h["W"]:
pen = "default,black"
else:
pen = h["W"]

# Write a line to the specfile
file.write(
"S - {} {} {} {} - {}\n".format(
symbol, size, fill, pen, text
)
)
else:
raise GMTInvalidInput(
"Unrecognized data type: {}".format(type(spec))
)
arg_str = " ".join([specfile, build_arg_string(kwargs)])
lib.call_module("legend", arg_str)
2 changes: 1 addition & 1 deletion pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Session:
"""

# The minimum version of GMT required
required_version = "6.0.0rc4"
required_version = "6.0.0rc2"
liamtoney marked this conversation as resolved.
Show resolved Hide resolved

@property
def session_pointer(self):
Expand Down
Binary file added pygmt/tests/baseline/test_legend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions pygmt/tests/test_legend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Tests for legend
"""
import pytest

from .. import Figure


@pytest.mark.mpl_image_compare
def test_legend():
"""
Create 3-entry legend.
"""
fig = Figure()

h1 = fig.plot(
x=[-5],
y=[5],
region=[-10, 10, -5, 10],
projection="X3i/0",
frame="a",
style="a15p",
pen="1.5p,purple",
)

h2 = fig.plot(x=[0], y=[5], style="t10p", color="cyan")

h3 = fig.plot(x=[5], y=[5], style="d5p", color="yellow", pen=True)

fig.legend(
[[h1, h2, h3], ["I am a star!", "I am a triangle!", "I am a diamond!"]],
F=True,
D="g0/0+w2i+jCM",
)

return fig
weiji14 marked this conversation as resolved.
Show resolved Hide resolved