Skip to content

Commit fee246c

Browse files
authored
Let Figure.text() support record-by-record transparency (#716)
Each text strings can have its own transparencies.
1 parent ce8ecf8 commit fee246c

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

pygmt/base_plotting.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,10 @@ def text(
14811481
{p}
14821482
{t}
14831483
"""
1484+
1485+
# pylint: disable=too-many-locals
1486+
# pylint: disable=too-many-branches
1487+
14841488
kwargs = self._preprocess(**kwargs)
14851489

14861490
# Ensure inputs are either textfiles, x/y/text, or position/text
@@ -1515,14 +1519,24 @@ def text(
15151519
if position is not None and isinstance(position, str):
15161520
kwargs["F"] += f'+c{position}+t"{text}"'
15171521

1522+
extra_arrays = []
1523+
# If an array of transparency is given, GMT will read it from
1524+
# the last numerical column per data record.
1525+
if "t" in kwargs and is_nonstr_iter(kwargs["t"]):
1526+
extra_arrays.append(kwargs["t"])
1527+
kwargs["t"] = ""
1528+
15181529
with Session() as lib:
15191530
file_context = dummy_context(textfiles) if kind == "file" else ""
15201531
if kind == "vectors":
15211532
if position is not None:
15221533
file_context = dummy_context("")
15231534
else:
15241535
file_context = lib.virtualfile_from_vectors(
1525-
np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text)
1536+
np.atleast_1d(x),
1537+
np.atleast_1d(y),
1538+
*extra_arrays,
1539+
np.atleast_1d(text),
15261540
)
15271541
with file_context as fname:
15281542
arg_str = " ".join([fname, build_arg_string(kwargs)])

pygmt/tests/test_text.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import os
66

77
import pytest
8+
import numpy as np
89

910
from .. import Figure
1011
from ..exceptions import GMTCLibError, GMTInvalidInput
1112
from ..helpers import GMTTempFile
13+
from ..helpers.testing import check_figures_equal
1214

1315
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1416
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
@@ -295,3 +297,44 @@ def test_text_angle_font_justify_from_textfile():
295297
justify=True,
296298
)
297299
return fig
300+
301+
302+
@check_figures_equal()
303+
def test_text_transparency():
304+
"Add texts with a constant transparency"
305+
x = np.arange(1, 10)
306+
y = np.arange(11, 20)
307+
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)]
308+
309+
fig_ref, fig_test = Figure(), Figure()
310+
# Use single-character arguments for the reference image
311+
with GMTTempFile() as tmpfile:
312+
np.savetxt(tmpfile.name, np.c_[x, y, text], fmt="%s")
313+
fig_ref.basemap(R="0/10/10/20", J="X10c", B="")
314+
fig_ref.text(textfiles=tmpfile.name, t=50)
315+
316+
fig_test.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
317+
fig_test.text(x=x, y=y, text=text, transparency=50)
318+
319+
return fig_ref, fig_test
320+
321+
322+
@check_figures_equal()
323+
def test_text_varying_transparency():
324+
"Add texts with varying transparency"
325+
x = np.arange(1, 10)
326+
y = np.arange(11, 20)
327+
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)]
328+
transparency = np.arange(10, 100, 10)
329+
330+
fig_ref, fig_test = Figure(), Figure()
331+
# Use single-character arguments for the reference image
332+
with GMTTempFile() as tmpfile:
333+
np.savetxt(tmpfile.name, np.c_[x, y, transparency, text], fmt="%s")
334+
fig_ref.basemap(R="0/10/10/20", J="X10c", B="")
335+
fig_ref.text(textfiles=tmpfile.name, t="")
336+
337+
fig_test.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
338+
fig_test.text(x=x, y=y, text=text, transparency=transparency)
339+
340+
return fig_ref, fig_test

0 commit comments

Comments
 (0)