Skip to content

Commit 713ea72

Browse files
authored
Let plot() accept record-by-record transparency (#626)
plot() can accept a list of transparency so that each symbol can have its own transparency level.
1 parent 0453ea0 commit 713ea72

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

pygmt/base_plotting.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
fmt_docstring,
1616
use_alias,
1717
kwargs_to_strings,
18+
is_nonstr_iter,
1819
)
1920

2021

@@ -684,6 +685,9 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
684685
685686
{p}
686687
{t}
688+
*transparency* can also be a 1d array to set varying transparency
689+
for symbols.
690+
687691
"""
688692
kwargs = self._preprocess(**kwargs)
689693

@@ -706,6 +710,10 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
706710
)
707711
extra_arrays.append(sizes)
708712

713+
if "t" in kwargs and is_nonstr_iter(kwargs["t"]):
714+
extra_arrays.append(kwargs["t"])
715+
kwargs["t"] = ""
716+
709717
with Session() as lib:
710718
# Choose how data will be passed in to the module
711719
if kind == "file":

pygmt/tests/test_plot.py

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
from .. import Figure
1515
from ..exceptions import GMTInvalidInput
16+
from ..helpers import GMTTempFile
17+
from ..helpers.testing import check_figures_equal
1618

1719

1820
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
@@ -128,7 +130,7 @@ def test_plot_projection(data):
128130

129131
@pytest.mark.mpl_image_compare
130132
def test_plot_colors(data, region):
131-
"Plot the data using z as sizes"
133+
"Plot the data using z as colors"
132134
fig = Figure()
133135
fig.plot(
134136
x=data[:, 0],
@@ -194,6 +196,104 @@ def test_plot_colors_sizes_proj(data, region):
194196
return fig
195197

196198

199+
@check_figures_equal()
200+
def test_plot_transparency():
201+
"Plot the data with a constant transparency"
202+
x = np.arange(1, 10)
203+
y = np.arange(1, 10)
204+
205+
fig_ref, fig_test = Figure(), Figure()
206+
# Use single-character arguments for the reference image
207+
with GMTTempFile() as tmpfile:
208+
np.savetxt(tmpfile.name, np.c_[x, y], fmt="%d")
209+
fig_ref.plot(
210+
data=tmpfile.name, S="c0.2c", G="blue", t=80.0, R="0/10/0/10", J="X4i", B=""
211+
)
212+
213+
fig_test.plot(
214+
x=x,
215+
y=y,
216+
region=[0, 10, 0, 10],
217+
projection="X4i",
218+
frame=True,
219+
style="c0.2c",
220+
color="blue",
221+
transparency=80.0,
222+
)
223+
return fig_ref, fig_test
224+
225+
226+
@check_figures_equal()
227+
def test_plot_varying_transparency():
228+
"Plot the data using z as transparency"
229+
x = np.arange(1, 10)
230+
y = np.arange(1, 10)
231+
z = np.arange(1, 10) * 10
232+
233+
fig_ref, fig_test = Figure(), Figure()
234+
# Use single-character arguments for the reference image
235+
with GMTTempFile() as tmpfile:
236+
np.savetxt(tmpfile.name, np.c_[x, y, z], fmt="%d")
237+
fig_ref.plot(
238+
data=tmpfile.name,
239+
R="0/10/0/10",
240+
J="X4i",
241+
B="",
242+
S="c0.2c",
243+
G="blue",
244+
t="",
245+
)
246+
247+
fig_test.plot(
248+
x=x,
249+
y=y,
250+
region=[0, 10, 0, 10],
251+
projection="X4i",
252+
frame=True,
253+
style="c0.2c",
254+
color="blue",
255+
transparency=z,
256+
)
257+
return fig_ref, fig_test
258+
259+
260+
@check_figures_equal()
261+
def test_plot_sizes_colors_transparencies():
262+
"Plot the data using z as transparency"
263+
x = np.arange(1.0, 10.0)
264+
y = np.arange(1.0, 10.0)
265+
color = np.arange(1, 10) * 0.15
266+
size = np.arange(1, 10) * 0.2
267+
transparency = np.arange(1, 10) * 10
268+
269+
fig_ref, fig_test = Figure(), Figure()
270+
# Use single-character arguments for the reference image
271+
with GMTTempFile() as tmpfile:
272+
np.savetxt(tmpfile.name, np.c_[x, y, color, size, transparency])
273+
fig_ref.plot(
274+
data=tmpfile.name,
275+
R="0/10/0/10",
276+
J="X4i",
277+
B="",
278+
S="cc",
279+
C="gray",
280+
t="",
281+
)
282+
fig_test.plot(
283+
x=x,
284+
y=y,
285+
region=[0, 10, 0, 10],
286+
projection="X4i",
287+
frame=True,
288+
style="cc",
289+
color=color,
290+
sizes=size,
291+
cmap="gray",
292+
transparency=transparency,
293+
)
294+
return fig_ref, fig_test
295+
296+
197297
@pytest.mark.mpl_image_compare
198298
def test_plot_matrix(data):
199299
"Plot the data passing in a matrix and specifying columns"

0 commit comments

Comments
 (0)