From 42208f07eb1f8d2579dfd156a8e6bc64a52e5b63 Mon Sep 17 00:00:00 2001 From: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Date: Wed, 5 Oct 2022 17:45:16 +0200 Subject: [PATCH] Figure.ternary: Add parameters alabel, blabel, and clabel (#2139) Co-authored-by: Dongdong Tian Co-authored-by: actions-bot <58130806+actions-bot@users.noreply.github.com> --- pygmt/src/ternary.py | 17 +++++++- .../baseline/test_ternary_1_label.png.dvc | 4 ++ .../baseline/test_ternary_3_labels.png.dvc | 4 ++ pygmt/tests/test_ternary.py | 40 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 pygmt/tests/baseline/test_ternary_1_label.png.dvc create mode 100644 pygmt/tests/baseline/test_ternary_3_labels.png.dvc diff --git a/pygmt/src/ternary.py b/pygmt/src/ternary.py index cf538d8a7b7..b357289fe4c 100644 --- a/pygmt/src/ternary.py +++ b/pygmt/src/ternary.py @@ -23,7 +23,7 @@ t="transparency", ) @kwargs_to_strings(R="sequence", c="sequence_comma", p="sequence") -def ternary(self, data, **kwargs): +def ternary(self, data, alabel=None, blabel=None, clabel=None, **kwargs): r""" Reads (*a*,\ *b*,\ *c*\ [,\ *z*]) records from *data* and plots symbols at those locations on a ternary diagram. If a symbol is selected and no symbol @@ -54,6 +54,14 @@ def ternary(self, data, **kwargs): and **c**. {cmap} {fill} + alabel : str + Set the label for the *a* vertex where the component is 100%. The + label is placed at a distance of three times the + :gmt-term:`MAP_LABEL_OFFSET` setting from the corner. + blabel : str + Set the label for the *b* vertex where the component is 100%. + clabel : str + Set the label for the *c* vertex where the component is 100%. style : str *symbol*\[\ *size*]. Plot individual symbols in a ternary diagram. @@ -66,6 +74,13 @@ def ternary(self, data, **kwargs): {transparency} """ kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access + + if alabel or blabel or clabel: + alabel = str(alabel) if alabel is not None else "-" + blabel = str(blabel) if blabel is not None else "-" + clabel = str(clabel) if clabel is not None else "-" + kwargs["L"] = "/".join([alabel, blabel, clabel]) + with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="vector", data=data) with file_context as infile: diff --git a/pygmt/tests/baseline/test_ternary_1_label.png.dvc b/pygmt/tests/baseline/test_ternary_1_label.png.dvc new file mode 100644 index 00000000000..fe585f35047 --- /dev/null +++ b/pygmt/tests/baseline/test_ternary_1_label.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 7278b6cdaf0076840927380a22d03846 + size: 63228 + path: test_ternary_1_label.png diff --git a/pygmt/tests/baseline/test_ternary_3_labels.png.dvc b/pygmt/tests/baseline/test_ternary_3_labels.png.dvc new file mode 100644 index 00000000000..53101403e80 --- /dev/null +++ b/pygmt/tests/baseline/test_ternary_3_labels.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: e303384766b3b5e31cad90a9b5e61136 + size: 64570 + path: test_ternary_3_labels.png diff --git a/pygmt/tests/test_ternary.py b/pygmt/tests/test_ternary.py index 986ca64c2d9..eb3a704e016 100644 --- a/pygmt/tests/test_ternary.py +++ b/pygmt/tests/test_ternary.py @@ -62,3 +62,43 @@ def test_ternary(array): pen="thinnest", ) return fig + + +@pytest.mark.mpl_image_compare +def test_ternary_3_labels(array): + """ + Test plotting a ternary chart with 3 labels. + """ + fig = Figure() + fig.ternary( + data=array, + region=[0, 100, 0, 100, 0, 100], + cmap="red,orange,yellow,green,blue,violet", + width="10c", + alabel="A", + blabel="B", + clabel="C", + frame=["bafg+lAir", "cafg+lLimestone", "aafg+lWater"], + style="c0.1c", + pen="thinnest", + ) + return fig + + +@pytest.mark.mpl_image_compare +def test_ternary_1_label(array): + """ + Test plotting a ternary chart with 1 label. + """ + fig = Figure() + fig.ternary( + data=array, + region=[0, 100, 0, 100, 0, 100], + cmap="red,orange,yellow,green,blue,violet", + width="10c", + alabel="A", + frame=["bafg+lAir", "cafg+lLimestone", "aafg+lWater"], + style="c0.1c", + pen="thinnest", + ) + return fig