Skip to content

Commit bc05436

Browse files
committed
Figure.colorbar: Pythonic way to set labels and annotations
1 parent d8eabe7 commit bc05436

1 file changed

Lines changed: 68 additions & 5 deletions

File tree

pygmt/src/colorbar.py

Lines changed: 68 additions & 5 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+
xlabel: str | None = None,
28+
ylabel: 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, xlabel}
43+
)
44+
_yaxis_is_set = ylabel 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=xlabel,
60+
)
61+
if _yaxis_is_set:
62+
yaxis = Axis(label=ylabel)
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,14 @@ def colorbar( # noqa: PLR0913
153199
length: float | str | None = None,
154200
width: float | str | None = None,
155201
orientation: Literal["horizontal", "vertical"] | None = None,
202+
annot: float | None = None,
203+
tick: float | None = None,
204+
grid: float | None = None,
205+
annot_angel: float | None = None,
206+
annot_prefix: str | None = None,
207+
annot_unit: str | None = None,
208+
xlabel: str | None = None,
209+
ylabel: str | None = None,
156210
reverse: bool = False,
157211
nan: bool = False,
158212
nan_position: Literal["start", "end"] | None = None,
@@ -337,10 +391,9 @@ def colorbar( # noqa: PLR0913
337391
>>> fig.basemap(region=[0, 10, 0, 3], projection="X10c/3c", frame=True)
338392
>>> # Call the colorbar method for the plot
339393
>>> fig.colorbar(
340-
... # Set cmap to the "roma" CPT
341394
... cmap="SCM/roma",
342-
... # Label the x-axis "Velocity" and the y-axis "m/s"
343-
... frame=["x+lVelocity", "y+lm/s"],
395+
... xlabel="Velocity",
396+
... ylabel="m/s",
344397
... )
345398
>>> # Show the plot
346399
>>> fig.show()
@@ -386,7 +439,17 @@ def colorbar( # noqa: PLR0913
386439
Q=Alias(log, name="log"),
387440
W=Alias(scale, name="scale"),
388441
).add_common(
389-
B=frame,
442+
B=_build_frame(
443+
annot=annot,
444+
tick=tick,
445+
grid=grid,
446+
annot_angel=annot_angel,
447+
annot_prefix=annot_prefix,
448+
annot_unit=annot_unit,
449+
xlabel=xlabel,
450+
ylabel=ylabel,
451+
frame=frame,
452+
),
390453
J=projection,
391454
R=region,
392455
V=verbose,

0 commit comments

Comments
 (0)