Skip to content

Commit 87a2407

Browse files
committed
Merge branch 'main' into class/frame-part1
2 parents d8eabe7 + e90c9cd commit 87a2407

3 files changed

Lines changed: 77 additions & 97 deletions

File tree

examples/gallery/embellishments/scalebar.py

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,84 @@
1-
r"""
1+
"""
22
Scale bar
33
=========
44
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.
417
"""
428

439
# %%
4410
import pygmt
45-
from pygmt.params import Box
11+
from pygmt.params import Box, Position
4612

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

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

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+
)
6634

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+
)
7547

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+
)
8260

8361
fig.show()
8462

8563

8664
# %%
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.
9267

93-
# Create a new Figure instance
9468
fig = pygmt.Figure()
9569

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
10782
box=Box(fill="white@30", pen="0.5p,gray30,solid", radius="3p"),
10883
)
10984

examples/tutorials/advanced/subplots.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@
229229

230230
# %%
231231
# You can also manually override the ``tag`` for each subplot using for example,
232-
# ``fig.set_panel(..., fixedlabel="b) Panel 2")`` which would allow you to manually tag
233-
# a single subplot as you wish. This can be useful for adding a more descriptive
234-
# subtitle to individual subplots.
232+
# ``fig.set_panel(..., tag="b) Panel 2")`` which would allow you to manually tag a
233+
# single subplot as you wish. This can be useful for adding a more descriptive subtitle
234+
# to individual subplots.
235235

236236
# sphinx_gallery_thumbnail_number = 3

pygmt/src/subplot.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
from pygmt.alias import Alias, AliasSystem
1111
from pygmt.clib import Session
1212
from pygmt.exceptions import GMTParameterError, GMTValueError
13-
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
13+
from pygmt.helpers import (
14+
build_arg_list,
15+
deprecate_parameter,
16+
fmt_docstring,
17+
kwargs_to_strings,
18+
use_alias,
19+
)
1420
from pygmt.params import Box, Position
1521
from pygmt.src._common import _parse_position
1622

@@ -352,10 +358,13 @@ def subplot( # noqa: PLR0913
352358

353359
@fmt_docstring
354360
@contextlib.contextmanager
355-
@use_alias(A="fixedlabel", C="clearance")
361+
# TODO(PyGMT>=0.23.0): Remove the deprecated 'fixedlabel' parameter.
362+
@deprecate_parameter("fixedlabel", "tag", "v0.19.0", remove_version="v0.23.0")
363+
@use_alias(C="clearance")
356364
def set_panel(
357365
self,
358366
panel: int | Sequence[int] | None = None,
367+
tag: str | None = None,
359368
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
360369
| bool = False,
361370
**kwargs,
@@ -372,6 +381,7 @@ def set_panel(
372381
``projection="X"`` will fill the subplot by using unequal scales].
373382
374383
$aliases
384+
- A = tag
375385
- V = verbose
376386
377387
Parameters
@@ -386,13 +396,10 @@ def set_panel(
386396
GMT maintains information about the current figure and subplot. Also, you may
387397
give the one-dimensional *index* instead which starts at 0 and follows the row
388398
or column order set via ``autolabel`` in :meth:`pygmt.Figure.subplot`.
389-
390-
fixedlabel : str
391-
Overrides the automatic labeling with the given string. No modifiers
392-
are allowed. Placement, justification, etc. are all inherited from how
393-
``autolabel`` was specified by the initial :meth:`pygmt.Figure.subplot`
394-
command.
395-
399+
tag
400+
Tag for the current subplot. It overrides the automatic tag set by the
401+
:meth:`pygmt.Figure.subplot` method. Use ``tag="-"`` to skip the tag for this
402+
panel.
396403
clearance : str or list
397404
[*side*]\ *clearance*.
398405
Reserve a space of dimension *clearance* between the margin and the
@@ -409,9 +416,7 @@ def set_panel(
409416
"""
410417
self._activate_figure()
411418

412-
aliasdict = AliasSystem().add_common(
413-
V=verbose,
414-
)
419+
aliasdict = AliasSystem(A=Alias(tag, name="tag")).add_common(V=verbose)
415420
aliasdict.merge(kwargs)
416421

417422
with Session() as lib:

0 commit comments

Comments
 (0)