diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ac641d0df37..ac1b5269bfa 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,6 +37,8 @@ Bug fixes By `Mayeul d'Avezac `_. - Return correct count for scalar datetime64 arrays (:issue:`2770`) By `Dan Nowacki `_. +- Fix facetgrid colormap bug when ``extend=True``. (:issue:`2932`) + By `Deepak Cherian `_. diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 21523ede4cd..0a507993cd6 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -265,7 +265,8 @@ def _determine_cmap_params(plot_data, vmin=None, vmax=None, cmap=None, if extend is None: extend = _determine_extend(calc_data, vmin, vmax) - if levels is not None or isinstance(norm, mpl.colors.BoundaryNorm): + if ((levels is not None or isinstance(norm, mpl.colors.BoundaryNorm)) + and (not isinstance(cmap, mpl.colors.Colormap))): cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled) norm = newnorm if norm is None else norm diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 5226be0d91d..759a2974ca6 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -4,12 +4,10 @@ import numpy as np import pandas as pd import pytest -from numpy.testing import assert_array_equal import xarray as xr import xarray.plot as xplt from xarray import DataArray -from xarray.coding.times import _import_cftime from xarray.plot.plot import _infer_interval_breaks from xarray.plot.utils import ( _build_discrete_cmap, _color_palette, _determine_cmap_params, @@ -539,6 +537,25 @@ def test_cmap_sequential_option(self): cmap_params = _determine_cmap_params(self.data) assert cmap_params['cmap'] == 'magma' + def test_do_nothing_if_provided_cmap(self): + cmap_list = [ + mpl.colors.LinearSegmentedColormap.from_list('name', ['r', 'g']), + mpl.colors.ListedColormap(['r', 'g', 'b']) + ] + + # can't parametrize with mpl objects when mpl is absent + for cmap in cmap_list: + cmap_params = _determine_cmap_params(self.data, + cmap=cmap, + levels=7) + assert cmap_params['cmap'] is cmap + + def test_do_something_if_provided_str_cmap(self): + cmap = 'RdBu_r' + cmap_params = _determine_cmap_params(self.data, cmap=cmap, levels=7) + assert cmap_params['cmap'] is not cmap + assert isinstance(cmap_params['cmap'], mpl.colors.ListedColormap) + def test_cmap_sequential_explicit_option(self): with xr.set_options(cmap_sequential=mpl.cm.magma): cmap_params = _determine_cmap_params(self.data)