Skip to content

Commit 73b476e

Browse files
dcherianshoyer
authored andcommitted
Bugfix for faceting line plots. (#2229)
* Bugfix for faceting line plots. * Make add_legend public and usable.
1 parent 59ad782 commit 73b476e

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

xarray/plot/facetgrid.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,16 @@ def map_dataarray_line(self, x=None, y=None, hue=None, **kwargs):
293293
ax=ax, _labels=False,
294294
**kwargs)
295295
self._mappables.append(mappable)
296-
_, _, _, xlabel, ylabel, huelabel = _infer_line_data(
296+
_, _, hueplt, xlabel, ylabel, huelabel = _infer_line_data(
297297
darray=self.data.loc[self.name_dicts.flat[0]],
298298
x=x, y=y, hue=hue)
299299

300+
self._hue_var = hueplt
301+
self._hue_label = huelabel
300302
self._finalize_grid(xlabel, ylabel)
301303

302-
if add_legend and huelabel:
303-
self.add_line_legend(huelabel)
304+
if add_legend and hueplt is not None and huelabel is not None:
305+
self.add_legend()
304306

305307
return self
306308

@@ -314,12 +316,12 @@ def _finalize_grid(self, *axlabels):
314316
if namedict is None:
315317
ax.set_visible(False)
316318

317-
def add_line_legend(self, huelabel):
319+
def add_legend(self, **kwargs):
318320
figlegend = self.fig.legend(
319321
handles=self._mappables[-1],
320-
labels=list(self.data.coords[huelabel].values),
321-
title=huelabel,
322-
loc="center right")
322+
labels=list(self._hue_var.values),
323+
title=self._hue_label,
324+
loc="center right", **kwargs)
323325

324326
# Draw the plot to set the bounding boxes correctly
325327
self.fig.draw(self.fig.canvas.get_renderer())

xarray/tests/test_plot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,12 @@ def setUp(self):
15291529
range(3), ['A', 'B', 'C', 'C++']],
15301530
name='Cornelius Ortega the 1st')
15311531

1532+
self.darray.hue.name = 'huename'
1533+
self.darray.hue.attrs['units'] = 'hunits'
1534+
self.darray.x.attrs['units'] = 'xunits'
1535+
self.darray.col.attrs['units'] = 'colunits'
1536+
self.darray.row.attrs['units'] = 'rowunits'
1537+
15321538
def test_facetgrid_shape(self):
15331539
g = self.darray.plot(row='row', col='col', hue='hue')
15341540
assert g.axes.shape == (len(self.darray.row), len(self.darray.col))

0 commit comments

Comments
 (0)