Skip to content

Commit 4b3b3eb

Browse files
authored
Figure.plot: Refactor to increase code readability (#2742)
1 parent 81db87e commit 4b3b3eb

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

pygmt/src/plot.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -209,48 +209,50 @@ def plot( # noqa: PLR0912
209209
kwargs = self._preprocess(**kwargs)
210210

211211
kind = data_kind(data, x, y)
212-
213212
extra_arrays = []
214-
if kwargs.get("S") is not None and kwargs["S"][0] in "vV" and direction is not None:
215-
extra_arrays.extend(direction)
216-
elif (
217-
kwargs.get("S") is None
218-
and kind == "geojson"
219-
and data.geom_type.isin(["Point", "MultiPoint"]).all()
220-
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
221-
kwargs["S"] = "s0.2c"
222-
elif kwargs.get("S") is None and kind == "file" and str(data).endswith(".gmt"):
223-
# checking that the data is a file path to set default style
224-
try:
225-
with Path(which(data)).open(encoding="utf8") as file:
226-
line = file.readline()
227-
if "@GMULTIPOINT" in line or "@GPOINT" in line:
228-
# if the file is gmt style and geometry is set to Point
229-
kwargs["S"] = "s0.2c"
230-
except FileNotFoundError:
231-
pass
232-
if is_nonstr_iter(kwargs.get("G")):
233-
if kind != "vectors":
234-
raise GMTInvalidInput(
235-
"Can't use arrays for fill if data is matrix or file."
236-
)
237-
extra_arrays.append(kwargs["G"])
238-
del kwargs["G"]
239-
if size is not None:
240-
if kind != "vectors":
241-
raise GMTInvalidInput(
242-
"Can't use arrays for 'size' if data is a matrix or file."
243-
)
244-
extra_arrays.append(size)
213+
if kind == "vectors": # Add more columns for vectors input
214+
# Parameters for vector styles
215+
if (
216+
kwargs.get("S") is not None
217+
and kwargs["S"][0] in "vV"
218+
and is_nonstr_iter(direction)
219+
):
220+
extra_arrays.extend(direction)
221+
# Fill
222+
if is_nonstr_iter(kwargs.get("G")):
223+
extra_arrays.append(kwargs.get("G"))
224+
del kwargs["G"]
225+
# Size
226+
if is_nonstr_iter(size):
227+
extra_arrays.append(size)
228+
# Intensity and transparency
229+
for flag in ["I", "t"]:
230+
if is_nonstr_iter(kwargs.get(flag)):
231+
extra_arrays.append(kwargs.get(flag))
232+
kwargs[flag] = ""
233+
else:
234+
for name, value in [
235+
("direction", direction),
236+
("fill", kwargs.get("G")),
237+
("size", size),
238+
("intensity", kwargs.get("I")),
239+
("transparency", kwargs.get("t")),
240+
]:
241+
if is_nonstr_iter(value):
242+
raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.")
245243

246-
for flag in ["I", "t"]:
247-
if is_nonstr_iter(kwargs.get(flag)):
248-
if kind != "vectors":
249-
raise GMTInvalidInput(
250-
f"Can't use arrays for {plot.aliases[flag]} if data is matrix or file."
251-
)
252-
extra_arrays.append(kwargs[flag])
253-
kwargs[flag] = ""
244+
# Set the default style if data has a geometry of Point or MultiPoint
245+
if kwargs.get("S") is None:
246+
if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
247+
kwargs["S"] = "s0.2c"
248+
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
249+
try:
250+
with Path(which(data)).open() as file:
251+
line = file.readline()
252+
if "@GMULTIPOINT" in line or "@GPOINT" in line:
253+
kwargs["S"] = "s0.2c"
254+
except FileNotFoundError:
255+
pass
254256

255257
with Session() as lib:
256258
with lib.virtualfile_in(

0 commit comments

Comments
 (0)