Skip to content

Commit 8cdf6a1

Browse files
committed
Allow passing str type into angle argument of text
So that we can set angle=True or angle="", and users can parse the angle from a textfile (in GMT it will be `-F+a`. Added a test case for this, and ensure that angle/font/justify=True (i.e. setting a boolean type) works.
1 parent 0267dd1 commit 8cdf6a1

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

pygmt/base_plotting.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -855,23 +855,25 @@ def text(
855855
the text
856856
text : str or 1d array
857857
The text string, or an array of strings to plot on the figure
858-
angle: int, float or bool
858+
angle: int, float, str or bool
859859
Set the angle measured in degrees counter-clockwise from
860860
horizontal. E.g. 30 sets the text at 30 degrees. If no angle is
861-
given then the input textfile(s) must have this as a column.
861+
explicitly given (i.e. angle=True) then the input textfile(s) must
862+
have this as a column.
862863
font : str or bool
863864
Set the font specification with format "size,font,color" where size
864865
is text size in points, font is the font to use, and color sets the
865866
font color. E.g. "12p,Helvetica-Bold,red" selects a 12p red
866-
Helvetica-Bold font. If no font info is given then the input
867-
textfile(s) must have this information in one of its columns.
867+
Helvetica-Bold font. If no font info is explicitly given (i.e.
868+
font=True), then the input textfile(s) must have this information
869+
in one of its columns.
868870
justify: str or bool
869871
Set the alignment which refers to the part of the text string that
870872
will be mapped onto the (x,y) point. Choose a 2 character
871873
combination of L, C, R (for left, center, or right) and T, M, B for
872874
top, middle, or bottom. E.g., BL for lower left. If no
873-
justification is given then the input textfile(s) must have this as
874-
a column.
875+
justification is explicitly given (i.e. justify=True), then the
876+
input textfile(s) must have this as a column.
875877
{J}
876878
{R}
877879
"""
@@ -888,7 +890,7 @@ def text(
888890
if angle is not None or font is not None or justify is not None:
889891
if "F" not in kwargs.keys():
890892
kwargs.update({"F": ""})
891-
if angle is not None and isinstance(angle, (int, float)):
893+
if angle is not None and isinstance(angle, (int, float, str)):
892894
kwargs["F"] += f"+a{str(angle)}"
893895
if font is not None and isinstance(font, str):
894896
kwargs["F"] += f"+f{font}"

pygmt/tests/test_text.py

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from .. import Figure
1010
from ..exceptions import GMTInvalidInput
11+
from ..helpers import GMTTempFile
1112

1213
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1314
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
@@ -172,3 +173,24 @@ def test_text_justify_parsed_from_textfile():
172173
D="j0.45/0+vred", # draw red-line from xy point to text label (city name)
173174
)
174175
return fig
176+
177+
178+
def test_text_angle_font_justify_from_textfile(region, projection):
179+
"""
180+
Print text with x, y, angle, font, justify, and text arguments parsed from
181+
the textfile.
182+
"""
183+
fig = Figure()
184+
with GMTTempFile(suffix=".txt") as tempfile:
185+
with open(tempfile.name, "w") as tmpfile:
186+
tmpfile.write("114 0.5 30 22p,Helvetica-Bold,black LM BORNEO")
187+
fig.text(
188+
region=[108, 120, -5, 8],
189+
projection="M20c",
190+
frame="a",
191+
textfiles=tempfile.name,
192+
angle=True,
193+
font=True,
194+
justify=True,
195+
)
196+
fig.show()

0 commit comments

Comments
 (0)