Skip to content

Commit 17d8427

Browse files
committed
Update Figure.scalebar and add tests
1 parent 37d02bc commit 17d8427

File tree

4 files changed

+90
-23
lines changed

4 files changed

+90
-23
lines changed

pygmt/src/scalebar.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,36 @@
22
scalebar - Add a scale bar.
33
"""
44

5+
from collections.abc import Sequence
56
from typing import Literal
67

78
from pygmt.alias import Alias, AliasSystem
89
from pygmt.clib import Session
9-
from pygmt.exceptions import GMTInvalidInputError
10+
from pygmt.exceptions import GMTInvalidInput
1011
from pygmt.helpers import build_arg_list
1112
from pygmt.params import Box, Position
1213

1314

1415
def scalebar( # noqa: PLR0913
1516
self,
16-
position: Position,
17+
position: Position | None = None,
1718
length: float | str | None = None,
18-
label_alignment: Literal["left", "right", "top", "bottom"] | None = None,
19-
scale_position: float | tuple[float, float] | bool = False,
19+
scale_position: float | Sequence[float] | bool = False,
2020
label: str | bool = False,
21-
fancy: bool = False,
21+
label_alignment: Literal["left", "right", "top", "bottom"] | None = None,
2222
unit: bool = False,
23+
fancy: bool = False,
2324
vertical: bool = False,
2425
box: Box | bool = False,
25-
perspective: str | bool = False,
2626
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
2727
| bool = False,
28+
panel: int | Sequence[int] | bool = False,
2829
transparency: float | None = None,
30+
perspective: float | Sequence[float] | str | bool = False,
2931
):
3032
"""
3133
Add a scale bar on the map.
3234
33-
The scale bar is plotted at the location defined by the reference point (specified
34-
by the **position** and *position_type** parameters) and anchor point (specified by
35-
the **anchor** and **anchor_offset** parameters). Refer to
36-
:doc:`/techref/reference_anchor_points` for details about the positioning.
37-
3835
Parameters
3936
----------
4037
position
@@ -59,19 +56,19 @@ def scalebar( # noqa: PLR0913
5956
- ``False``: Default to the location of the reference point.
6057
label
6158
Text string to use as the scale bar label. If ``False``, no label is drawn. If
62-
``True``, the distance unit provided in the **length** parameter (default is km)
63-
is used as the label. The parameter requires ``fancy=True``.
59+
``True``, the distance unit provided in the ``length` parameter (default is km)
60+
is used as the label. This parameter requires ``fancy=True``.
6461
label_alignment
65-
Alignment of the scale bar label. Choose from "left", "right", "top", or
66-
"bottom". [Default is "top"].
62+
Alignment of the scale bar label. Choose from ``"left"``, ``"right"``,
63+
``"top"``, or ``"bottom"``. [Default is ``"top"``].
6764
fancy
68-
If ``True``, draw a fancy scale bar. A fancy scale bar is a segmented bar with
69-
alternating black and white rectangles. If ``False``, draw a plain scale bar.
65+
If ``True``, draw a "fancy" scale bar, which is a segmented bar with alternating
66+
black and white rectangles. If ``False``, draw a plain scale bar.
7067
unit
7168
If ``True``, append the unit to all distance annotations along the scale. For a
7269
plain scale, this will instead select the unit to be appended to the distance
73-
length. The unit is determined from the suffix in the **length** or defaults to
74-
km.
70+
length. The unit is determined from the suffix in the ``length`` or defaults to
71+
``"km"``.
7572
vertical
7673
If ``True``, plot a vertical rather than a horizontal Cartesian scale.
7774
box
@@ -98,14 +95,14 @@ def scalebar( # noqa: PLR0913
9895
... )
9996
>>> fig.show()
10097
"""
101-
self._preprocess()
98+
self._activate_figure()
10299

103100
if position is None:
104101
msg = "Parameter 'position' must be specified."
105-
raise GMTInvalidInputError(msg)
102+
raise GMTInvalidInput(msg)
106103
if length is None:
107104
msg = "Parameter 'length' must be specified."
108-
raise GMTInvalidInputError(msg)
105+
raise GMTInvalidInput(msg)
109106

110107
aliasdict = AliasSystem(
111108
F=Alias(box, name="box"),
@@ -124,9 +121,10 @@ def scalebar( # noqa: PLR0913
124121
Alias(unit, name="unit", prefix="+u"),
125122
Alias(vertical, name="vertical", prefix="+v"),
126123
],
127-
p=Alias(perspective, name="perspective"),
128124
).add_common(
129125
V=verbose,
126+
c=panel,
127+
p=perspective,
130128
t=transparency,
131129
)
132130

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: 0bc967a510306086752fae3b556cc7e2
3+
size: 10201
4+
hash: md5
5+
path: test_scalebar.png
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: 5a339623e1c78d80484f99321b56ddf2
3+
size: 11712
4+
hash: md5
5+
path: test_scalebar_complete.png

pygmt/tests/test_scalebar.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Test Figure.scalebar.
3+
"""
4+
5+
import pytest
6+
from pygmt import Figure
7+
from pygmt.exceptions import GMTInvalidInput
8+
from pygmt.params import Position
9+
10+
11+
@pytest.mark.mpl_image_compare
12+
def test_scalebar():
13+
"""
14+
Create a map with a scale bar.
15+
"""
16+
fig = Figure()
17+
fig.basemap(region=[100, 120, 20, 30], projection="M10c", frame=True)
18+
fig.scalebar(position=Position((118, 22), cstype="mapcoords"), length=200)
19+
return fig
20+
21+
22+
@pytest.mark.mpl_image_compare
23+
def test_scalebar_complete():
24+
"""
25+
Test all parameters of scalebar.
26+
"""
27+
fig = Figure()
28+
fig.basemap(region=[100, 120, 20, 30], projection="M10c", frame=True)
29+
fig.scalebar(
30+
position=Position((110, 22), cstype="mapcoords"),
31+
length=1000,
32+
fancy=True,
33+
label="Scale",
34+
label_alignment="left",
35+
scale_position=(110, 25),
36+
unit=True,
37+
box=True,
38+
)
39+
return fig
40+
41+
42+
def test_scalebar_no_position():
43+
"""
44+
Test that an error is raised when position is not provided.
45+
"""
46+
fig = Figure()
47+
fig.basemap(region=[100, 120, 20, 30], projection="M10c", frame=True)
48+
with pytest.raises(GMTInvalidInput):
49+
fig.scalebar(length=200)
50+
51+
52+
def test_scalebar_no_length():
53+
"""
54+
Test that an error is raised when length is not provided.
55+
"""
56+
fig = Figure()
57+
fig.basemap(region=[100, 120, 20, 30], projection="M10c", frame=True)
58+
with pytest.raises(GMTInvalidInput):
59+
fig.scalebar(position=Position((118, 22), cstype="mapcoords"))

0 commit comments

Comments
 (0)