Skip to content

Commit df88e02

Browse files
committed
Figure.plot/plot3d/text: Pass a dict of vectors rather than x/y/extra_arrays
1 parent c33be01 commit df88e02

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

pygmt/src/plot.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
w="wrap",
5151
)
5252
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
53-
def plot(
53+
def plot( # noqa: PLR0912
5454
self,
5555
data=None,
5656
x=None,
@@ -232,34 +232,37 @@ def plot(
232232
kwargs = self._preprocess(**kwargs)
233233

234234
kind = data_kind(data)
235-
extra_arrays = []
236-
if kind == "empty": # Add more columns for vectors input
235+
if kind == "empty": # Data is given via a series of vectors.
236+
data = {"x": x, "y": y}
237237
# Parameters for vector styles
238238
if (
239239
isinstance(kwargs.get("S"), str)
240240
and len(kwargs["S"]) >= 1
241241
and kwargs["S"][0] in "vV"
242242
and is_nonstr_iter(direction)
243243
):
244-
extra_arrays.extend(direction)
244+
data.update({"x2": direction[0], "y2": direction[1]})
245245
# Fill
246246
if is_nonstr_iter(kwargs.get("G")):
247-
extra_arrays.append(kwargs.get("G"))
247+
data["fill"] = kwargs["G"]
248248
del kwargs["G"]
249249
# Size
250250
if is_nonstr_iter(size):
251-
extra_arrays.append(size)
251+
data["size"] = size
252252
# Intensity and transparency
253-
for flag in ["I", "t"]:
253+
for flag, name in ["I", "intensity"], ["t", "transparency"]:
254254
if is_nonstr_iter(kwargs.get(flag)):
255-
extra_arrays.append(kwargs.get(flag))
255+
data[name] = kwargs[flag]
256256
kwargs[flag] = ""
257257
# Symbol must be at the last column
258258
if is_nonstr_iter(symbol):
259259
if "S" not in kwargs:
260260
kwargs["S"] = True
261-
extra_arrays.append(symbol)
261+
data["symbol"] = symbol
262262
else:
263+
if any(v is not None for v in (x, y)):
264+
msg = "Too much data. Use either data or x/y/z."
265+
raise GMTInvalidInput(msg)
263266
for name, value in [
264267
("direction", direction),
265268
("fill", kwargs.get("G")),
@@ -277,7 +280,5 @@ def plot(
277280
kwargs["S"] = "s0.2c"
278281

279282
with Session() as lib:
280-
with lib.virtualfile_in(
281-
check_kind="vector", data=data, x=x, y=y, extra_arrays=extra_arrays
282-
) as vintbl:
283+
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
283284
lib.call_module(module="plot", args=build_arg_list(kwargs, infile=vintbl))

pygmt/src/plot3d.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
w="wrap",
5252
)
5353
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
54-
def plot3d(
54+
def plot3d( # noqa: PLR0912
5555
self,
5656
data=None,
5757
x=None,
@@ -210,35 +210,38 @@ def plot3d(
210210
kwargs = self._preprocess(**kwargs)
211211

212212
kind = data_kind(data)
213-
extra_arrays = []
214-
215-
if kind == "empty": # Add more columns for vectors input
213+
if kind == "empty": # Data is given via a series of vectors.
214+
data = {"x": x, "y": y, "z": z}
216215
# Parameters for vector styles
217216
if (
218217
isinstance(kwargs.get("S"), str)
219218
and len(kwargs["S"]) >= 1
220219
and kwargs["S"][0] in "vV"
221220
and is_nonstr_iter(direction)
222221
):
223-
extra_arrays.extend(direction)
222+
data.update({"x2": direction[0], "y2": direction[1]})
224223
# Fill
225224
if is_nonstr_iter(kwargs.get("G")):
226-
extra_arrays.append(kwargs.get("G"))
225+
data["fill"] = kwargs["G"]
227226
del kwargs["G"]
228227
# Size
229228
if is_nonstr_iter(size):
230-
extra_arrays.append(size)
229+
data["size"] = size
231230
# Intensity and transparency
232-
for flag in ["I", "t"]:
231+
for flag, name in [("I", "intensity"), ("t", "transparency")]:
233232
if is_nonstr_iter(kwargs.get(flag)):
234-
extra_arrays.append(kwargs.get(flag))
233+
data[name] = kwargs[flag]
235234
kwargs[flag] = ""
236235
# Symbol must be at the last column
237236
if is_nonstr_iter(symbol):
238237
if "S" not in kwargs:
239238
kwargs["S"] = True
240-
extra_arrays.append(symbol)
239+
data["symbol"] = symbol
241240
else:
241+
if any(v is not None for v in (x, y, z)):
242+
msg = "Too much data. Use either data or x/y/z."
243+
raise GMTInvalidInput(msg)
244+
242245
for name, value in [
243246
("direction", direction),
244247
("fill", kwargs.get("G")),
@@ -257,12 +260,6 @@ def plot3d(
257260

258261
with Session() as lib:
259262
with lib.virtualfile_in(
260-
check_kind="vector",
261-
data=data,
262-
x=x,
263-
y=y,
264-
z=z,
265-
extra_arrays=extra_arrays,
266-
required_z=True,
263+
check_kind="vector", data=data, required_z=True
267264
) as vintbl:
268265
lib.call_module(module="plot3d", args=build_arg_list(kwargs, infile=vintbl))

pygmt/src/text.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,24 @@ def text_( # noqa: PLR0912
222222
elif isinstance(arg, int | float | str):
223223
kwargs["F"] += f"{flag}{arg}"
224224

225-
extra_arrays = []
226225
confdict = {}
226+
data = None
227227
if kind == "empty":
228+
data = {"x": x, "y": y}
229+
228230
for arg, flag, name in array_args:
229231
if is_nonstr_iter(arg):
230232
kwargs["F"] += flag
231233
# angle is numeric type and font/justify are str type.
232234
if name == "angle":
233-
extra_arrays.append(arg)
235+
data["angle"] = arg
234236
else:
235-
extra_arrays.append(np.asarray(arg, dtype=np.str_))
237+
data[name] = np.asarray(arg, dtype=np.str_)
236238

237239
# If an array of transparency is given, GMT will read it from the last numerical
238240
# column per data record.
239241
if is_nonstr_iter(kwargs.get("t")):
240-
extra_arrays.append(kwargs["t"])
242+
data["transparency"] = kwargs["t"]
241243
kwargs["t"] = True
242244

243245
# Append text to the last column. Text must be passed in as str type.
@@ -247,7 +249,7 @@ def text_( # noqa: PLR0912
247249
text, encoding=encoding
248250
)
249251
confdict["PS_CHAR_ENCODING"] = encoding
250-
extra_arrays.append(text)
252+
data["text"] = text
251253
else:
252254
if isinstance(position, str):
253255
kwargs["F"] += f"+c{position}+t{text}"
@@ -260,10 +262,7 @@ def text_( # noqa: PLR0912
260262
with Session() as lib:
261263
with lib.virtualfile_in(
262264
check_kind="vector",
263-
data=textfiles,
264-
x=x,
265-
y=y,
266-
extra_arrays=extra_arrays,
265+
data=textfiles or data,
267266
required_data=required_data,
268267
) as vintbl:
269268
lib.call_module(

0 commit comments

Comments
 (0)