|
1 | | -r""" |
| 1 | +""" |
2 | 2 | Scale bar |
3 | 3 | ========= |
4 | 4 |
|
5 | | -The ``map_scale`` parameter of the :meth:`pygmt.Figure.basemap` and |
6 | | -:meth:`pygmt.Figure.coast` methods is used to add a scale bar to a map. |
7 | | -This example shows how such a scale bar can be customized: |
8 | | -
|
9 | | - - position: **g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**. Set the position |
10 | | - of the reference point. Choose from |
11 | | -
|
12 | | - - **g**: Give map coordinates as *longitude*\/\ *latitude*. |
13 | | - - **j**\|\ **J**: Specify a |
14 | | - :doc:`2-character justification code </techref/justification_codes>`. |
15 | | - Lower / uppercase **j** / **J** mean inside / outside of the map |
16 | | - bounding box. |
17 | | - - **n**: Give normalized bounding box coordinates as *nx*\/\ *ny*. |
18 | | - - **x**: Give plot coordinates as *x*\/\ *y*. |
19 | | -
|
20 | | - - length: **+w**. Give a distance value, and, optionally a distance unit. |
21 | | - Choose from **e** (meters), **f** (feet), **k** (kilometers) [Default], |
22 | | - **M** (statute miles), **n** (nautical miles), or **u** (US survey feet). |
23 | | - - origin: **+c**\ [*slon*/]\ *slat*. Control where on the map the scale bar |
24 | | - applies. If **+c** is not given the reference point is used. If only |
25 | | - **+c** is appended the middle of the map is used. Note that *slon* is only |
26 | | - optional for projections with constant scale along parallels, e.g., |
27 | | - Mercator projection. |
28 | | - - justify: **+j**. Set the anchor point. Specify a |
29 | | - :doc:`2-character justification code </techref/justification_codes>`. |
30 | | - - offset: **+o**\ *offset* or **+o**\ *xoffset*/\ *yoffset*. Give either a |
31 | | - common shift or individual shifts in x- (longitude) and y- (latitude) |
32 | | - directions. |
33 | | - - height: Use :gmt-term:`MAP_SCALE_HEIGHT` via :func:`pygmt.config`. |
34 | | - - fancy style: **+f**. Get a scale bar that looks like train tracks. |
35 | | - - unit: **+u**. Add the distance unit given via **+w** to the single |
36 | | - distance values. |
37 | | - - label: **+l**. Add the distance unit given via **+w** as label. Append |
38 | | - text to get a customized label instead. |
39 | | - - alignment: **+a**. Set the label alignment. Choose from **t**\(op) |
40 | | - [Default], **b**\(ottom), **l**\(eft), or **r**\(ight). |
| 5 | +The :meth:`pygmt.Figure.scalebar` method can be used to add a scale bar to a map. This |
| 6 | +example shows how such a scale bar can be customized. |
41 | 7 | """ |
42 | 8 |
|
43 | 9 | # %% |
44 | 10 | import pygmt |
45 | | -from pygmt.params import Box |
| 11 | +from pygmt.params import Box, Position |
46 | 12 |
|
47 | 13 | # Create a new Figure instance |
48 | 14 | fig = pygmt.Figure() |
49 | 15 |
|
50 | 16 | # Mercator projection with 10 centimeters width |
51 | 17 | fig.basemap(region=[-45, -25, -15, 0], projection="M0/0/10c", frame=["WSne", "af"]) |
52 | 18 |
|
53 | | -# ----------------------------------------------------------------------------- |
54 | | -# Top Left: Add a plain scale bar |
55 | | -# It is placed based on geographic coordinates (g) 42° West and 1° South, |
56 | | -# applies at the reference point (+c is not given), and represents a |
57 | | -# length (+w) of 500 kilometers |
58 | | -fig.basemap(map_scale="g-42/-1+w500k") |
59 | | - |
60 | | -# ----------------------------------------------------------------------------- |
61 | | -# Top Right: Add a fancy scale bar |
62 | | -# It is placed based on normalized bounding box coordinates (n) |
63 | | -# Use a fancy style (+f) to get a scale bar that looks like train tracks |
64 | | -# Add the distance unit (+u) to the single distance values |
65 | | -fig.basemap(map_scale="n0.8/0.95+w500k+f+u") |
| 19 | +# --- Top Left: Add a plain scale bar --- |
| 20 | +# It is placed based on geographic coordinates 42° West and 1° South, applies at the |
| 21 | +# reference point by default, and represents a length of 500 kilometers. |
| 22 | +fig.scalebar(length="500k", position=Position((-42, -1), cstype="mapcoords")) |
| 23 | + |
| 24 | +# --- Top Right: Add a fancy scale bar --- |
| 25 | +# It is placed based on normalized bounding box coordinates. Use a fancy style to get a |
| 26 | +# scale bar that looks like train tracks. Add the distance unit to the single distance |
| 27 | +# values. |
| 28 | +fig.scalebar( |
| 29 | + position=Position((0.8, 0.95), cstype="boxcoords"), |
| 30 | + length="500k", |
| 31 | + fancy=True, |
| 32 | + unit=True, |
| 33 | +) |
66 | 34 |
|
67 | | -# ----------------------------------------------------------------------------- |
68 | | -# Bottom Left: Add a thick scale bar |
69 | | -# Adjust the GMT default parameter MAP_SCALE_HEIGHT locally (the change applies |
70 | | -# only to the code within the "with" statement) |
71 | | -# It applies (+c) at the middle of the map (no location is appended to +c) |
72 | | -# Without appending text, +l adds the distance unit as label |
73 | | -with pygmt.config(MAP_SCALE_HEIGHT="10p"): |
74 | | - fig.basemap(map_scale="n0.2/0.15+c+w500k+f+l") |
| 35 | +# --- Bottom Left: Add a thick scale bar --- |
| 36 | +# It applies at the middle of the map (scale_loc is set to True). Use the height |
| 37 | +# parameter to adjust the thickness of the scale bar Without providing text, the label |
| 38 | +# parameter adds the distance unit as label. |
| 39 | +fig.scalebar( |
| 40 | + position=Position((0.2, 0.15), cstype="boxcoords"), |
| 41 | + scale_loc=True, |
| 42 | + length="500k", |
| 43 | + height="10p", |
| 44 | + fancy=True, |
| 45 | + label=True, |
| 46 | +) |
75 | 47 |
|
76 | | -# ----------------------------------------------------------------------------- |
77 | | -# Bottom Right: Add a scale bar valid for a specific location |
78 | | -# It is placed at BottomRight (j) using MiddleRight as anchor point (+j) with |
79 | | -# an offset (+o) of 1 centimeter in both x- and y-directions |
80 | | -# It applies (+c) at -7° South, add a customized label by appending text to +l |
81 | | -fig.basemap(map_scale="jBR+jMR+o1c/1c+c-7+w500k+f+u+lvalid at 7° S") |
| 48 | +# --- Bottom Right: Add a scale bar valid for a specific location --- |
| 49 | +# It is placed at BottomRight using MiddleRight as anchor point with an offset of 1 |
| 50 | +# centimeter in both x- and y-directions. It applies at -7° South. A customized label |
| 51 | +# is added via the label parameter. |
| 52 | +fig.scalebar( |
| 53 | + position=Position("BR", anchor="MR", offset=1), |
| 54 | + scale_loc=-7, |
| 55 | + length="500k", |
| 56 | + fancy=True, |
| 57 | + unit=True, |
| 58 | + label="valid at 7° S", |
| 59 | +) |
82 | 60 |
|
83 | 61 | fig.show() |
84 | 62 |
|
85 | 63 |
|
86 | 64 | # %% |
87 | | -# The ``box`` parameter allows surrounding the scale bar. This can be useful |
88 | | -# when adding a scale bar to a colorful map. To fill the box, append **+g** |
89 | | -# with the desired color (or pattern). The outline of the box can be adjusted |
90 | | -# by appending **+p** with the desired thickness, color, and style. To force |
91 | | -# rounded edges append **+r** with the desired radius. |
| 65 | +# The ``box`` parameter allows surrounding the scale bar. This can be useful when adding |
| 66 | +# a scale bar to a colorful map to improve contrast and readability. |
92 | 67 |
|
93 | | -# Create a new Figure instance |
94 | 68 | fig = pygmt.Figure() |
95 | 69 |
|
96 | | -fig.coast( |
97 | | - region=[-45, -25, -15, 0], |
98 | | - projection="M10c", |
99 | | - land="tan", |
100 | | - water="steelblue", |
101 | | - frame=["WSne", "af"], |
102 | | - # Set the label alignment (+a) to right (r) |
103 | | - map_scale="jBL+o1c/1c+c-7+w500k+f+lkm+ar", |
104 | | - # Fill the box in white with a transparency of 30 percent, add a solid |
105 | | - # outline in darkgray (gray30) with a thickness of 0.5 points, and use |
106 | | - # rounded edges with a radius of 3 points |
| 70 | +fig.basemap(region=[-45, -25, -15, 0], projection="M10c", frame=["WSne", "af"]) |
| 71 | +fig.coast(land="tan", water="steelblue") |
| 72 | +fig.scalebar( |
| 73 | + position=Position("BL", cstype="inside", offset=1), |
| 74 | + scale_loc=-7, |
| 75 | + length="500k", |
| 76 | + fancy=True, |
| 77 | + label="km", |
| 78 | + label_alignment="right", |
| 79 | + # Fill the box in white with a transparency of 30 percent, add a solid outline in |
| 80 | + # darkgray (gray30) with a thickness of 0.5 points, and use rounded edges with a |
| 81 | + # radius of 3 points |
107 | 82 | box=Box(fill="white@30", pen="0.5p,gray30,solid", radius="3p"), |
108 | 83 | ) |
109 | 84 |
|
|
0 commit comments