Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow np-array levels and colors in 2D plots #3295

Merged
merged 4 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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