@@ -156,7 +156,7 @@ def plot(darray, row=None, col=None, col_wrap=None, ax=None, rtol=0.01,
156
156
# matplotlib format strings
157
157
def line (darray , * args , ** kwargs ):
158
158
"""
159
- Line plot of 1 dimensional DataArray index against values
159
+ Line plot of DataArray index against values
160
160
161
161
Wraps :func:`matplotlib:matplotlib.pyplot.plot`
162
162
@@ -176,15 +176,17 @@ def line(darray, *args, **kwargs):
176
176
ax : matplotlib axes object, optional
177
177
Axis on which to plot this figure. By default, use the current axis.
178
178
Mutually exclusive with ``size`` and ``figsize``.
179
+ x : string, optional
180
+ Coordinate for x axis (2D inputs only). If None use darray.dims[1]
179
181
*args, **kwargs : optional
180
182
Additional arguments to matplotlib.pyplot.plot
181
183
182
184
"""
183
185
plt = import_matplotlib_pyplot ()
184
186
185
187
ndims = len (darray .dims )
186
- if ndims != 1 :
187
- raise ValueError ('Line plots are for 1 dimensional DataArrays. '
188
+ if ndims > 2 :
189
+ raise ValueError ('Line plots are for 1- or 2- dimensional DataArrays. '
188
190
'Passed DataArray has {ndims} '
189
191
'dimensions' .format (ndims = ndims ))
190
192
@@ -193,11 +195,18 @@ def line(darray, *args, **kwargs):
193
195
aspect = kwargs .pop ('aspect' , None )
194
196
size = kwargs .pop ('size' , None )
195
197
ax = kwargs .pop ('ax' , None )
198
+ x = kwargs .pop ('x' , None )
196
199
197
200
ax = get_axis (figsize , size , aspect , ax )
198
201
199
- xlabel , = darray .dims
200
- x = darray .coords [xlabel ]
202
+ if ndims == 1 :
203
+ xlabel , = darray .dims
204
+ x = darray .coords [xlabel ]
205
+
206
+ else :
207
+ xlabel , ylabel = _infer_xy_labels (darray = darray , x = x , y = None )
208
+ x = darray .coords [xlabel ]
209
+ darray = darray .transpose (xlabel , ylabel )
201
210
202
211
_ensure_plottable (x )
203
212
@@ -209,6 +218,9 @@ def line(darray, *args, **kwargs):
209
218
if darray .name is not None :
210
219
ax .set_ylabel (darray .name )
211
220
221
+ if darray .ndim == 2 :
222
+ ax .legend (darray .coords [ylabel ].values , title = ylabel )
223
+
212
224
# Rotate dates on xlabels
213
225
if np .issubdtype (x .dtype , np .datetime64 ):
214
226
plt .gcf ().autofmt_xdate ()
0 commit comments