Skip to content

Commit ff1af6b

Browse files
yvonnefroehlichseismanmichaelgrund
authored
Add gallery example "Scale bar" (#2822)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Michael Grund <[email protected]>
1 parent ab5d950 commit ff1af6b

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
r"""
2+
Scale bar
3+
=========
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 two-character (order independent) code.
14+
Choose from vertical **T**\(op), **M**\(iddle), or **B**\(ottom) and
15+
horizontal **L**\(eft), **C**\(entre), or **R**\(ight). Lower / upper
16+
case **j** / **J** mean inside / outside of the map 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 two-character (order
29+
independent) code. Choose from vertical **T**\(op), **M**\(iddle), or
30+
**B**\(ottom) and horizontal **L**\(eft), **C**\(entre), or **R**\(ight).
31+
- offset: **+o**\ *offset* or **+o**\ *xoffset*/\ *yoffset*. Give either a
32+
common shift or individual shifts in x (longitude) and y (latitude)
33+
directions.
34+
- height: Use :gmt-term:`MAP_SCALE_HEIGHT` via :func:`pygmt.config`.
35+
- fancy style: **+f**. Get a scale bar that looks like train tracks.
36+
- unit: **+u**. Add the distance unit given via **+w** to the single
37+
distance values.
38+
- label: **+l**. Add the distance unit given via **+w** as label. Append
39+
text to get a customized label instead.
40+
- alignment: **+a**. Set the label alignment. Choose from **t**\(op)
41+
[Default], **b**\(ottom), **l**\(eft), or **r**\(ight).
42+
"""
43+
44+
# %%
45+
import pygmt
46+
47+
# Create a new Figure instance
48+
fig = pygmt.Figure()
49+
50+
# Mercator projection with 10 centimeters width
51+
fig.basemap(region=[-45, -25, -15, 0], projection="M0/0/10c", frame=["WSne", "af"])
52+
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")
66+
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")
75+
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")
82+
83+
fig.show()
84+
85+
86+
# %%
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.
92+
93+
# Create a new Figure instance
94+
fig = pygmt.Figure()
95+
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
107+
box="+gwhite@30+p0.5p,gray30,solid+r3p",
108+
)
109+
110+
fig.show()
111+
112+
# sphinx_gallery_thumbnail_number = 1

0 commit comments

Comments
 (0)