Skip to content

Commit 78a9307

Browse files
committed
Length is a required parameter
1 parent 5a31c6c commit 78a9307

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

pygmt/src/scalebar.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pygmt._typing import AnchorCode
99
from pygmt.alias import Alias, AliasSystem
1010
from pygmt.clib import Session
11-
from pygmt.exceptions import GMTInvalidInput
1211
from pygmt.helpers import build_arg_list, fmt_docstring
1312
from pygmt.params import Box, Position
1413
from pygmt.src._common import _parse_position
@@ -19,9 +18,9 @@
1918
@fmt_docstring
2019
def scalebar( # noqa: PLR0913
2120
self,
22-
position: Position | Sequence[float | str] | AnchorCode | None = None,
23-
length: float | str | None = None,
21+
length: float | str,
2422
height: float | str | None = None,
23+
position: Position | Sequence[float | str] | AnchorCode | None = None,
2524
scale_at: float | Sequence[float] | bool = False,
2625
label: str | bool = False,
2726
label_alignment: Literal["left", "right", "top", "bottom"] | None = None,
@@ -40,6 +39,12 @@ def scalebar( # noqa: PLR0913
4039
4140
Parameters
4241
----------
42+
length
43+
Length of the scale bar in km. Append a suffix to specify different units. Valid
44+
units are: **e**: meters; **f**: feet; **k**: kilometers; **M**: statute mile;
45+
**n**: nautical miles; **u**: US Survey foot.
46+
height
47+
Height of the scale bar. Only works when ``fancy=True``. [Default is ``"5p"``].
4348
position
4449
Position of the scale bar on the plot. It can be specified in multiple ways:
4550
@@ -52,12 +57,6 @@ def scalebar( # noqa: PLR0913
5257
5358
If not specified, defaults to the Bottom Left corner of the plot with a 0.2-cm
5459
and 0.4-cm offset in the x- and y-directions, respectively.
55-
length
56-
Length of the scale bar in km. Append a suffix to specify different units. Valid
57-
units are: **e**: meters; **f**: feet; **k**: kilometers; **M**: statute mile;
58-
**n**: nautical miles; **u**: US Survey foot.
59-
height
60-
Height of the scale bar. Only works when ``fancy=True``. [Default is ``"5p"``].
6160
scale_at
6261
Specify the location where the map scale is calculated. It can be:
6362
@@ -100,8 +99,8 @@ def scalebar( # noqa: PLR0913
10099
>>> fig = pygmt.Figure()
101100
>>> fig.basemap(region=[0, 80, -30, 30], projection="M10c", frame=True)
102101
>>> fig.scalebar(
103-
... position=Position((10, 10), cstype="mapcoords"),
104102
... length=1000,
103+
... position=Position((10, 10), cstype="mapcoords"),
105104
... fancy=True,
106105
... label="Scale",
107106
... unit=True,
@@ -110,25 +109,13 @@ def scalebar( # noqa: PLR0913
110109
"""
111110
self._activate_figure()
112111

112+
# Parse the 'position' parameter.
113+
# No need to check conflicts with other parameters since it's a new function.
113114
position = _parse_position(
114115
position,
115-
kwdict={
116-
"length": length,
117-
"height": height,
118-
"label_alignment": label_alignment,
119-
"scale_at": scale_at,
120-
"fancy": fancy,
121-
"label": label,
122-
"unit": unit,
123-
"vertical": vertical,
124-
},
125116
default=Position("BL", offset=(0.2, 0.4)), # Default to "BL" with offset.
126117
)
127118

128-
if length is None:
129-
msg = "Parameter 'length' must be specified."
130-
raise GMTInvalidInput(msg)
131-
132119
aliasdict = AliasSystem(
133120
F=Alias(box, name="box"),
134121
L=[

pygmt/tests/test_scalebar.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import pytest
66
from pygmt import Figure
7-
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.params import Position
98

109

@@ -27,9 +26,9 @@ def test_scalebar_complete():
2726
fig = Figure()
2827
fig.basemap(region=[100, 120, 20, 30], projection="M10c", frame=True)
2928
fig.scalebar(
30-
position=Position((110, 22), cstype="mapcoords"),
3129
length=1000,
3230
height="10p",
31+
position=Position((110, 22), cstype="mapcoords"),
3332
fancy=True,
3433
label="Scale",
3534
label_alignment="left",
@@ -47,16 +46,6 @@ def test_scalebar_cartesian():
4746
"""
4847
fig = Figure()
4948
fig.basemap(region=[0, 10, 0, 5], projection="X10c/5c", frame=True)
50-
fig.scalebar(position=Position((2, 1), cstype="mapcoords"), length=1)
51-
fig.scalebar(position=Position((4, 1), cstype="mapcoords"), length=1, vertical=True)
49+
fig.scalebar(length=1, position=Position((2, 1), cstype="mapcoords"))
50+
fig.scalebar(length=1, position=Position((4, 1), cstype="mapcoords"), vertical=True)
5251
return fig
53-
54-
55-
def test_scalebar_no_length():
56-
"""
57-
Test that an error is raised when length is not provided.
58-
"""
59-
fig = Figure()
60-
fig.basemap(region=[100, 120, 20, 30], projection="M10c", frame=True)
61-
with pytest.raises(GMTInvalidInput):
62-
fig.scalebar(position=Position((118, 22), cstype="mapcoords"))

0 commit comments

Comments
 (0)