@@ -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,19 @@ 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]
181
+ add_legend : boolean, optional
182
+ Add legend with y axis coordinates (2D inputs only).
179
183
*args, **kwargs : optional
180
184
Additional arguments to matplotlib.pyplot.plot
181
185
182
186
"""
183
187
plt = import_matplotlib_pyplot ()
184
188
185
189
ndims = len (darray .dims )
186
- if ndims != 1 :
187
- raise ValueError ('Line plots are for 1 dimensional DataArrays. '
190
+ if ndims > 2 :
191
+ raise ValueError ('Line plots are for 1- or 2- dimensional DataArrays. '
188
192
'Passed DataArray has {ndims} '
189
193
'dimensions' .format (ndims = ndims ))
190
194
@@ -193,11 +197,19 @@ def line(darray, *args, **kwargs):
193
197
aspect = kwargs .pop ('aspect' , None )
194
198
size = kwargs .pop ('size' , None )
195
199
ax = kwargs .pop ('ax' , None )
200
+ x = kwargs .pop ('x' , None )
201
+ add_legend = kwargs .pop ('add_legend' , True )
196
202
197
203
ax = get_axis (figsize , size , aspect , ax )
198
204
199
- xlabel , = darray .dims
200
- x = darray .coords [xlabel ]
205
+ if ndims == 1 :
206
+ xlabel , = darray .dims
207
+ x = darray .coords [xlabel ]
208
+
209
+ else :
210
+ xlabel , ylabel = _infer_xy_labels (darray = darray , x = x , y = None )
211
+ x = darray .coords [xlabel ]
212
+ darray = darray .transpose (xlabel , ylabel )
201
213
202
214
_ensure_plottable (x )
203
215
@@ -209,6 +221,9 @@ def line(darray, *args, **kwargs):
209
221
if darray .name is not None :
210
222
ax .set_ylabel (darray .name )
211
223
224
+ if darray .ndim == 2 and add_legend :
225
+ ax .legend (darray .coords [ylabel ].values , title = ylabel )
226
+
212
227
# Rotate dates on xlabels
213
228
if np .issubdtype (x .dtype , np .datetime64 ):
214
229
plt .gcf ().autofmt_xdate ()
0 commit comments