diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 04ba2c48fec..528dad338d9 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -39,9 +39,6 @@ @kwargs_to_strings( R="sequence", textfiles="sequence_space", - angle="sequence_comma", - font="sequence_comma", - justify="sequence_comma", c="sequence_comma", p="sequence", ) @@ -97,12 +94,12 @@ def text_( of the map. text : str or 1-D array The text string, or an array of strings to plot on the figure. - angle: float, str, or bool + angle: float, str, bool or list Set the angle measured in degrees counter-clockwise from horizontal (e.g. 30 sets the text at 30 degrees). If no angle is explicitly given (i.e. ``angle=True``) then the input to ``textfiles`` must have this as a column. - font : str or bool + font : str, bool or list of str Set the font specification with format *size*\ ,\ *font*\ ,\ *color* where *size* is text size in points, *font* is the font to use, and *color* sets the font color. For example, @@ -110,7 +107,7 @@ def text_( font. If no font info is explicitly given (i.e. ``font=True``), then the input to ``textfiles`` must have this information in one of its columns. - justify : str or bool + justify : str, bool or list of str Set the alignment which refers to the part of the text string that will be mapped onto the (x, y) point. Choose a two-letter combination of **L**, **C**, **R** (for left, center, or right) and @@ -170,7 +167,7 @@ def text_( ``x``/``y`` and ``text``. {wrap} """ - # pylint: disable=too-many-branches + # pylint: disable=too-many-branches,too-many-locals kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access # Ensure inputs are either textfiles, x/y/text, or position/text @@ -201,25 +198,22 @@ def text_( ): kwargs.update({"F": ""}) - if angle is True: - kwargs["F"] += "+a" - elif isinstance(angle, (int, float, str)): - kwargs["F"] += f"+a{str(angle)}" - - if font is True: - kwargs["F"] += "+f" - elif isinstance(font, str): - kwargs["F"] += f"+f{font}" - - if justify is True: - kwargs["F"] += "+j" - elif isinstance(justify, str): - kwargs["F"] += f"+j{justify}" + extra_arrays = [] + for arg, flag in [(angle, "+a"), (font, "+f"), (justify, "+j")]: + if arg is True: + kwargs["F"] += flag + elif is_nonstr_iter(arg): + kwargs["F"] += flag + if flag == "+a": # angle is numeric type + extra_arrays.append(np.atleast_1d(arg)) + else: # font or justify is str type + extra_arrays.append(np.atleast_1d(arg).astype(str)) + elif isinstance(arg, (int, float, str)): + kwargs["F"] += f"{flag}{arg}" if isinstance(position, str): kwargs["F"] += f"+c{position}+t{text}" - extra_arrays = [] # If an array of transparency is given, GMT will read it from # the last numerical column per data record. if kwargs.get("t") is not None and is_nonstr_iter(kwargs["t"]): diff --git a/pygmt/tests/baseline/test_text_angle_justify_font_arrays.png.dvc b/pygmt/tests/baseline/test_text_angle_justify_font_arrays.png.dvc new file mode 100644 index 00000000000..2b23888fe67 --- /dev/null +++ b/pygmt/tests/baseline/test_text_angle_justify_font_arrays.png.dvc @@ -0,0 +1,5 @@ +outs: +- md5: c111539b0b5966eee4305f9485c85bd0 + size: 8208 + hash: md5 + path: test_text_angle_justify_font_arrays.png diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 9a8b1cf6663..0b79c2fde15 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -311,6 +311,42 @@ def test_text_angle_font_justify_from_textfile(): return fig +@pytest.mark.mpl_image_compare(filename="test_text_position.png") +def test_text_justify_array(region): + """ + Test passing an array of justify codes. + + Re-use the baseline image from test_text_position(). + """ + fig = Figure() + fig.basemap(region=region, projection="x1c", frame="a") + fig.text( + x=[0, 2.5, 5.0, 0, 2.5, 5.0, 0, 2.5, 5.0], + y=[0, 0, 0, 1.25, 1.25, 1.25, 2.5, 2.5, 2.5], + justify=["BL", "BC", "BR", "ML", "MC", "MR", "TL", "TC", "TR"], + text=["BL", "BC", "BR", "ML", "C M", "MR", "TL", "TC", "TR"], + ) + return fig + + +@pytest.mark.mpl_image_compare +def test_text_angle_justify_font_arrays(region): + """ + Test passing arrays of angle, justify and font. + """ + fig = Figure() + fig.basemap(region=region, projection="X5c/2.5c", frame=True) + fig.text( + x=[2.5, 2.5], + y=[1.0, 2.0], + angle=[30, 50], + justify=["TL", "BR"], + font=["15p,Helvetica-Bold,red", "5p,Times-Italic,blue"], + text=["TEXT1", "TEXT2 with spaces"], + ) + return fig + + @pytest.mark.mpl_image_compare def test_text_transparency(): """