Skip to content

Commit 64ef1d7

Browse files
committed
Figure.colorbar: Pythonic way to set labels and annotations
1 parent 3a33364 commit 64ef1d7

1 file changed

Lines changed: 89 additions & 12 deletions

File tree

pygmt/src/colorbar.py

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,58 @@
1111
from pygmt.exceptions import GMTValueError
1212
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias
1313
from pygmt.helpers.utils import is_nonstr_iter
14-
from pygmt.params import Box, Position
14+
from pygmt.params import Axis, Box, Frame, Position
1515
from pygmt.src._common import _parse_position
1616

1717
__doctest_skip__ = ["colorbar"]
1818

1919

20+
def _build_frame(
21+
annot: float | None = None,
22+
tick: float | None = None,
23+
grid: float | None = None,
24+
annot_angel: float | None = None,
25+
annot_prefix: str | None = None,
26+
annot_unit: str | None = None,
27+
label: str | None = None,
28+
unit: str | None = None,
29+
frame=None,
30+
):
31+
"""
32+
Create the list of Alias objects for the -B option.
33+
34+
>>> _build_frame(annot=1, tick=0.5, xlabel="Distance", ylabel="Depth")
35+
Frame(xaxis=Axis(annot=1, tick=0.5, label='Distance'), yaxis=Axis(label='Depth'))
36+
"""
37+
if frame is not None and frame is not False:
38+
return frame
39+
40+
_xaxis_is_set = any(
41+
v is not None
42+
for v in {annot, tick, grid, annot_angel, annot_prefix, annot_unit, label}
43+
)
44+
_yaxis_is_set = unit is not None
45+
46+
# Need to return None if no parameters are give. Otherwise, it may return "".
47+
if not (_xaxis_is_set or _yaxis_is_set):
48+
return None
49+
50+
xaxis, yaxis = None, None
51+
if _xaxis_is_set:
52+
xaxis = Axis(
53+
annot=annot,
54+
tick=tick,
55+
grid=grid,
56+
# angle=annot_angel,
57+
# prefix=annot_prefix,
58+
# unit=annot_unit,
59+
label=label,
60+
)
61+
if _yaxis_is_set:
62+
yaxis = Axis(label=unit)
63+
return Frame(xaxis=xaxis, yaxis=yaxis)
64+
65+
2066
def _alias_option_D( # noqa: N802, PLR0913
2167
position=None,
2268
length=None,
@@ -153,6 +199,15 @@ def colorbar( # noqa: PLR0913
153199
length: float | str | None = None,
154200
width: float | str | None = None,
155201
orientation: Literal["horizontal", "vertical"] | None = None,
202+
label: str | None = None,
203+
unit: str | None = None,
204+
annot: float | None = None,
205+
tick: float | None = None,
206+
grid: float | None = None,
207+
annot_angel: float | None = None,
208+
annot_prefix: str | None = None,
209+
annot_unit: str | None = None,
210+
frame: str | Sequence[str] | bool = False,
156211
reverse: bool = False,
157212
nan: bool = False,
158213
nan_position: Literal["start", "end"] | None = None,
@@ -168,7 +223,6 @@ def colorbar( # noqa: PLR0913
168223
scale: float | None = None,
169224
projection: str | None = None,
170225
region: Sequence[float | str] | str | None = None,
171-
frame: str | Sequence[str] | bool = False,
172226
verbose: Literal["quiet", "error", "warning", "timing", "info", "compat", "debug"]
173227
| bool = False,
174228
panel: int | Sequence[int] | bool = False,
@@ -201,7 +255,6 @@ def colorbar( # noqa: PLR0913
201255
Full GMT docs at :gmt-docs:`colorbar.html`.
202256
203257
$aliases
204-
- B = frame
205258
- F = box
206259
- G = truncate
207260
- I = shading
@@ -217,6 +270,7 @@ def colorbar( # noqa: PLR0913
217270
.. hlist::
218271
:columns: 1
219272
273+
- B = label, unit, annot, tick, grid, annot_angel, annot_prefix, annot_unit
220274
- D = position, **+w**: length/width, **+h**/**+v**: orientation,
221275
**+r**: reverse, **+n**: nan/nan_position,
222276
**+e**: fg_triangle/bg_triangle/triangle_height,
@@ -244,6 +298,26 @@ def colorbar( # noqa: PLR0913
244298
given with unit ``%`` then it is in percentage of the bar length. [Length
245299
defaults to 80% of the corresponding plot side dimension, and width defaults
246300
to 4% of the bar length].
301+
label
302+
unit
303+
Set the label and unit for the colorbar. The label is placed along the colorbar
304+
and the unit is placed at the end of the colorbar.
305+
annot
306+
grid
307+
tick
308+
Intervals for annotations, grid lines, and ticks. Refer to
309+
:class:`pygmt.params.Axis` for more details on how these parameters work.
310+
Parameters ``annot_prefix``, ``annot_unit``, and ``annot_angel`` can be used to
311+
further customize the annotations.
312+
frame
313+
Set colorbar boundary frame, labels, and axes attributes.
314+
315+
.. deprecated:: v0.19.0
316+
317+
Use ``annot``, ``tick``, ``grid``, ``annot_angel``, ``annot_prefix``,
318+
``annot_unit``, ``label``, and ``unit`` parameters to customize the colorbar
319+
annotations and labels.
320+
247321
orientation
248322
Set the colorbar orientation to either ``"horizontal"`` or ``"vertical"``.
249323
[Default is vertical, unless ``position`` is set to bottom-center or top-center
@@ -321,8 +395,6 @@ def colorbar( # noqa: PLR0913
321395
requested colorbar length.
322396
$projection
323397
$region
324-
frame
325-
Set colorbar boundary frame, labels, and axes attributes.
326398
$verbose
327399
$panel
328400
$perspective
@@ -336,12 +408,7 @@ def colorbar( # noqa: PLR0913
336408
>>> # Create a basemap
337409
>>> fig.basemap(region=[0, 10, 0, 3], projection="X10c/3c", frame=True)
338410
>>> # Call the colorbar method for the plot
339-
>>> fig.colorbar(
340-
... # Set cmap to the "roma" CPT
341-
... cmap="SCM/roma",
342-
... # Label the x-axis "Velocity" and the y-axis "m/s"
343-
... frame=["x+lVelocity", "y+lm/s"],
344-
... )
411+
>>> fig.colorbar(cmap="SCM/roma", label="Velocity", unit="m/s")
345412
>>> # Show the plot
346413
>>> fig.show()
347414
"""
@@ -386,7 +453,17 @@ def colorbar( # noqa: PLR0913
386453
Q=Alias(log, name="log"),
387454
W=Alias(scale, name="scale"),
388455
).add_common(
389-
B=frame,
456+
B=_build_frame(
457+
annot=annot,
458+
tick=tick,
459+
grid=grid,
460+
annot_angel=annot_angel,
461+
annot_prefix=annot_prefix,
462+
annot_unit=annot_unit,
463+
label=label,
464+
unit=unit,
465+
frame=frame,
466+
),
390467
J=projection,
391468
R=region,
392469
V=verbose,

0 commit comments

Comments
 (0)