Skip to content

Commit 95e5ea9

Browse files
author
dcherian
committed
Only allow one of x or y to be specified.
1 parent 904e822 commit 95e5ea9

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

xarray/plot/plot.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,8 @@ def line(darray, *args, **kwargs):
177177
hue : string, optional
178178
Coordinate for which you want multiple lines plotted
179179
(2D DataArrays only).
180-
x : string, optional
181-
1D and 2D DataArrays: Coordinate for x axis.
182-
y : string, optional
183-
1D DataArray: Can be coordinate name or DataArray.name
184-
2D DataArray: Coordinate for y axis.
180+
x, y : string, optional
181+
Coordinates for x, y axis. Only one of these may be specified.
185182
add_legend : boolean, optional
186183
Add legend with y axis coordinates (2D inputs only).
187184
*args, **kwargs : optional
@@ -207,29 +204,27 @@ def line(darray, *args, **kwargs):
207204

208205
ax = get_axis(figsize, size, aspect, ax)
209206

210-
if ndims == 1:
211-
dim, = darray.dims # get the only dimension name
207+
error_msg = 'must be either None or %r' % (' or '.join(darray.dims))
212208

213-
error_msg = 'must be either None or %r' % dim
214-
if darray.name:
215-
error_msg += ' or %r.' % darray.name
209+
if x not in [None, *darray.dims]:
210+
raise ValueError('x ' + error_msg)
216211

217-
if x not in [None, dim, darray.name]:
218-
raise ValueError('x ' + error_msg)
212+
if y not in [None, *darray.dims]:
213+
raise ValueError('y ' + error_msg)
219214

220-
if y not in [None, dim, darray.name]:
221-
raise ValueError('y ' + error_msg)
215+
if x is not None and y is not None:
216+
raise ValueError('You cannot specify both x and y kwargs.')
222217

223-
if x is not None and y is not None and x == y:
224-
raise ValueError('Cannot make a plot with x=%r and y=%r' % (x, y))
218+
if ndims == 1:
219+
dim, = darray.dims # get the only dimension name
225220

226-
if (x is None and y is None) or x == dim or y == darray.name:
221+
if (x is None and y is None) or x == dim:
227222
xplt = darray.coords[dim]
228223
yplt = darray
229224
xlabel = dim
230225
ylabel = darray.name
231226

232-
elif y == dim or x == darray.name:
227+
else:
233228
yplt = darray.coords[dim]
234229
xplt = darray
235230
xlabel = darray.name
@@ -246,10 +241,6 @@ def line(darray, *args, **kwargs):
246241
yplt = darray.transpose(xlabel, huelabel)
247242

248243
else:
249-
if x is not None and x is not darray.name:
250-
raise ValueError('Cannot make a plot with x=%r and y=%r '
251-
% (x, y))
252-
253244
ylabel, huelabel = _infer_xy_labels(darray=darray, x=y, y=hue)
254245
xlabel = darray.name
255246
xplt = darray.transpose(ylabel, huelabel)

xarray/tests/test_plot.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,24 @@ def test_1d_x_y_kw(self):
100100
da = DataArray(np.cos(z), dims=['z'], coords=[z], name='f')
101101

102102
xy = [[None, None],
103-
[None, 'f'],
104103
[None, 'z'],
105-
['f', None],
106-
['z', None],
107-
['z', 'f'],
108-
['f', 'z']]
104+
['z', None]]
109105

110106
f, ax = plt.subplots(2, 4)
111107

112108
for aa, (x, y) in enumerate(xy):
113109
da.plot(x=x, y=y, ax=ax.flat[aa])
114110
ax.flat[aa].set_title('x=' + str(x) + ' | '+'y='+str(y))
115111

116-
with raises_regex(ValueError, 'Cannot'):
112+
with raises_regex(ValueError, 'cannot'):
117113
da.plot(x='z', y='z')
118114

115+
with raises_regex(ValueError, 'None'):
116+
da.plot(x='f', y='z')
117+
118+
with raises_regex(ValueError, 'None'):
119+
da.plot(x='z', y='f')
120+
119121
def test_2d_line(self):
120122
with raises_regex(ValueError, 'hue'):
121123
self.darray[:, :, 0].plot.line()
@@ -126,7 +128,7 @@ def test_2d_line(self):
126128
self.darray[:, :, 0].plot.line(x='dim_0', hue='dim_1')
127129
self.darray[:, :, 0].plot.line(y='dim_0', hue='dim_1')
128130

129-
with raises_regex(ValueError, 'Cannot'):
131+
with raises_regex(ValueError, 'cannot'):
130132
self.darray[:, :, 0].plot.line(x='dim_1', y='dim_0', hue='dim_1')
131133

132134
def test_2d_line_accepts_legend_kw(self):
@@ -323,7 +325,7 @@ def test_ylabel_is_data_name(self):
323325

324326
def test_xlabel_is_data_name(self):
325327
self.darray.name = 'temperature'
326-
self.darray.plot(x=self.darray.name)
328+
self.darray.plot(y='period')
327329
self.assertEqual(self.darray.name, plt.gca().get_xlabel())
328330

329331
def test_format_string(self):

0 commit comments

Comments
 (0)