Skip to content

Commit 9e1c690

Browse files
mathausedcherian
authored andcommitted
allow np-array levels and colors in 2D plots (#3295)
* test if levels is None * allow np levels and color list * whats-new * Update doc/whats-new.rst Co-Authored-By: Deepak Cherian <[email protected]>
1 parent d126044 commit 9e1c690

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ Bug fixes
165165
By `Hasan Ahmad <https://github.com/HasanAhmadQ7>`_.
166166
- Prevent :py:meth:`~xarray.DataArray.argmax` and :py:meth:`~xarray.DataArray.argmin` from calling
167167
dask compute (:issue:`3237`). By `Ulrich Herter <https://github.com/ulijh>`_.
168+
- Plots in 2 dimensions (pcolormesh, contour) now allow to specify levels as numpy
169+
array (:issue:`3284`). By `Mathias Hauser <https://github.com/mathause>`_.
168170

169171
.. _whats-new.0.12.3:
170172

xarray/plot/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ def _process_cmap_cbar_kwargs(
731731

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

737737
# we should not be getting a list of colors in cmap anymore

xarray/tests/test_plot.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,26 +1283,38 @@ class TestContour(Common2dMixin, PlotTestCase):
12831283

12841284
plotfunc = staticmethod(xplt.contour)
12851285

1286+
# matplotlib cmap.colors gives an rgbA ndarray
1287+
# when seaborn is used, instead we get an rgb tuple
1288+
@staticmethod
1289+
def _color_as_tuple(c):
1290+
return tuple(c[:3])
1291+
12861292
def test_colors(self):
1287-
# matplotlib cmap.colors gives an rgbA ndarray
1288-
# when seaborn is used, instead we get an rgb tuple
1289-
def _color_as_tuple(c):
1290-
return tuple(c[:3])
12911293

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

12961298
artist = self.plotmethod(colors=["k", "b"])
1297-
assert _color_as_tuple(artist.cmap.colors[1]) == (0.0, 0.0, 1.0)
1299+
assert self._color_as_tuple(artist.cmap.colors[1]) == (0.0, 0.0, 1.0)
12981300

12991301
artist = self.darray.plot.contour(
13001302
levels=[-0.5, 0.0, 0.5, 1.0], colors=["k", "r", "w", "b"]
13011303
)
1302-
assert _color_as_tuple(artist.cmap.colors[1]) == (1.0, 0.0, 0.0)
1303-
assert _color_as_tuple(artist.cmap.colors[2]) == (1.0, 1.0, 1.0)
1304+
assert self._color_as_tuple(artist.cmap.colors[1]) == (1.0, 0.0, 0.0)
1305+
assert self._color_as_tuple(artist.cmap.colors[2]) == (1.0, 1.0, 1.0)
1306+
# the last color is now under "over"
1307+
assert self._color_as_tuple(artist.cmap._rgba_over) == (0.0, 0.0, 1.0)
1308+
1309+
def test_colors_np_levels(self):
1310+
1311+
# https://github.com/pydata/xarray/issues/3284
1312+
levels = np.array([-0.5, 0.0, 0.5, 1.0])
1313+
artist = self.darray.plot.contour(levels=levels, colors=["k", "r", "w", "b"])
1314+
assert self._color_as_tuple(artist.cmap.colors[1]) == (1.0, 0.0, 0.0)
1315+
assert self._color_as_tuple(artist.cmap.colors[2]) == (1.0, 1.0, 1.0)
13041316
# the last color is now under "over"
1305-
assert _color_as_tuple(artist.cmap._rgba_over) == (0.0, 0.0, 1.0)
1317+
assert self._color_as_tuple(artist.cmap._rgba_over) == (0.0, 0.0, 1.0)
13061318

13071319
def test_cmap_and_color_both(self):
13081320
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)