Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gallery example "Scale bar" #2822

Merged
merged 52 commits into from
Dec 25, 2023
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
d5394f2
Add basic code and documentation
yvonnefroehlich Nov 19, 2023
3808564
[format-command] fixes
actions-bot Nov 19, 2023
38540cb
Remove 'box'
yvonnefroehlich Nov 19, 2023
bbf06d2
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Nov 20, 2023
8b8bad7
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Nov 20, 2023
0851337
Expand documentation
Nov 21, 2023
1bec367
Update example for 'box'
yvonnefroehlich Nov 23, 2023
0804898
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Nov 30, 2023
6d14f88
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 1, 2023
617b715
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 5, 2023
5c169a7
Fix typos
yvonnefroehlich Dec 7, 2023
0875174
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 8, 2023
7a5c127
Use alias 'box' for 'F'
Dec 8, 2023
dcd8941
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 14, 2023
54e7ed3
Improve docs
yvonnefroehlich Dec 16, 2023
aa9fa95
Add blank line at end of file
yvonnefroehlich Dec 16, 2023
19468ea
Add comments
yvonnefroehlich Dec 17, 2023
9b8281c
Replace tabs by white spaces
yvonnefroehlich Dec 17, 2023
ec322e4
Fix rst syntax
yvonnefroehlich Dec 17, 2023
019732f
Fix rst syntax
yvonnefroehlich Dec 17, 2023
0981d57
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 17, 2023
ad63900
Improve comments, fix typos
yvonnefroehlich Dec 17, 2023
9af9b1e
Better include origin of length scale
yvonnefroehlich Dec 17, 2023
047d01d
Fix typo
Dec 18, 2023
55bbaf4
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 18, 2023
b7fc7da
Fix typo
Dec 18, 2023
7222642
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 18, 2023
909678c
Improve code and docs
Dec 18, 2023
64c811a
Remove blank line
Dec 18, 2023
e6fc3a0
Improve code and docs reagarding +c
yvonnefroehlich Dec 18, 2023
94c9ac4
Shorten docs
yvonnefroehlich Dec 18, 2023
14a7cfd
Fix typos
yvonnefroehlich Dec 18, 2023
4430223
Fix typos
yvonnefroehlich Dec 18, 2023
93bcff0
Fix typos
yvonnefroehlich Dec 18, 2023
beaa1a6
Extend docs for position
yvonnefroehlich Dec 19, 2023
937fe2e
Add docs for distance unit
yvonnefroehlich Dec 19, 2023
6023891
Use 'or' consistently
yvonnefroehlich Dec 19, 2023
8f29791
Fix typo
yvonnefroehlich Dec 19, 2023
fcf3164
Replace "|" by "or"
yvonnefroehlich Dec 19, 2023
0cfdf75
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 19, 2023
88baf28
Fix line length
yvonnefroehlich Dec 19, 2023
b08b6be
Improv
yvonnefroehlich Dec 19, 2023
d8065ed
Fix line length and add article
yvonnefroehlich Dec 19, 2023
fd504d0
Fix typo
yvonnefroehlich Dec 20, 2023
cf0d1a6
Improve argument passed to 'xshift'
yvonnefroehlich Dec 22, 2023
1335750
Improve argument passed to 'xshift'
yvonnefroehlich Dec 22, 2023
5e519ad
Improve formulation
yvonnefroehlich Dec 22, 2023
311cca7
Combine subplots into one plot
yvonnefroehlich Dec 24, 2023
40fd4f8
Adjust thumbnail image
yvonnefroehlich Dec 24, 2023
c8f27c1
Use consistently a width of 500 kilometers
yvonnefroehlich Dec 24, 2023
60dea40
Reduce thickness of scale bar
yvonnefroehlich Dec 24, 2023
9f73a45
Merge branch 'main' into add-gallery-scalebar
yvonnefroehlich Dec 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions examples/gallery/embellishments/scalebar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
r"""
Scale bar
=========

The ``map_scale`` parameter of the :meth:`pygmt.Figure.basemap` and
:meth:`pygmt.Figure.coast` methods is used to add a scale bar to a map.
This example shows how such a scale bar can be customized:

- position: **g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**. Set the position
of the reference point. Choose from

- **g**: Give map coordinates as *longitude*\/\ *latitude*.
- **j**\|\ **J**: Specify a two-character (order independent) code.
Choose from vertical **T**\(op), **M**\(iddle), or **B**\(ottom) and
horizontal **L**\(eft), **C**\(entre), or **R**\(ight). Lower / upper
case **j** / **J** mean inside / outside of the map bounding box.
- **n**: Give normalized bounding box coordinates as *nx*\/\ *ny*.
- **x**: Give plot coordinates as *x*\/\ *y*.

- length: **+w**. Give a distance value, and, optionally a distance unit.
Choose from **e** (meters), **f** (feet), **k** (kilometers) [Default],
**M** (statute miles), **n** (nautical miles), or **u** (US survey feet).
- origin: **+c**\ [*slon*/]\ *slat*. Control where on the map the scale bar
applies. If **+c** is not given the reference point is used. If only
**+c** is appended the middle of the map is used. Note that *slon* is only
optional for projections with constant scale along parallels, e.g.,
Mercator projection.
- justify: **+j**. Set the anchor point. Specify a two-character (order
independent) code. Choose from vertical **T**\(op), **M**\(iddle), or
**B**\(ottom) and horizontal **L**\(eft), **C**\(entre), or **R**\(ight).
- offset: **+o**\ *offset* or **+o**\ *xoffset*/\ *yoffset*. Give either a
common shift or individual shifts in x (longitude) and y (latitude)
directions.
- height: Use :gmt-term:`MAP_SCALE_HEIGHT` via :func:`pygmt.config`.
- fancy style: **+f**. Get a scale bar that looks like train tracks.
- unit: **+u**. Add the distance unit given via **+w** to the single
distance values.
- label: **+l**. Add the distance unit given via **+w** as label. Append
text to get a customized label instead.
- alignment: **+a**. Set the label alignment. Choose from **t**\(op)
[Default], **b**\(ottom), **l**\(eft), or **r**\(ight).
"""

# %%
import pygmt

# Create a new Figure instance
fig = pygmt.Figure()

# Mercator projection with 10 centimeters width
fig.basemap(region=[-45, -25, -15, 0], projection="M0/0/10c", frame=["WSne", "af"])

# -----------------------------------------------------------------------------
# Top Left: Add a plain scale bar
# It is placed based on geographic coordinates (g) 42° West and 1° South,
# applies at the reference point (+c is not given), and represents a
# length (+w) of 500 kilometers
fig.basemap(map_scale="g-42/-1+w500k")

# -----------------------------------------------------------------------------
# Top Right: Add a fancy scale bar
# It is placed based on normalized bounding box coordinates (n)
# Use a fancy style (+f) to get a scale bar that looks like train tracks
# Add the distance unit (+u) to the single distance values
fig.basemap(map_scale="n0.8/0.95+w500k+f+u")

# -----------------------------------------------------------------------------
# Bottom Left: Add a thick scale bar
# Adjust the GMT default parameter MAP_SCALE_HEIGHT locally (the change applies
# only to the code within the "with" statement)
# It applies (+c) at the middle of the map (no location is appended to +c)
# Without appending text, +l adds the distance unit as label
with pygmt.config(MAP_SCALE_HEIGHT="10p"):
fig.basemap(map_scale="n0.2/0.15+c+w500k+f+l")

# -----------------------------------------------------------------------------
# Bottom Right: Add a scale bar valid for a specific location
# It is placed at BottomRight (j) using MiddleRight as anchor point (+j) with
# an offset (+o) of 1 centimeter in both x and y directions
# It applies (+c) at -7° South, add a customized label by appending text to +l
fig.basemap(map_scale="jBR+jMR+o1c/1c+c-7+w500k+f+u+lvalid at 7° S")

fig.show()


# %%
# The ``box`` parameter allows surrounding the scale bar. This can be useful
# when adding a scale bar to a colorful map. To fill the box, append **+g**
# with the desired color (or pattern). The outline of the box can be adjusted
# by appending **+p** with the desired thickness, color, and style. To force
# rounded edges append **+r** with the desired radius.

# Create a new Figure instance
fig = pygmt.Figure()

fig.coast(
region=[-45, -25, -15, 0],
projection="M10c",
land="tan",
water="steelblue",
frame=["WSne", "af"],
# Set the label alignment (+a) to right (r)
map_scale="jBL+o1c/1c+c-7+w500k+f+lkm+ar",
# Fill the box in white with a transparency of 30 percent, add a solid
# outline in darkgray (gray30) with a thickness of 0.5 points, and use
# rounded edges with a radius of 3 points
box="+gwhite@30+p0.5p,gray30,solid+r3p",
)

fig.show()

# sphinx_gallery_thumbnail_number = 1