diff --git a/pygmt/src/colorbar.py b/pygmt/src/colorbar.py index c85dcb0ce0f..1f05f4b81b0 100644 --- a/pygmt/src/colorbar.py +++ b/pygmt/src/colorbar.py @@ -25,7 +25,7 @@ @kwargs_to_strings( R="sequence", G="sequence", I="sequence", c="sequence_comma", p="sequence" ) -def colorbar(self, **kwargs): +def colorbar(self, frame=None, annotation=None, xlabel=None, ylabel=None, **kwargs): r""" Plot a gray or color scale-bar on maps. @@ -45,6 +45,12 @@ def colorbar(self, **kwargs): ---------- frame : str or list Set color bar boundary frame, labels, and axes attributes. + annotation : int or float or str + Set the interval for annotated gridlines on the colorbar. + xlabel : str + Set the label for the x-axis of the colorbar. + ylabel : str + Set the label for the y-axis of the colorbar. {cmap} position : str [**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\ @@ -102,6 +108,21 @@ def colorbar(self, **kwargs): {perspective} {transparency} """ + if xlabel or ylabel or annotation: + if kwargs.get("B") is None: + frame = [] + elif isinstance(kwargs.get("B"), list): + frame = kwargs.get("B") + else: + frame = [kwargs.get("B")] + if xlabel: + frame.append("x+l" + str(xlabel)) + if ylabel: + frame.append("y+l" + str(ylabel)) + if annotation: + frame.append("a" + str(annotation)) + if frame: + kwargs["B"] = frame kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access with Session() as lib: lib.call_module(module="colorbar", args=build_arg_string(kwargs)) diff --git a/pygmt/tests/baseline/test_colorbar_frame_list_parameters.png.dvc b/pygmt/tests/baseline/test_colorbar_frame_list_parameters.png.dvc new file mode 100644 index 00000000000..656c084917a --- /dev/null +++ b/pygmt/tests/baseline/test_colorbar_frame_list_parameters.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 69740a2512fd502e3d53d16bddc0f97c + size: 4878 + path: test_colorbar_frame_list_parameters.png diff --git a/pygmt/tests/baseline/test_colorbar_frame_parameters.png.dvc b/pygmt/tests/baseline/test_colorbar_frame_parameters.png.dvc new file mode 100644 index 00000000000..4bbd05d5a54 --- /dev/null +++ b/pygmt/tests/baseline/test_colorbar_frame_parameters.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: b7fc503b09c1c610a22081edf9cc612f + size: 4840 + path: test_colorbar_frame_parameters.png diff --git a/pygmt/tests/baseline/test_colorbar_frame_string_parameters.png.dvc b/pygmt/tests/baseline/test_colorbar_frame_string_parameters.png.dvc new file mode 100644 index 00000000000..a397b94cb02 --- /dev/null +++ b/pygmt/tests/baseline/test_colorbar_frame_string_parameters.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 7a8bbd25a40214160f00a5c5bf83f69d + size: 3764 + path: test_colorbar_frame_string_parameters.png diff --git a/pygmt/tests/test_colorbar.py b/pygmt/tests/test_colorbar.py index 7b58ad26381..53620fc30e5 100644 --- a/pygmt/tests/test_colorbar.py +++ b/pygmt/tests/test_colorbar.py @@ -65,3 +65,53 @@ def test_colorbar_shading_list(): fig.basemap(region=[0, 10, 0, 10], projection="X15c", frame="a") fig.colorbar(cmap="geo", shading=[-0.7, 0.2], frame=True) return fig + + +@pytest.mark.mpl_image_compare +def test_colorbar_frame_parameters(): + """ + Create colorbar with arguments for xlabel, ylabel, and annotation, and no + argument passed to frame. + """ + fig = Figure() + fig.colorbar( + cmap="rainbow", + position="x0c/0c+w1c/0.5c", + xlabel="test-x", + ylabel="test-y", + annotation=0.25, + ) + return fig + + +@pytest.mark.mpl_image_compare +def test_colorbar_frame_list_parameters(): + """ + Create colorbar with a list passed to frame and an argument passed to + xlabel. + """ + fig = Figure() + fig.colorbar( + cmap="rainbow", + position="x0c/0c+w1c/0.5c", + frame=["a.25", "y+lylabel"], + xlabel="test-x", + ) + return fig + + +@pytest.mark.mpl_image_compare +def test_colorbar_frame_string_parameters(): + """ + Create colorbar with a string passed to frame and arguments passed to + xlabel and ylabel. + """ + fig = Figure() + fig.colorbar( + cmap="rainbow", + position="x0c/0c+w1c/0.5c", + frame="a0.5", + xlabel="test-x", + ylabel="test-y", + ) + return fig