Skip to content

Commit 6556500

Browse files
committed
Refactor the codes for handling y/xmin/xmax
1 parent afe7592 commit 6556500

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

pygmt/src/hlines.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def hlines(
3838
The term "horizontal" lines can be interpreted differently in different coordinate
3939
systems:
4040
41-
- *Cartesian* coordinate system: lines are plotted as straight lines.
41+
- **Cartesian** coordinate system: lines are plotted as straight lines.
42+
- **Polar** projection: lines are plotted as arcs along constant radius.
4243
- *Geographic* projection: lines are plotted as parallels along constant latitude.
43-
- *Polar* projection: lines are plotted as arcs along constant radius.
4444
4545
Parameters
4646
----------
@@ -58,7 +58,8 @@ def hlines(
5858
label
5959
Label for the line(s), to be displayed in the legend.
6060
no_clip
61-
If ``True``, do not clip lines outside the plot region.
61+
If ``True``, do not clip lines outside the plot region. Only makes sense in
62+
Cartesian coordinate system.
6263
perspective
6364
Select perspective view and set the azimuth and elevation angle of the
6465
viewpoint. Refer to :method:`pygmt.Figure.plot` for details.
@@ -78,36 +79,41 @@ def hlines(
7879
"""
7980
self._preprocess()
8081

81-
# Ensure y is a 1D array.
82+
# Determine the x limits from the current plot region if not specified.
83+
if xmin is None or xmax is None:
84+
xlimits = self.region[:2]
85+
if xmin is None:
86+
xmin = xlimits[0]
87+
if xmax is None:
88+
xmax = xlimits[1]
89+
90+
# Ensure y/xmin/xmax are 1-D arrays.
8291
_y = np.atleast_1d(y)
83-
nlines = len(_y) # Number of lines to plot.
92+
_xmin = np.atleast_1d(xmin)
93+
_xmax = np.atleast_1d(xmax)
8494

85-
# Ensure xmin and xmax are 1D arrays.
86-
# First, determine the x limits if not specified.
87-
if xmin is None or xmax is None:
88-
xlimits = self.region[:2] # Get x limits from current plot region
89-
_xmin = np.full(nlines, xlimits[0]) if xmin is None else np.atleast_1d(xmin)
90-
_xmax = np.full(nlines, xlimits[1]) if xmax is None else np.atleast_1d(xmax)
95+
nlines = len(_y) # Number of lines to plot.
9196

92-
# Check if xmin/xmax are scalars or have the same length.
97+
# Check if xmin/xmax have the same length or are scalars.
9398
if _xmin.size != _xmax.size:
9499
msg = "'xmin' and 'xmax' are expected to be scalars or have the same length."
95100
raise GMTInvalidInput(msg)
96101

97-
# Ensure _xmin/_xmax match the _y length if they're scalars or have length 1.
98-
if _xmin.size == 1 and _xmax.size == 1:
99-
_xmin = np.repeat(_xmin, nlines)
100-
_xmax = np.repeat(_xmax, nlines)
101-
102-
# Check if _xmin/_xmax match the _y length.
103-
if _xmin.size != nlines or _xmax.size != nlines:
102+
# Check if xmin/xmax have the expected length.
103+
# Only check xmin, since we already know that xmin/xmax have the same length.
104+
if _xmin.size not in {1, nlines}:
104105
msg = (
105-
f"'xmin' and 'xmax' are expected to have length '{nlines}' but "
106-
f"have length '{_xmin.size}' and '{_xmax.size}'."
106+
f"'xmin' and 'xmax' are expected to be scalars or have lengths '{nlines}', "
107+
f"but lengths '{_xmin.size}' and '{_xmax.size}' are given."
107108
)
108109
raise GMTInvalidInput(msg)
109110

110-
# Call the plot method to plot the lines.
111+
# Ensure xmin/xmax have the same length as y.
112+
if _xmin.size == 1 and nlines != 1:
113+
_xmin = np.repeat(_xmin, nlines)
114+
_xmax = np.repeat(_xmax, nlines)
115+
116+
# Call the Figure.plot method to plot the lines.
111117
for i in range(nlines):
112118
# Special handling for label.
113119
# 1. Only specify label when plotting the first line.
@@ -121,7 +127,7 @@ def hlines(
121127
# projection). To plot "horizontal" lines along constant latitude (in geographic
122128
# coordinate system) or constant radius (in polar projection), we need to
123129
# resample the line to at least 4 points.
124-
npoints = 4 # 2 for Cartesian, 4 for geographic and polar projections.
130+
npoints = 4 # 2 for Cartesian, at least 4 for geographic and polar projections.
125131
self.plot(
126132
x=np.linspace(_xmin[i], _xmax[i], npoints),
127133
y=[_y[i]] * npoints,

0 commit comments

Comments
 (0)