@@ -38,9 +38,9 @@ def hlines(
38
38
The term "horizontal" lines can be interpreted differently in different coordinate
39
39
systems:
40
40
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.
42
43
- *Geographic* projection: lines are plotted as parallels along constant latitude.
43
- - *Polar* projection: lines are plotted as arcs along constant radius.
44
44
45
45
Parameters
46
46
----------
@@ -58,7 +58,8 @@ def hlines(
58
58
label
59
59
Label for the line(s), to be displayed in the legend.
60
60
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.
62
63
perspective
63
64
Select perspective view and set the azimuth and elevation angle of the
64
65
viewpoint. Refer to :method:`pygmt.Figure.plot` for details.
@@ -78,36 +79,41 @@ def hlines(
78
79
"""
79
80
self ._preprocess ()
80
81
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.
82
91
_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 )
84
94
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.
91
96
92
- # Check if xmin/xmax are scalars or have the same length.
97
+ # Check if xmin/xmax have the same length or are scalars .
93
98
if _xmin .size != _xmax .size :
94
99
msg = "'xmin' and 'xmax' are expected to be scalars or have the same length."
95
100
raise GMTInvalidInput (msg )
96
101
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 }:
104
105
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 ."
107
108
)
108
109
raise GMTInvalidInput (msg )
109
110
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.
111
117
for i in range (nlines ):
112
118
# Special handling for label.
113
119
# 1. Only specify label when plotting the first line.
@@ -121,7 +127,7 @@ def hlines(
121
127
# projection). To plot "horizontal" lines along constant latitude (in geographic
122
128
# coordinate system) or constant radius (in polar projection), we need to
123
129
# 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.
125
131
self .plot (
126
132
x = np .linspace (_xmin [i ], _xmax [i ], npoints ),
127
133
y = [_y [i ]] * npoints ,
0 commit comments