Skip to content

Commit 7965dbf

Browse files
committed
Figure.plot: Refactor to increase code readability
1 parent e64cfef commit 7965dbf

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

pygmt/src/plot.py

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -214,48 +214,49 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs):
214214
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
215215

216216
kind = data_kind(data, x, y)
217-
218217
extra_arrays = []
219-
if kwargs.get("S") is not None and kwargs["S"][0] in "vV" and direction is not None:
220-
extra_arrays.extend(direction)
221-
elif (
222-
kwargs.get("S") is None
223-
and kind == "geojson"
224-
and data.geom_type.isin(["Point", "MultiPoint"]).all()
225-
): # checking if the geometry of a geoDataFrame is Point or MultiPoint
226-
kwargs["S"] = "s0.2c"
227-
elif kwargs.get("S") is None and kind == "file" and str(data).endswith(".gmt"):
228-
# checking that the data is a file path to set default style
229-
try:
230-
with open(which(data), mode="r", encoding="utf8") as file:
231-
line = file.readline()
232-
if "@GMULTIPOINT" in line or "@GPOINT" in line:
233-
# if the file is gmt style and geometry is set to Point
218+
219+
if kind != "vectors":
220+
# Parameters can't be 1-D arrays if "data" is used
221+
for arg, name in [
222+
(direction, "direction"),
223+
(kwargs.get("G"), "fill"),
224+
(size, "size"),
225+
(kwargs.get("I"), "intensity"),
226+
(kwargs.get("t"), "transparency"),
227+
]:
228+
if is_nonstr_iter(arg):
229+
raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.")
230+
231+
# Set the default style if the data has a geometry of Point or MultiPoint
232+
if kwargs.get("S") is None:
233+
if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all():
234234
kwargs["S"] = "s0.2c"
235-
except FileNotFoundError:
236-
pass
237-
if kwargs.get("G") is not None and is_nonstr_iter(kwargs["G"]):
238-
if kind != "vectors":
239-
raise GMTInvalidInput(
240-
"Can't use arrays for fill if data is matrix or file."
241-
)
242-
extra_arrays.append(kwargs["G"])
243-
del kwargs["G"]
244-
if size is not None:
245-
if kind != "vectors":
246-
raise GMTInvalidInput(
247-
"Can't use arrays for 'size' if data is a matrix or file."
248-
)
249-
extra_arrays.append(size)
235+
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
236+
try:
237+
with open(which(data), mode="r", encoding="utf8") as file:
238+
line = file.readline()
239+
if "@GMULTIPOINT" in line or "@GPOINT" in line:
240+
kwargs["S"] = "s0.2c"
241+
except FileNotFoundError:
242+
pass
243+
else:
244+
if (
245+
kwargs.get("S") is not None
246+
and kwargs["S"][0] in "vV"
247+
and direction is not None
248+
):
249+
extra_arrays.extend(direction)
250250

251-
for flag in ["I", "t"]:
252-
if kwargs.get(flag) is not None and is_nonstr_iter(kwargs[flag]):
253-
if kind != "vectors":
254-
raise GMTInvalidInput(
255-
f"Can't use arrays for {plot.aliases[flag]} if data is matrix or file."
256-
)
257-
extra_arrays.append(kwargs[flag])
258-
kwargs[flag] = ""
251+
if is_nonstr_iter(kwargs.get("G")):
252+
extra_arrays.append(kwargs.get("G"))
253+
del kwargs["G"]
254+
if is_nonstr_iter(size):
255+
extra_arrays.append(size)
256+
for flag in ["I", "t"]:
257+
if is_nonstr_iter(kwargs.get(flag)):
258+
extra_arrays.append(kwargs.get(flag))
259+
kwargs[flag] = ""
259260

260261
with Session() as lib:
261262
file_context = lib.virtualfile_from_data(

0 commit comments

Comments
 (0)