Skip to content

Commit

Permalink
allow np-array levels and colors in 2D plots (#3295)
Browse files Browse the repository at this point in the history
* test if levels is None

* allow np levels and color list

* whats-new

* Update doc/whats-new.rst

Co-Authored-By: Deepak Cherian <dcherian@users.noreply.github.com>
  • Loading branch information
2 people authored and max-sixty committed Sep 9, 2019
1 parent d126044 commit 9e1c690
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ Bug fixes
By `Hasan Ahmad <https://github.com/HasanAhmadQ7>`_.
- Prevent :py:meth:`~xarray.DataArray.argmax` and :py:meth:`~xarray.DataArray.argmin` from calling
dask compute (:issue:`3237`). By `Ulrich Herter <https://github.com/ulijh>`_.
- Plots in 2 dimensions (pcolormesh, contour) now allow to specify levels as numpy
array (:issue:`3284`). By `Mathias Hauser <https://github.com/mathause>`_.

.. _whats-new.0.12.3:

Expand Down
2 changes: 1 addition & 1 deletion xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ def _process_cmap_cbar_kwargs(

# colors is only valid when levels is supplied or the plot is of type
# contour or contourf
if colors and (("contour" not in func.__name__) and (not levels)):
if colors and (("contour" not in func.__name__) and (levels is None)):
raise ValueError("Can only specify colors with contour or levels")

# we should not be getting a list of colors in cmap anymore
Expand Down
28 changes: 20 additions & 8 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,26 +1283,38 @@ class TestContour(Common2dMixin, PlotTestCase):

plotfunc = staticmethod(xplt.contour)

# matplotlib cmap.colors gives an rgbA ndarray
# when seaborn is used, instead we get an rgb tuple
@staticmethod
def _color_as_tuple(c):
return tuple(c[:3])

def test_colors(self):
# matplotlib cmap.colors gives an rgbA ndarray
# when seaborn is used, instead we get an rgb tuple
def _color_as_tuple(c):
return tuple(c[:3])

# with single color, we don't want rgb array
artist = self.plotmethod(colors="k")
assert artist.cmap.colors[0] == "k"

artist = self.plotmethod(colors=["k", "b"])
assert _color_as_tuple(artist.cmap.colors[1]) == (0.0, 0.0, 1.0)
assert self._color_as_tuple(artist.cmap.colors[1]) == (0.0, 0.0, 1.0)

artist = self.darray.plot.contour(
levels=[-0.5, 0.0, 0.5, 1.0], colors=["k", "r", "w", "b"]
)
assert _color_as_tuple(artist.cmap.colors[1]) == (1.0, 0.0, 0.0)
assert _color_as_tuple(artist.cmap.colors[2]) == (1.0, 1.0, 1.0)
assert self._color_as_tuple(artist.cmap.colors[1]) == (1.0, 0.0, 0.0)
assert self._color_as_tuple(artist.cmap.colors[2]) == (1.0, 1.0, 1.0)
# the last color is now under "over"
assert self._color_as_tuple(artist.cmap._rgba_over) == (0.0, 0.0, 1.0)

def test_colors_np_levels(self):

# https://github.com/pydata/xarray/issues/3284
levels = np.array([-0.5, 0.0, 0.5, 1.0])
artist = self.darray.plot.contour(levels=levels, colors=["k", "r", "w", "b"])
assert self._color_as_tuple(artist.cmap.colors[1]) == (1.0, 0.0, 0.0)
assert self._color_as_tuple(artist.cmap.colors[2]) == (1.0, 1.0, 1.0)
# the last color is now under "over"
assert _color_as_tuple(artist.cmap._rgba_over) == (0.0, 0.0, 1.0)
assert self._color_as_tuple(artist.cmap._rgba_over) == (0.0, 0.0, 1.0)

def test_cmap_and_color_both(self):
with pytest.raises(ValueError):
Expand Down

0 comments on commit 9e1c690

Please sign in to comment.