Skip to content

Commit aa1431d

Browse files
committed
Add Figure.scalebar to plot a scale bar on maps
1 parent 170f82e commit aa1431d

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

pygmt/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ def _repr_html_(self) -> str:
449449
plot3d,
450450
psconvert,
451451
rose,
452+
scalebar,
452453
set_panel,
453454
shift_origin,
454455
solar,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from pygmt.src.project import project
4444
from pygmt.src.psconvert import psconvert
4545
from pygmt.src.rose import rose
46+
from pygmt.src.scalebar import scalebar
4647
from pygmt.src.select import select
4748
from pygmt.src.shift_origin import shift_origin
4849
from pygmt.src.solar import solar

pygmt/src/scalebar.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
scalebar - Add a scale bar.
3+
"""
4+
5+
from typing import Literal
6+
7+
from pygmt.alias import Alias, AliasSystem
8+
from pygmt.clib import Session
9+
from pygmt.helpers import build_arg_list
10+
11+
12+
def scalebar( # noqa: PLR0913
13+
self,
14+
position,
15+
length,
16+
position_type: Literal[
17+
"user", "justify", "mirror", "normalize", "plot", None
18+
] = None,
19+
label_alignment=None,
20+
scale_position=None,
21+
fancy=None,
22+
justify=None,
23+
label=None,
24+
offset=None,
25+
unit=None,
26+
vertical=None,
27+
box=None,
28+
):
29+
"""
30+
Add a scale bar.
31+
32+
Parameters
33+
----------
34+
TODO
35+
36+
Examples
37+
--------
38+
>>> import pygmt
39+
>>> from pygmt.params import Box
40+
>>> fig = pygmt.Figure()
41+
>>> fig.basemap(region=[0, 80, -30, 30], projection="M10c", frame=True)
42+
>>> fig.scalebar(
43+
... position=(10, 10),
44+
... position_type="g",
45+
... length=1000,
46+
... fancy=True,
47+
... label="Scale",
48+
... unit=True,
49+
... box=Box(pen=0.5, fill="lightblue"),
50+
... )
51+
>>> fig.show()
52+
"""
53+
self._preprocess()
54+
55+
aliasdict = AliasSystem(
56+
L=[
57+
Alias(
58+
position_type,
59+
name="position_type",
60+
mapping={
61+
"user": "g",
62+
"justify": "j",
63+
"mirror": "J",
64+
"normalize": "n",
65+
"plot": "x",
66+
},
67+
),
68+
Alias(position, name="position", separator="/"),
69+
Alias(length, name="length", prefix="+w"),
70+
Alias(label_alignment, name="label_alignment", prefix="+a"),
71+
Alias(scale_position, name="scale_position", prefix="+c", separator="/"),
72+
Alias(fancy, name="fancy", prefix="+f"),
73+
Alias(justify, name="justify", prefix="+j"),
74+
Alias(label, name="label", prefix="+l"),
75+
Alias(offset, name="offset", prefix="+o", separator="/"),
76+
Alias(unit, name="unit", prefix="+u"),
77+
Alias(vertical, name="vertical", prefix="+v"),
78+
],
79+
F=Alias(box, name="box"),
80+
)
81+
82+
with Session() as lib:
83+
lib.call_module(module="basemap", args=build_arg_list(aliasdict))

0 commit comments

Comments
 (0)