Skip to content

Commit 1a8ea29

Browse files
committed
Add Figure.directional_rose for adding directional rose on maps
1 parent b4a94d7 commit 1a8ea29

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Plotting map elements
2727
Figure.basemap
2828
Figure.coast
2929
Figure.colorbar
30+
Figure.directional_rose
3031
Figure.hlines
3132
Figure.inset
3233
Figure.legend

pygmt/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ def _repr_html_(self) -> str:
413413
coast,
414414
colorbar,
415415
contour,
416+
directional_rose,
416417
grdcontour,
417418
grdimage,
418419
grdview,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pygmt.src.config import config
1111
from pygmt.src.contour import contour
1212
from pygmt.src.dimfilter import dimfilter
13+
from pygmt.src.directional_rose import directional_rose
1314
from pygmt.src.filter1d import filter1d
1415
from pygmt.src.grd2cpt import grd2cpt
1516
from pygmt.src.grd2xyz import grd2xyz

pygmt/src/directional_rose.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
directional_rose - Add a map directional rose.
3+
"""
4+
5+
from collections.abc import Sequence
6+
from typing import Literal
7+
8+
from pygmt.alias import Alias, AliasSystem
9+
from pygmt.clib import Session
10+
from pygmt.exceptions import GMTInvalidInput
11+
from pygmt.helpers import build_arg_list
12+
from pygmt.params import Box, Position
13+
14+
15+
def directional_rose(
16+
self,
17+
position: Position | None = None,
18+
width: float | str | None = None,
19+
label: Sequence[str] | bool = False,
20+
fancy: Literal[1, 2, 3] | bool = False,
21+
box: Box | bool = False,
22+
perspective: str | bool = False,
23+
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
24+
| bool = False,
25+
transparency: float | None = None,
26+
):
27+
"""
28+
Add a directional rose on the map.
29+
30+
Parameters
31+
----------
32+
position
33+
Specify the position of the directional rose on a map. See
34+
:class:`pygmt.params.Position` for details.
35+
width
36+
Width of the rose in plot coordinates (append **i** (inch), **cm**
37+
(centimeters), or **p** (points)), or append % for a size in percentage of map
38+
width [Default is 10 %].
39+
label
40+
A sequence of four strings to label the cardinal points W,E,S,N. Use an empty
41+
string to skip a specific label. If set to ``True``, use the default labels
42+
``["W", "E", "S", "N"]``.
43+
fancy
44+
Get a fancy rose. The fanciness level can be set to 1, 2, or 3:
45+
46+
- Level 1 draws the two principal E-W, N-S orientations
47+
- Level 2 adds the two intermediate NW-SE and NE-SW orientations
48+
- Level 3 adds the four minor orientations WNW-ESE, NNW-SSE, NNE-SSW, and
49+
ENE-WSW
50+
51+
If set to ``True``, defaults to level 1.
52+
box
53+
Draw a background box behind the directional rose. If set to ``True``, a simple
54+
rectangular box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box
55+
appearance, pass a :class:`pygmt.params.Box` object to control style, fill, pen,
56+
and other box properties.
57+
$perspective
58+
$verbose
59+
$transparency
60+
61+
Examples
62+
--------
63+
>>> import pygmt
64+
>>> fig = pygmt.Figure()
65+
>>> fig.basemap(region=[0, 80, -30, 30], projection="M10c", frame=True)
66+
>>> fig.directional_rose(position=Position((10, 10), cstype="mapcoords"))
67+
>>> fig.show()
68+
"""
69+
self._activate_figure()
70+
71+
if position is None:
72+
msg = "Parameter 'position' is required."
73+
raise GMTInvalidInput(msg)
74+
75+
aliasdict = AliasSystem(
76+
F=Alias(box, name="box"),
77+
Td=[
78+
Alias(position, name="position"),
79+
Alias(width, name="width", prefix="+w"),
80+
Alias(fancy, name="fancy", prefix="+f"), # +F is not supported yet.
81+
Alias(label, name="label", prefix="+l", sep=",", size=4),
82+
],
83+
).add_common(
84+
V=verbose,
85+
p=perspective,
86+
t=transparency,
87+
)
88+
89+
with Session() as lib:
90+
lib.call_module(module="basemap", args=build_arg_list(aliasdict))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: 4bf9dc9a74af3df5fe03ba4cdfaf27ca
3+
size: 14424
4+
hash: md5
5+
path: test_directional_rose_complex.png
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Test Figure.directional_rose.
3+
"""
4+
5+
import pytest
6+
from pygmt import Figure
7+
8+
9+
@pytest.mark.mpl_image_compare(filename="test_basemap_rose.png")
10+
def test_directional_rose():
11+
"""
12+
Test the Figure.directional_rose method.
13+
"""
14+
fig = Figure()
15+
fig.basemap(region=[127.5, 128.5, 26, 27], projection="H15c", frame=True)
16+
fig.directional_rose(position="MC", position_type="inside", width="5c")
17+
return fig
18+
19+
20+
@pytest.mark.mpl_image_compare
21+
def test_directional_rose_complex():
22+
"""
23+
Test the Figure.directional_rose method with more parameters.
24+
"""
25+
fig = Figure()
26+
fig.basemap(region=[0, 80, -30, 30], projection="M10c", frame=True)
27+
fig.directional_rose(
28+
position=(50, 0),
29+
position_type="mapcoords",
30+
width="1c",
31+
label=["", "", "", "N"],
32+
fancy=2,
33+
anchor="MC",
34+
anchor_offset=(1, 1),
35+
)
36+
return fig

pygmt/tests/test_inset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ def test_inset_context_manager():
3030
fig.basemap(region=[-74, -69.5, 41, 43], projection="M9c", frame=True)
3131
with fig.inset(position="jBL+w3c+o0.2c", margin=0, box=Box(pen="black")):
3232
fig.basemap(region=[-80, -65, 35, 50], projection="M3c", frame="afg")
33-
fig.basemap(rose="jTR+w3c") # Pass rose argument with basemap after the inset
33+
# Plot an rose after the inset
34+
fig.directional_rose(position="TR", position_type="inside", width="3c")
3435
return fig

0 commit comments

Comments
 (0)