Skip to content

Commit b2a377f

Browse files
authored
facetgrid: properly support cbar_kwargs. (#2444)
* facetgrid: properly support cbar_kwargs. * Update doc/plotting.rst * Update xarray/plot/facetgrid.py * Update whats-new.rst
1 parent d77db21 commit b2a377f

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

doc/plotting.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ Faceted plotting supports other arguments common to xarray 2d plots.
527527
528528
@savefig plot_facet_robust.png
529529
g = hasoutliers.plot.pcolormesh('lon', 'lat', col='time', col_wrap=3,
530-
robust=True, cmap='viridis')
530+
robust=True, cmap='viridis',
531+
cbar_kwargs={'label': 'this has outliers'})
531532
532533
FacetGrid Objects
533534
~~~~~~~~~~~~~~~~~

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ Enhancements
8686
Bug fixes
8787
~~~~~~~~~
8888

89+
- ``FacetGrid`` now properly uses the ``cbar_kwargs`` keyword argument.
90+
(:issue:`1504`, :issue:`1717`)
91+
By `Deepak Cherian <https://github.com/dcherian>`_.
8992
- Addition and subtraction operators used with a CFTimeIndex now preserve the
9093
index's type. (:issue:`2244`).
9194
By `Spencer Clark <https://github.com/spencerkclark>`_.

xarray/plot/facetgrid.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ def map_dataarray(self, func, x, y, **kwargs):
223223

224224
cmapkw = kwargs.get('cmap')
225225
colorskw = kwargs.get('colors')
226+
cbar_kwargs = kwargs.pop('cbar_kwargs', {})
227+
cbar_kwargs = {} if cbar_kwargs is None else dict(cbar_kwargs)
228+
229+
if kwargs.get('cbar_ax', None) is not None:
230+
raise ValueError('cbar_ax not supported by FacetGrid.')
226231

227232
# colors is mutually exclusive with cmap
228233
if cmapkw and colorskw:
@@ -264,7 +269,7 @@ def map_dataarray(self, func, x, y, **kwargs):
264269
self._finalize_grid(x, y)
265270

266271
if kwargs.get('add_colorbar', True):
267-
self.add_colorbar()
272+
self.add_colorbar(**cbar_kwargs)
268273

269274
return self
270275

xarray/tests/test_plot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,23 @@ def test_facetgrid_cmap(self):
11471147
# check that all colormaps are the same
11481148
assert len(set(m.get_cmap().name for m in fg._mappables)) == 1
11491149

1150+
def test_facetgrid_cbar_kwargs(self):
1151+
a = easy_array((10, 15, 2, 3))
1152+
d = DataArray(a, dims=['y', 'x', 'columns', 'rows'])
1153+
g = self.plotfunc(d, x='x', y='y', col='columns', row='rows',
1154+
cbar_kwargs={'label': 'test_label'})
1155+
1156+
# catch contour case
1157+
if hasattr(g, 'cbar'):
1158+
assert g.cbar._label == 'test_label'
1159+
1160+
def test_facetgrid_no_cbar_ax(self):
1161+
a = easy_array((10, 15, 2, 3))
1162+
d = DataArray(a, dims=['y', 'x', 'columns', 'rows'])
1163+
with pytest.raises(ValueError):
1164+
g = self.plotfunc(d, x='x', y='y', col='columns', row='rows',
1165+
cbar_ax=1)
1166+
11501167
def test_cmap_and_color_both(self):
11511168
with pytest.raises(ValueError):
11521169
self.plotmethod(colors='k', cmap='RdBu')

0 commit comments

Comments
 (0)