Skip to content

Commit b2dcd88

Browse files
core-manseisman
andauthored
Figure.plot: Add the "symbol" parameter to support plotting data points with varying symbols (#1117)
Co-authored-by: Dongdong Tian <[email protected]>
1 parent c6188b9 commit b2dcd88

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

pygmt/src/plot.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
w="wrap",
4949
)
5050
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
51-
def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs):
51+
def plot(
52+
self, data=None, x=None, y=None, size=None, symbol=None, direction=None, **kwargs
53+
):
5254
r"""
5355
Plot lines, polygons, and symbols in 2-D.
5456
@@ -87,6 +89,8 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs):
8789
size : 1-D array
8890
The size of the data points in units specified using ``style``.
8991
Only valid if using ``x``/``y``.
92+
symbol : 1-D array
93+
The symbols of the data points. Only valid if using ``x``/``y``.
9094
direction : list of two 1-D arrays
9195
If plotting vectors (using ``style="V"`` or ``style="v"``), then
9296
should be a list of two 1-D arrays with the vector directions. These
@@ -226,13 +230,19 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs):
226230
if is_nonstr_iter(kwargs.get(flag)):
227231
extra_arrays.append(kwargs.get(flag))
228232
kwargs[flag] = ""
233+
# Symbol must be at the last column
234+
if is_nonstr_iter(symbol):
235+
if "S" not in kwargs:
236+
kwargs["S"] = True
237+
extra_arrays.append(symbol)
229238
else:
230239
for name, value in [
231240
("direction", direction),
232241
("fill", kwargs.get("G")),
233242
("size", size),
234243
("intensity", kwargs.get("I")),
235244
("transparency", kwargs.get("t")),
245+
("symbol", symbol),
236246
]:
237247
if is_nonstr_iter(value):
238248
raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
outs:
2+
- md5: bf71e5e72529a85d9ac45aa71321962e
3+
size: 3798
4+
hash: md5
5+
path: test_plot_symbol.png

pygmt/tests/test_plot.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def test_plot_fail_no_data(data, region):
9393

9494
def test_plot_fail_1d_array_with_data(data, region):
9595
"""
96-
Should raise an exception if array fill, size, intensity and transparency are used
97-
with matrix.
96+
Should raise an exception if arrays of fill, size, intensity, transparency and
97+
symbol are specified when data is given.
9898
"""
9999
fig = Figure()
100100
kwargs = {"data": data, "region": region, "projection": "X10c", "frame": "afg"}
@@ -106,6 +106,8 @@ def test_plot_fail_1d_array_with_data(data, region):
106106
fig.plot(style="c0.2c", fill="red", intensity=data[:, 2], **kwargs)
107107
with pytest.raises(GMTInvalidInput):
108108
fig.plot(style="c0.2c", fill="red", transparency=data[:, 2] * 100, **kwargs)
109+
with pytest.raises(GMTInvalidInput):
110+
fig.plot(style="0.2c", fill="red", symbol=["c"] * data.shape[0], **kwargs)
109111

110112

111113
@pytest.mark.mpl_image_compare
@@ -297,6 +299,25 @@ def test_plot_sizes_colors_transparencies():
297299
return fig
298300

299301

302+
@pytest.mark.mpl_image_compare
303+
def test_plot_symbol():
304+
"""
305+
Plot the data using array-like symbols.
306+
"""
307+
fig = Figure()
308+
fig.plot(
309+
x=[1, 2, 3, 4],
310+
y=[1, 1, 1, 1],
311+
region=[0, 5, 0, 5],
312+
projection="X4c",
313+
fill="blue",
314+
size=[0.1, 0.2, 0.3, 0.4],
315+
symbol=["c", "t", "i", "s"],
316+
frame="af",
317+
)
318+
return fig
319+
320+
300321
@pytest.mark.mpl_image_compare(filename="test_plot_matrix.png")
301322
@pytest.mark.parametrize("fill", ["#aaaaaa", 170])
302323
def test_plot_matrix(data, fill):

0 commit comments

Comments
 (0)