Skip to content

Commit ab39722

Browse files
authored
plot: If provided with colormap do not modify it. (#2935)
* plot: If provided with colormap do not modify it. * lint * pep8 * Fixes. * More fixes. * autopep8 * more pep8 * whats-new under bugfixes * Simpler colormap check. * lint. * @requires_matplotlib why is this needed? * Fix test * neaten * update comment.
1 parent 24d49fc commit ab39722

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Bug fixes
3737
By `Mayeul d'Avezac <https://github.com/mdavezac>`_.
3838
- Return correct count for scalar datetime64 arrays (:issue:`2770`)
3939
By `Dan Nowacki <https://github.com/dnowacki-usgs>`_.
40+
- Fix facetgrid colormap bug when ``extend=True``. (:issue:`2932`)
41+
By `Deepak Cherian <https://github.com/dcherian`_.
4042
- A deep copy deep-copies the coords (:issue:`1463`)
4143
By `Martin Pletcher <https://github.com/pletchm>`_.
4244

xarray/plot/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ def _determine_cmap_params(plot_data, vmin=None, vmax=None, cmap=None,
265265
if extend is None:
266266
extend = _determine_extend(calc_data, vmin, vmax)
267267

268-
if levels is not None or isinstance(norm, mpl.colors.BoundaryNorm):
268+
if ((levels is not None or isinstance(norm, mpl.colors.BoundaryNorm))
269+
and (not isinstance(cmap, mpl.colors.Colormap))):
269270
cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled)
270271
norm = newnorm if norm is None else norm
271272

xarray/tests/test_plot.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import numpy as np
55
import pandas as pd
66
import pytest
7-
from numpy.testing import assert_array_equal
87

98
import xarray as xr
109
import xarray.plot as xplt
1110
from xarray import DataArray
12-
from xarray.coding.times import _import_cftime
1311
from xarray.plot.plot import _infer_interval_breaks
1412
from xarray.plot.utils import (
1513
_build_discrete_cmap, _color_palette, _determine_cmap_params,
@@ -539,6 +537,25 @@ def test_cmap_sequential_option(self):
539537
cmap_params = _determine_cmap_params(self.data)
540538
assert cmap_params['cmap'] == 'magma'
541539

540+
def test_do_nothing_if_provided_cmap(self):
541+
cmap_list = [
542+
mpl.colors.LinearSegmentedColormap.from_list('name', ['r', 'g']),
543+
mpl.colors.ListedColormap(['r', 'g', 'b'])
544+
]
545+
546+
# can't parametrize with mpl objects when mpl is absent
547+
for cmap in cmap_list:
548+
cmap_params = _determine_cmap_params(self.data,
549+
cmap=cmap,
550+
levels=7)
551+
assert cmap_params['cmap'] is cmap
552+
553+
def test_do_something_if_provided_str_cmap(self):
554+
cmap = 'RdBu_r'
555+
cmap_params = _determine_cmap_params(self.data, cmap=cmap, levels=7)
556+
assert cmap_params['cmap'] is not cmap
557+
assert isinstance(cmap_params['cmap'], mpl.colors.ListedColormap)
558+
542559
def test_cmap_sequential_explicit_option(self):
543560
with xr.set_options(cmap_sequential=mpl.cm.magma):
544561
cmap_params = _determine_cmap_params(self.data)

0 commit comments

Comments
 (0)