Skip to content

Commit b0fbf38

Browse files
authored
Figure.colorbar: Add parameters label/unit/annot/tick/grid and more to set colorbar annotations/labels (#4407)
1 parent 8ed4684 commit b0fbf38

File tree

1 file changed

+105
-12
lines changed

1 file changed

+105
-12
lines changed

pygmt/src/colorbar.py

Lines changed: 105 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,77 @@
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 | bool = False,
22+
tick: float | bool = False,
23+
grid: float | bool = False,
24+
annot_angle: 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+
>>> list(_build_frame(annot=1, tick=0.5, label="Distance", unit="km"))
35+
['xa1f0.5+lDistance', 'y+lkm']
36+
37+
>>> list(
38+
... _build_frame(
39+
... annot=1,
40+
... tick=0.5,
41+
... grid=0.2,
42+
... annot_angle=30,
43+
... label="Distance",
44+
... unit="km",
45+
... )
46+
... )
47+
['xa1f0.5g0.2+lDistance+a30', 'y+lkm']
48+
>>> list(_build_frame(frame=["xaf0.5+lDistance", "y+lkm"]))
49+
['xaf0.5+lDistance', 'y+lkm']
50+
51+
>>> _build_frame(frame="none")
52+
'none'
53+
>>> _build_frame() # Passing no parameters returns None
54+
"""
55+
# Using the old 'frame' parameter.
56+
if frame is not None and frame is not False:
57+
return frame
58+
59+
_xaxis_is_set = any(
60+
v is not None and v is not False
61+
for v in {annot, tick, grid, annot_angle, annot_prefix, annot_unit, label}
62+
)
63+
_yaxis_is_set = unit is not None
64+
65+
# Need to return None if no parameters are give. Otherwise, it may return "".
66+
if not (_xaxis_is_set or _yaxis_is_set):
67+
return None
68+
69+
xaxis, yaxis = None, None
70+
if _xaxis_is_set:
71+
xaxis = Axis(
72+
annot=annot,
73+
tick=tick,
74+
grid=grid,
75+
angle=annot_angle,
76+
prefix=annot_prefix,
77+
unit=annot_unit,
78+
label=label,
79+
)
80+
if _yaxis_is_set:
81+
yaxis = Axis(label=unit)
82+
return Frame(xaxis=xaxis, yaxis=yaxis)
83+
84+
2085
def _alias_option_D( # noqa: N802, PLR0913
2186
position=None,
2287
length=None,
@@ -153,6 +218,14 @@ def colorbar( # noqa: PLR0913
153218
length: float | str | None = None,
154219
width: float | str | None = None,
155220
orientation: Literal["horizontal", "vertical"] | None = None,
221+
label: str | None = None,
222+
unit: str | None = None,
223+
annot: float | bool = False,
224+
tick: float | bool = False,
225+
grid: float | bool = False,
226+
annot_angle: float | None = None,
227+
annot_prefix: str | None = None,
228+
annot_unit: str | None = None,
156229
reverse: bool = False,
157230
nan: bool = False,
158231
nan_position: Literal["start", "end"] | None = None,
@@ -201,7 +274,6 @@ def colorbar( # noqa: PLR0913
201274
Full GMT docs at :gmt-docs:`colorbar.html`.
202275
203276
$aliases
204-
- B = frame
205277
- F = box
206278
- G = truncate
207279
- I = shading
@@ -217,6 +289,7 @@ def colorbar( # noqa: PLR0913
217289
.. hlist::
218290
:columns: 1
219291
292+
- B = label, unit, annot, tick, grid, annot_angle, annot_prefix, annot_unit
220293
- D = position, **+w**: length/width, **+h**/**+v**: orientation,
221294
**+r**: reverse, **+n**: nan/nan_position,
222295
**+e**: fg_triangle/bg_triangle/triangle_height,
@@ -244,6 +317,24 @@ def colorbar( # noqa: PLR0913
244317
given with unit ``%`` then it is in percentage of the bar length. [Length
245318
defaults to 80% of the corresponding plot side dimension, and width defaults
246319
to 4% of the bar length].
320+
label
321+
unit
322+
Set the label and unit for the colorbar. The label is placed along the colorbar
323+
and the unit is placed at the end of the colorbar.
324+
annot
325+
tick
326+
grid
327+
Intervals for annotations, ticks, and gridlines. Refer to
328+
:class:`pygmt.params.Axis` for more details on how these parameters work.
329+
annot_prefix
330+
annot_unit
331+
annot_angle
332+
The prefix, unit and angle for the annotations. The prefix is placed before the
333+
annotation text; the unit is placed after the annotation text; and the angle is
334+
the angle of the annotation text.
335+
frame
336+
Set colorbar boundary frame, labels, and axes attributes. If set to ``"none"``,
337+
then no frame will be drawn.
247338
orientation
248339
Set the colorbar orientation to either ``"horizontal"`` or ``"vertical"``.
249340
[Default is vertical, unless ``position`` is set to bottom-center or top-center
@@ -321,9 +412,6 @@ def colorbar( # noqa: PLR0913
321412
requested colorbar length.
322413
$projection
323414
$region
324-
frame
325-
Set colorbar boundary frame, labels, and axes attributes. If set to ``"none"``,
326-
then no frame will be drawn.
327415
$verbose
328416
$panel
329417
$perspective
@@ -337,12 +425,7 @@ def colorbar( # noqa: PLR0913
337425
>>> # Create a basemap
338426
>>> fig.basemap(region=[0, 10, 0, 3], projection="X10c/3c", frame=True)
339427
>>> # Call the colorbar method for the plot
340-
>>> fig.colorbar(
341-
... # Set cmap to the "roma" CPT
342-
... cmap="SCM/roma",
343-
... # Label the x-axis "Velocity" and the y-axis "m/s"
344-
... frame=["x+lVelocity", "y+lm/s"],
345-
... )
428+
>>> fig.colorbar(cmap="SCM/roma", label="Velocity", unit="m/s")
346429
>>> # Show the plot
347430
>>> fig.show()
348431
"""
@@ -387,7 +470,17 @@ def colorbar( # noqa: PLR0913
387470
Q=Alias(log, name="log"),
388471
W=Alias(scale, name="scale"),
389472
).add_common(
390-
B=frame,
473+
B=_build_frame(
474+
annot=annot,
475+
tick=tick,
476+
grid=grid,
477+
annot_angle=annot_angle,
478+
annot_prefix=annot_prefix,
479+
annot_unit=annot_unit,
480+
label=label,
481+
unit=unit,
482+
frame=frame,
483+
),
391484
J=projection,
392485
R=region,
393486
V=verbose,

0 commit comments

Comments
 (0)