Skip to content

Commit 65f5aee

Browse files
core-manseisman
andauthored
Allow passing an array as intensity for plot3d (#1109)
Co-authored-by: Dongdong Tian <[email protected]>
1 parent 1ac4c59 commit 65f5aee

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

pygmt/src/plot3d.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,14 @@ def plot3d(
111111
Offset the plot symbol or line locations by the given amounts
112112
*dx*/*dy*\ [/*dz*] [Default is no offset].
113113
{G}
114-
intensity : float or bool
115-
Provide an *intens* value (nominally in the -1 to +1 range) to
116-
modulate the fill color by simulating illumination [Default is None].
117-
If using ``intensity=True``, we will instead read *intens* from the
118-
first data column after the symbol parameters (if given).
114+
intensity : float or bool or 1d array
115+
Provide an *intensity* value (nominally in the -1 to +1 range) to
116+
modulate the fill color by simulating illumination. If using
117+
``intensity=True``, we will instead read *intensity* from the first
118+
data column after the symbol parameters (if given). *intensity* can
119+
also be a 1d array to set varying intensity for symbols, but it is only
120+
valid for ``x``/``y``/``z``.
121+
119122
close : str
120123
[**+b**\|\ **d**\|\ **D**][**+xl**\|\ **r**\|\ *x0*]\
121124
[**+yl**\|\ **r**\|\ *y0*][**+p**\ *pen*].
@@ -183,9 +186,14 @@ def plot3d(
183186
)
184187
extra_arrays.append(sizes)
185188

186-
if "t" in kwargs and is_nonstr_iter(kwargs["t"]):
187-
extra_arrays.append(kwargs["t"])
188-
kwargs["t"] = ""
189+
for flag in ["I", "t"]:
190+
if flag in kwargs and is_nonstr_iter(kwargs[flag]):
191+
if kind != "vectors":
192+
raise GMTInvalidInput(
193+
f"Can't use arrays for {plot3d.aliases[flag]} if data is matrix or file."
194+
)
195+
extra_arrays.append(kwargs[flag])
196+
kwargs[flag] = ""
189197

190198
with Session() as lib:
191199
# Choose how data will be passed in to the module
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 79f7d8062dbb6f29ffa0a3aaa7382f13
3+
size: 24052
4+
path: test_plot3d_varying_intensity.png

pygmt/tests/test_plot3d.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,19 @@ def test_plot3d_fail_no_data(data, region):
130130
)
131131

132132

133-
def test_plot3d_fail_size_color(data, region):
133+
def test_plot3d_fail_color_size_intensity(data, region):
134134
"""
135-
Should raise an exception if array sizes and color are used with matrix.
135+
Should raise an exception if array color, sizes and intensity are used with
136+
matrix.
136137
"""
137138
fig = Figure()
139+
kwargs = dict(data=data, region=region, projection="X10c", frame="afg")
138140
with pytest.raises(GMTInvalidInput):
139-
fig.plot3d(
140-
data=data,
141-
region=region,
142-
projection="X4i",
143-
style="c0.2c",
144-
color=data[:, 2],
145-
frame="afg",
146-
)
141+
fig.plot3d(style="c0.2c", color=data[:, 2], **kwargs)
147142
with pytest.raises(GMTInvalidInput):
148-
fig.plot3d(
149-
data=data,
150-
region=region,
151-
projection="X4i",
152-
style="cc",
153-
sizes=data[:, 2],
154-
color="red",
155-
frame="afg",
156-
)
143+
fig.plot3d(style="cc", sizes=data[:, 2], color="red", **kwargs)
144+
with pytest.raises(GMTInvalidInput):
145+
fig.plot3d(style="cc", intensity=data[:, 2], color="red", **kwargs)
157146

158147

159148
@check_figures_equal()
@@ -329,6 +318,33 @@ def test_plot3d_colors_sizes_proj(data, region):
329318
return fig_ref, fig_test
330319

331320

321+
@pytest.mark.mpl_image_compare
322+
def test_plot3d_varying_intensity():
323+
"""
324+
Plot the data with array-like intensity.
325+
"""
326+
x = np.arange(-1, 1.1, 0.1)
327+
y = np.zeros(x.size)
328+
z = y
329+
intensity = x
330+
331+
fig = Figure()
332+
fig.plot3d(
333+
x=x,
334+
y=y,
335+
z=z,
336+
region=[-1.1, 1.1, -0.5, 0.5, -0.5, 0.5],
337+
projection="X15c/5c",
338+
zsize="5c",
339+
perspective=[135, 30],
340+
frame=["Sltr", "xaf+lIntensity"],
341+
style="c0.5c",
342+
color="blue",
343+
intensity=intensity,
344+
)
345+
return fig
346+
347+
332348
@check_figures_equal()
333349
def test_plot3d_transparency():
334350
"""

0 commit comments

Comments
 (0)