|
| 1 | +""" |
| 2 | +scalebar - Add a scale bar. |
| 3 | +""" |
| 4 | + |
| 5 | +from collections.abc import Sequence |
| 6 | +from typing import Literal |
| 7 | + |
| 8 | +from pygmt._typing import AnchorCode |
| 9 | +from pygmt.alias import Alias, AliasSystem |
| 10 | +from pygmt.clib import Session |
| 11 | +from pygmt.helpers import build_arg_list, fmt_docstring |
| 12 | +from pygmt.params import Box, Position |
| 13 | +from pygmt.src._common import _parse_position |
| 14 | + |
| 15 | +__doctest_skip__ = ["scalebar"] |
| 16 | + |
| 17 | + |
| 18 | +@fmt_docstring |
| 19 | +def scalebar( # noqa: PLR0913 |
| 20 | + self, |
| 21 | + length: float | str, |
| 22 | + height: float | str | None = None, |
| 23 | + position: Position | Sequence[float | str] | AnchorCode | None = None, |
| 24 | + scale_loc: float | Sequence[float] | bool = False, |
| 25 | + label: str | bool = False, |
| 26 | + label_alignment: Literal["left", "right", "top", "bottom"] | None = None, |
| 27 | + unit: bool = False, |
| 28 | + fancy: bool = False, |
| 29 | + vertical: bool = False, |
| 30 | + box: Box | bool = False, |
| 31 | + verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"] |
| 32 | + | bool = False, |
| 33 | + panel: int | Sequence[int] | bool = False, |
| 34 | + perspective: float | Sequence[float] | str | bool = False, |
| 35 | + transparency: float | None = None, |
| 36 | +): |
| 37 | + """ |
| 38 | + Add a scale bar on the plot. |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + length |
| 43 | + Length of the scale bar in kilometers. Append a suffix to specify another unit. |
| 44 | + Valid units are: **e**: meters; **f**: feet; **k**: kilometers; **M**: statute |
| 45 | + miles; **n**: nautical miles; **u**: US survey feet. |
| 46 | + height |
| 47 | + Height of the scale bar [Default is ``"5p"``]. Only works when ``fancy=True``. |
| 48 | + position |
| 49 | + Position of the scale bar on the plot. It can be specified in multiple ways: |
| 50 | +
|
| 51 | + - A :class:`pygmt.params.Position` object to fully control the reference point, |
| 52 | + anchor point, and offset. |
| 53 | + - A sequence of two values representing the x- and y-coordinates in plot |
| 54 | + coordinates, e.g., ``(1, 2)`` or ``("1c", "2c")``. |
| 55 | + - A :doc:`2-character justification code </techref/justification_codes>` for a |
| 56 | + position inside the plot, e.g., ``"TL"`` for Top Left corner inside the plot. |
| 57 | +
|
| 58 | + If not specified, defaults to the Bottom Left corner of the plot with a 0.2-cm |
| 59 | + and 0.4-cm offset in the x- and y-directions, respectively. |
| 60 | + scale_loc |
| 61 | + Specify the location where the map scale is calculated. It can be: |
| 62 | +
|
| 63 | + - *slat*: Map scale is calculated for latitude *slat*. |
| 64 | + - (*slon*, *slat*): Map scale is calculated for latitude *slat* and longitude |
| 65 | + *slon*, which is useful for oblique projections. |
| 66 | + - ``True``: Map scale is calculated for the middle of the map. |
| 67 | + - ``False``: Default to the location of the reference point. |
| 68 | + label |
| 69 | + Text string for the scale bar label. If ``False``, no label is added. If |
| 70 | + ``True``, the distance unit provided in the ``length`` parameter is used |
| 71 | + [Default is ``"km"``]. Requires ``fancy=True``. |
| 72 | + label_alignment |
| 73 | + Alignment of the scale bar label. Choose from ``"left"``, ``"right"``, |
| 74 | + ``"top"``, or ``"bottom"`` [Default is ``"top"``]. |
| 75 | + fancy |
| 76 | + If ``True``, draw a "fancy" scale bar, which is a segmented bar with alternating |
| 77 | + black and white rectangles. If ``False``, draw a plain scale bar. Only supported |
| 78 | + for non-Cartesian projections. |
| 79 | + unit |
| 80 | + If ``True``, append the unit to all distance annotations along the scale. For a |
| 81 | + plain scale, this will instead select the unit to be appended to the distance |
| 82 | + length. The unit is determined from the suffix provided to the ``length`` |
| 83 | + parameter or defaults to ``"km"``. |
| 84 | + vertical |
| 85 | + If ``True``, plot a vertical rather than a horizontal scale. Only |
| 86 | + supported for Cartesian projections. |
| 87 | + box |
| 88 | + Draw a background box behind the scale bar. If set to ``True``, a simple |
| 89 | + rectangular box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box |
| 90 | + appearance, pass a :class:`pygmt.params.Box` object to control style, fill, pen, |
| 91 | + and other box properties. |
| 92 | + $verbose |
| 93 | + $panel |
| 94 | + $perspective |
| 95 | + $transparency |
| 96 | +
|
| 97 | + Examples |
| 98 | + -------- |
| 99 | + >>> import pygmt |
| 100 | + >>> from pygmt.params import Position |
| 101 | + >>> fig = pygmt.Figure() |
| 102 | + >>> fig.basemap(region=[0, 80, -30, 30], projection="M10c", frame=True) |
| 103 | + >>> fig.scalebar( |
| 104 | + ... length=1000, |
| 105 | + ... position=Position((10, 10), cstype="mapcoords"), |
| 106 | + ... fancy=True, |
| 107 | + ... label="Scale", |
| 108 | + ... unit=True, |
| 109 | + ... ) |
| 110 | + >>> fig.show() |
| 111 | + """ |
| 112 | + self._activate_figure() |
| 113 | + position = _parse_position(position, default=Position("BL", offset=(0.2, 0.4))) |
| 114 | + |
| 115 | + aliasdict = AliasSystem( |
| 116 | + F=Alias(box, name="box"), |
| 117 | + L=[ |
| 118 | + Alias(position, name="position"), |
| 119 | + Alias(length, name="length", prefix="+w"), |
| 120 | + Alias( |
| 121 | + label_alignment, |
| 122 | + name="label_alignment", |
| 123 | + prefix="+a", |
| 124 | + mapping={"left": "l", "right": "r", "top": "t", "bottom": "b"}, |
| 125 | + ), |
| 126 | + Alias(scale_loc, name="scale_loc", prefix="+c", sep="/", size=2), |
| 127 | + Alias(fancy, name="fancy", prefix="+f"), |
| 128 | + Alias(label, name="label", prefix="+l"), |
| 129 | + Alias(unit, name="unit", prefix="+u"), |
| 130 | + Alias(vertical, name="vertical", prefix="+v"), |
| 131 | + ], |
| 132 | + ).add_common( |
| 133 | + V=verbose, |
| 134 | + c=panel, |
| 135 | + p=perspective, |
| 136 | + t=transparency, |
| 137 | + ) |
| 138 | + |
| 139 | + confdict = {} |
| 140 | + if height is not None: |
| 141 | + confdict["MAP_SCALE_HEIGHT"] = height |
| 142 | + |
| 143 | + with Session() as lib: |
| 144 | + lib.call_module( |
| 145 | + module="basemap", args=build_arg_list(aliasdict, confdict=confdict) |
| 146 | + ) |
0 commit comments