Skip to content

Commit ccbb84d

Browse files
authored
Fix pcolormesh with str coords (#7612)
* pcolormesh with str coords * add whats-new
1 parent e7b4930 commit ccbb84d

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Bug fixes
5959
By `Jimmy Westling <https://github.com/illviljan>`_.
6060
- Improved performance in ``open_dataset`` for datasets with large object arrays (:issue:`7484`, :pull:`7494`).
6161
By `Alex Goodman <https://github.com/agoodm>`_ and `Deepak Cherian <https://github.com/dcherian>`_.
62+
- Fix :py:meth:`DataArray.plot.pcolormesh` which now works if one of the coordinates has str dtype (:issue:`6775`, :pull:`7612`).
63+
By `Michael Niklas <https://github.com/headtr1ck>`_.
6264

6365
Documentation
6466
~~~~~~~~~~~~~

xarray/plot/dataarray_plot.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -2293,27 +2293,23 @@ def pcolormesh(
22932293
else:
22942294
infer_intervals = True
22952295

2296-
if (
2297-
infer_intervals
2298-
and not np.issubdtype(x.dtype, str)
2299-
and (
2300-
(np.shape(x)[0] == np.shape(z)[1])
2301-
or ((x.ndim > 1) and (np.shape(x)[1] == np.shape(z)[1]))
2302-
)
2296+
if any(np.issubdtype(k.dtype, str) for k in (x, y)):
2297+
# do not infer intervals if any axis contains str ticks, see #6775
2298+
infer_intervals = False
2299+
2300+
if infer_intervals and (
2301+
(np.shape(x)[0] == np.shape(z)[1])
2302+
or ((x.ndim > 1) and (np.shape(x)[1] == np.shape(z)[1]))
23032303
):
2304-
if len(x.shape) == 1:
2304+
if x.ndim == 1:
23052305
x = _infer_interval_breaks(x, check_monotonic=True, scale=xscale)
23062306
else:
23072307
# we have to infer the intervals on both axes
23082308
x = _infer_interval_breaks(x, axis=1, scale=xscale)
23092309
x = _infer_interval_breaks(x, axis=0, scale=xscale)
23102310

2311-
if (
2312-
infer_intervals
2313-
and not np.issubdtype(y.dtype, str)
2314-
and (np.shape(y)[0] == np.shape(z)[0])
2315-
):
2316-
if len(y.shape) == 1:
2311+
if infer_intervals and (np.shape(y)[0] == np.shape(z)[0]):
2312+
if y.ndim == 1:
23172313
y = _infer_interval_breaks(y, check_monotonic=True, scale=yscale)
23182314
else:
23192315
# we have to infer the intervals on both axes

xarray/tests/test_plot.py

+10
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ def test2d_1d_2d_coordinates_pcolormesh(self) -> None:
427427
_, unique_counts = np.unique(v[:-1], axis=0, return_counts=True)
428428
assert np.all(unique_counts == 1)
429429

430+
def test_str_coordinates_pcolormesh(self) -> None:
431+
# test for #6775
432+
x = DataArray(
433+
[[1, 2, 3], [4, 5, 6]],
434+
dims=("a", "b"),
435+
coords={"a": [1, 2], "b": ["a", "b", "c"]},
436+
)
437+
x.plot.pcolormesh()
438+
x.T.plot.pcolormesh()
439+
430440
def test_contourf_cmap_set(self) -> None:
431441
a = DataArray(easy_array((4, 4)), dims=["z", "time"])
432442

0 commit comments

Comments
 (0)