Skip to content

Commit 2600006

Browse files
core-manseismanweiji14
authored
Allow passing a array as intensity for plot (#1065)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent cc5010e commit 2600006

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

pygmt/src/plot.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
144144
the coordinates of a *refpoint* which will serve as a fixed external
145145
reference point for all groups.
146146
{G}
147-
intensity : float or bool
148-
Provide an *intens* value (nominally in the -1 to +1 range) to
149-
modulate the fill color by simulating illumination [None]. If
150-
using ``intensity=True``, we will instead read *intens* from the
151-
first data column after the symbol parameters (if given).
147+
intensity : float or bool or 1d array
148+
Provide an *intensity* value (nominally in the -1 to +1 range) to
149+
modulate the fill color by simulating illumination. If using
150+
``intensity=True``, we will instead read *intensity* from the first
151+
data column after the symbol parameters (if given). *intensity* can
152+
also be a 1d array to set varying intensity for symbols, but it is only
153+
valid for ``x``/``y`` pairs.
152154
close : str
153155
[**+b**\|\ **d**\|\ **D**][**+xl**\|\ **r**\|\ *x0*]\
154156
[**+yl**\|\ **r**\|\ *y0*][**+p**\ *pen*].
@@ -220,9 +222,14 @@ def plot(self, x=None, y=None, data=None, sizes=None, direction=None, **kwargs):
220222
)
221223
extra_arrays.append(sizes)
222224

223-
if "t" in kwargs and is_nonstr_iter(kwargs["t"]):
224-
extra_arrays.append(kwargs["t"])
225-
kwargs["t"] = ""
225+
for flag in ["I", "t"]:
226+
if flag in kwargs and is_nonstr_iter(kwargs[flag]):
227+
if kind != "vectors":
228+
raise GMTInvalidInput(
229+
f"Can't use arrays for {plot.aliases[flag]} if data is matrix or file."
230+
)
231+
extra_arrays.append(kwargs[flag])
232+
kwargs[flag] = ""
226233

227234
with Session() as lib:
228235
# Choose how data will be passed in to the module
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: f4b44bcae2670fac23c6e43324bda8fe
3+
size: 16182
4+
path: test_plot_varying_intensity.png

pygmt/tests/test_plot.py

+31-19
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,19 @@ def test_plot_fail_no_data(data):
9393
)
9494

9595

96-
def test_plot_fail_size_color(data):
96+
def test_plot_fail_color_size_intensity(data):
9797
"""
98-
Should raise an exception if array sizes and color are used with matrix.
98+
Should raise an exception if array color, sizes and intensity are used with
99+
matrix.
99100
"""
100101
fig = Figure()
102+
kwargs = dict(data=data, region=region, projection="X10c", frame="afg")
101103
with pytest.raises(GMTInvalidInput):
102-
fig.plot(
103-
data=data,
104-
region=region,
105-
projection="X4i",
106-
style="c0.2c",
107-
color=data[:, 2],
108-
frame="afg",
109-
)
104+
fig.plot(style="c0.2c", color=data[:, 2], **kwargs)
110105
with pytest.raises(GMTInvalidInput):
111-
fig.plot(
112-
data=data,
113-
region=region,
114-
projection="X4i",
115-
style="cc",
116-
sizes=data[:, 2],
117-
color="red",
118-
frame="afg",
119-
)
106+
fig.plot(style="cc", sizes=data[:, 2], color="red", **kwargs)
107+
with pytest.raises(GMTInvalidInput):
108+
fig.plot(style="c0.2c", color="red", intensity=data[:, 2], **kwargs)
120109

121110

122111
@check_figures_equal()
@@ -231,6 +220,29 @@ def test_plot_colors_sizes_proj(data, region):
231220
return fig
232221

233222

223+
@pytest.mark.mpl_image_compare
224+
def test_plot_varying_intensity():
225+
"""
226+
Plot the data with array-like intensity.
227+
"""
228+
x = np.arange(-1, 1.1, 0.1)
229+
y = np.zeros(x.size)
230+
intensity = x
231+
232+
fig = Figure()
233+
fig.plot(
234+
x=x,
235+
y=y,
236+
region=[-1.1, 1.1, -0.5, 0.5],
237+
projection="X15c/2c",
238+
frame=["S", "xaf+lIntensity"],
239+
style="c0.5c",
240+
color="blue",
241+
intensity=intensity,
242+
)
243+
return fig
244+
245+
234246
@check_figures_equal()
235247
def test_plot_transparency():
236248
"""

0 commit comments

Comments
 (0)