88from pygmt ._typing import AnchorCode
99from pygmt .alias import Alias , AliasSystem
1010from pygmt .clib import Session
11+ from pygmt .exceptions import GMTInvalidInputError
1112from pygmt .helpers import build_arg_list
13+ from pygmt .params import Box
1214
1315
1416def scalebar ( # noqa: PLR0913
1517 self ,
16- position ,
17- length ,
18+ position : Sequence [float | str ] | AnchorCode | None = None ,
1819 position_type : Literal [
19- "mapcoords" ,
20- "boxcoords" ,
21- "plotcoords" ,
22- "inside" ,
23- "outside" ,
24- ] = "mapcoords" ,
25- label_alignment : Literal ["left" , "right" , "top" , "bottom" ] | None = None ,
26- scale_position = None ,
27- justify : AnchorCode | None = None ,
20+ "mapcoords" , "boxcoords" , "plotcoords" , "inside" , "outside"
21+ ] = "plotcoords" ,
22+ anchor : AnchorCode | None = None ,
2823 anchor_offset : Sequence [float | str ] | None = None ,
24+ length : float | str | None = None ,
25+ label_alignment : Literal ["left" , "right" , "top" , "bottom" ] | None = None ,
26+ scale_position : float | tuple [float , float ] | bool = False ,
2927 label : str | bool = False ,
3028 fancy : bool = False ,
3129 unit : bool = False ,
3230 vertical : bool = False ,
33- box = None ,
31+ box : Box | bool = False ,
32+ perspective : str | bool = False ,
33+ verbose : Literal ["quiet" , "error" , "warning" , "timing" , "info" , "compat" , "debug" ]
34+ | bool = False ,
35+ transparency : float | None = None ,
3436):
3537 """
36- Add a scale bar.
38+ Add a scale bar on the map.
39+
40+ The scale bar is plotted at the location defined by the reference point (specified
41+ by the **position** and *position_type** parameters) and anchor point (specified by
42+ the **anchor** and **anchor_offset** parameters). Refer to
43+ :doc:`/techref/reference_anchor_points` for details about the positioning.
3744
3845 Parameters
3946 ----------
4047 position/position_type
41- Location of the map scale bar. The actual meaning of this parameter depends
42- on the ``position_type`` parameter.
43- - ``position_type="mapcoords"``: *position* is given as (x, y) in user
48+ Specify the reference point on the map for the directional rose. The reference
49+ point can be specified in five different ways, which is selected by the
50+ **position_type** parameter. The actual reference point is then given by the
51+ coordinates or code specified by the **position** parameter.
52+
53+ The **position_type** parameter can be one of the following:
54+
55+ - ``"mapcoords"``: **position** is given as (*longitude*, *latitude*) in map
4456 coordinates.
45- - ``position_type="boxcoords"``: *position* is given as (nx, ny) in normalized
46- coordinates, where (0, 0) is the lower-left corner and (1, 1) is the
47- upper-right corner of the plot.
48- - ``position_type="plotcoords"``: *position* is given as (x, y) in plot
49- coordinates, i.e., the distances in inches, centimeters, or points from the
50- lower left plot origin.
51- - ``position_type="inside"``: *position* is given as a two-character
52- justification code, meaning the anchor point of the rose is inside the plot
53- bounding box.
54- - ``position_type="outside"``: *position* is given as a two-character
55- justification code, but the rose is outside the plot bounding box.
57+ - ``"boxcoords"``: **position** is given as (*nx*, *ny*) in normalized
58+ coordinates, i.e., fractional coordinates between 0 and 1 in both the x and y
59+ directions. For example, (0, 0) is the lower-left corner and (1, 1) is the
60+ upper-right corner of the plot bounding box.
61+ - ``"plotcoords"``: **position** is given as (x, y) in plot coordinates, i.e.,
62+ the distances in inches, centimeters, or points from the lower left plot
63+ origin.
64+ - ``"inside"`` or ``"outside"``: **position** is one of the nine
65+ :doc:`2-character justification codes </techref/justification_codes>`, meaning
66+ placing the reference point at specific locations, either inside or outside
67+ the plot bounding box.
68+ anchor
69+ Anchor point of the directional rose, specified by one of the
70+ :doc:`2-character justification codes </techref/justification_codes>`.
71+ The default value depends on the **position_type** parameter.
5672
57- Parameters
58- ----------
59- TODO
73+ - ``position_type="inside"``: **anchor** defaults to the same as **position**.
74+ - ``position_type="outside"``: **anchor** defaults to the mirror opposite of
75+ **position**.
76+ - Otherwise, **anchor** defaults to ``"MC"`` (middle center).
77+ anchor_offset
78+ *offset* or (*offset_x*, *offset_y*).
79+ Offset the anchor point by *offset_x* and *offset_y*. If a single value *offset*
80+ is given, *offset_y* = *offset_x* = *offset*.
81+ length
82+ Length of the scale bar in km. You can append different units to the length,
83+ which are:
84+ - **e**: meters
85+ - **f**: feet
86+ - **k**: kilometers
87+ - **M**: statute mile
88+ - **n**: nautical miles
89+ - **u**: US Survey foot
90+ scale_position
91+ Specify the location where on a geographic map the scale applies. It can be:
92+
93+ - *slat*: Map scale is calculated for latitude *slat*
94+ - (*slon*, *slat*): Map scale is calculated for latitude *slat* and longitude
95+ *slon*, which is useful for oblique projections.
96+ - ``True``: Map scale is calculated for the middle of the map.
97+ - ``False``: Default to the location of the reference point.
98+ label
99+ Text string to use as the scale bar label. If ``False``, no label is drawn. If
100+ ``True``, the distance unit provided in the **length** parameter (default is km)
101+ is used as the label. The parameter requires ``fancy=True``.
102+ label_alignment
103+ Alignment of the scale bar label. Choose from "left", "right", "top", or
104+ "bottom". [Default is "top"].
105+ fancy
106+ If ``True``, draw a “fancy” scale bar. A fancy scale bar is a segmented bar with
107+ alternating black and white rectangles. If ``False``, draw a plain scale bar.
108+ unit
109+ If ``True``, append the unit to all distance annotations along the scale. For a
110+ plain scale, this will instead select the unit to be appended to the distance
111+ length. The unit is determined from the suffix in the **length** or defaults to
112+ km.
113+ vertical
114+ If ``True``, plot a vertical rather than a horizontal Cartesian scale.
115+ box
116+ Draw a background box behind the directional rose. If set to ``True``, a simple
117+ rectangular box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box
118+ appearance, pass a :class:`pygmt.params.Box` object to control style, fill, pen,
119+ and other box properties.
120+ {perspective}
121+ {verbose}
122+ {transparency}
60123
61124 Examples
62125 --------
@@ -76,7 +139,15 @@ def scalebar( # noqa: PLR0913
76139 """
77140 self ._preprocess ()
78141
142+ if position is None :
143+ msg = "Parameter 'position' must be specified."
144+ raise GMTInvalidInputError (msg )
145+ if length is None :
146+ msg = "Parameter 'length' must be specified."
147+ raise GMTInvalidInputError (msg )
148+
79149 aliasdict = AliasSystem (
150+ F = Alias (box , name = "box" ),
80151 L = [
81152 Alias (
82153 position_type ,
@@ -90,24 +161,25 @@ def scalebar( # noqa: PLR0913
90161 },
91162 ),
92163 Alias (position , name = "position" , sep = "/" , size = 2 ),
164+ Alias (anchor , name = "justify" , prefix = "+j" ),
165+ Alias (anchor_offset , name = "anchor_offset" , prefix = "+o" , sep = "/" , size = 2 ),
93166 Alias (length , name = "length" , prefix = "+w" ),
94167 Alias (
95168 label_alignment ,
96169 name = "label_alignment" ,
97170 prefix = "+a" ,
98171 mapping = {"left" : "l" , "right" : "r" , "top" : "t" , "bottom" : "b" },
99172 ),
100- Alias (scale_position , name = "scale_position" , prefix = "+c" , sep = "/" ),
173+ Alias (scale_position , name = "scale_position" , prefix = "+c" , sep = "/" , size = 2 ),
101174 Alias (fancy , name = "fancy" , prefix = "+f" ),
102- Alias (justify , name = "justify" , prefix = "+j" ),
103175 Alias (label , name = "label" , prefix = "+l" ),
104- Alias (
105- anchor_offset , name = "anchor_offset" , prefix = "+o" , sep = "/" , size = [1 , 2 ]
106- ),
107176 Alias (unit , name = "unit" , prefix = "+u" ),
108177 Alias (vertical , name = "vertical" , prefix = "+v" ),
109178 ],
110- F = Alias (box , name = "box" ),
179+ p = Alias (perspective , name = "perspective" ),
180+ ).add_common (
181+ V = verbose ,
182+ t = transparency ,
111183 )
112184
113185 with Session () as lib :
0 commit comments