Skip to content
forked from pydata/xarray

Commit

Permalink
plot: If provided with colormap do not modify it. (pydata#2935)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dcherian authored May 9, 2019
1 parent 24d49fc commit ab39722
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Bug fixes
By `Mayeul d'Avezac <https://github.com/mdavezac>`_.
- Return correct count for scalar datetime64 arrays (:issue:`2770`)
By `Dan Nowacki <https://github.com/dnowacki-usgs>`_.
- Fix facetgrid colormap bug when ``extend=True``. (:issue:`2932`)
By `Deepak Cherian <https://github.com/dcherian`_.
- A deep copy deep-copies the coords (:issue:`1463`)
By `Martin Pletcher <https://github.com/pletchm>`_.

Expand Down
3 changes: 2 additions & 1 deletion xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 19 additions & 2 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ab39722

Please sign in to comment.