Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figure.text: Support passing in a list of angle/font/justify values #2720

Merged
merged 12 commits into from
Oct 17, 2023
38 changes: 16 additions & 22 deletions pygmt/src/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
@kwargs_to_strings(
R="sequence",
textfiles="sequence_space",
angle="sequence_comma",
font="sequence_comma",
justify="sequence_comma",
Comment on lines -42 to -44
Copy link
Member Author

@seisman seisman Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines were initially added in #321, but they should NOT be like this.

c="sequence_comma",
p="sequence",
)
Expand Down Expand Up @@ -97,20 +94,20 @@ 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: int, float, str or bool
angle: int, 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,
``font="12p,Helvetica-Bold,red"`` selects a 12p, red, Helvetica-Bold
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
outs:
- md5: c111539b0b5966eee4305f9485c85bd0
size: 8208
hash: md5
path: test_text_angle_justify_font_arrays.png
36 changes: 36 additions & 0 deletions pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
michaelgrund marked this conversation as resolved.
Show resolved Hide resolved
)
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():
"""
Expand Down