Skip to content

Commit 2128dd4

Browse files
authored
Fix the bug for Figure.text() when "text" is a non-string array (#724)
In `Figure.text()`, the `text` array must be in string type. Closes #706.
1 parent 2a88f73 commit 2128dd4

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pygmt/base_plotting.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,8 @@ def text(
15361536
np.atleast_1d(x),
15371537
np.atleast_1d(y),
15381538
*extra_arrays,
1539-
np.atleast_1d(text),
1539+
# text must be in str type, see issue #706
1540+
np.atleast_1d(text).astype(str),
15401541
)
15411542
with file_context as fname:
15421543
arg_str = " ".join([fname, build_arg_string(kwargs)])

pygmt/tests/test_text.py

+23
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,26 @@ def test_text_varying_transparency():
338338
fig_test.text(x=x, y=y, text=text, transparency=transparency)
339339

340340
return fig_ref, fig_test
341+
342+
343+
@check_figures_equal()
344+
def test_text_nonstr_text():
345+
"Input text is in non-string type (e.g., int, float)"
346+
fig_ref, fig_test = Figure(), Figure()
347+
348+
# Use single-character arguments and input files for the reference image
349+
with GMTTempFile(suffix=".txt") as tempfile:
350+
with open(tempfile.name, "w") as tmpfile:
351+
tmpfile.write("1 1 1.0\n2 2 2.0\n3 3 3.0\n4 4 4.0\n")
352+
fig_ref.text(R="0/10/0/10", J="X10c", B="", textfiles=tempfile.name)
353+
354+
fig_test.text(
355+
region=[0, 10, 0, 10],
356+
projection="X10c",
357+
frame=True,
358+
x=[1, 2, 3, 4],
359+
y=[1, 2, 3, 4],
360+
text=[1, 2, 3.0, 4.0],
361+
)
362+
363+
return fig_ref, fig_test

0 commit comments

Comments
 (0)