8000 Fix `pcolormesh` with str coords by headtr1ck · Pull Request #7612 · pydata/xarray · GitHub
[go: up one dir, main page]

Skip to content

Fix pcolormesh with str coords #7612

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

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
pcolormesh with str coords
  • Loading branch information
headtr1ck committed Mar 12, 2023
commit d4847c9f2253543693d9c8bf1b0d4a16b51e8e6b
25 changes: 11 additions & 14 deletions xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,27 +2293,24 @@ def pcolormesh(
else:
infer_intervals = True

if (
infer_intervals
and not np.issubdtype(x.dtype, str)
and (
(np.shape(x)[0] == np.shape(z)[1])
or ((x.ndim > 1) and (np.shape(x)[1] == np.shape(z)[1]))
)
if any(np.issubdtype(k.dtype, str) for k in (x, y)):
# do not infer intervals if any axis contains str ticks
# see https://github.com/pydata/xarray/issues/6775
infer_intervals = False

if infer_intervals and (
(np.shape(x)[0] == np.shape(z)[1])
or ((x.ndim > 1) and (np.shape(x)[1] == np.shape(z)[1]))
):
if len(x.shape) == 1:
if x.ndim == 1:
x = _infer_interval_breaks(x, check_monotonic=True, scale=xscale)
else:
# we have to infer the intervals on both axes
x = _infer_interval_breaks(x, axis=1, scale=xscale)
x = _infer_interval_breaks(x, axis=0, scale=xscale)

if (
infer_intervals
and not np.issubdtype(y.dtype, str)
and (np.shape(y)[0] == np.shape(z)[0])
):
if len(y.shape) == 1:
if infer_intervals and (np.shape(y)[0] == np.shape(z)[0]):
if y.ndim == 1:
y = _infer_interval_breaks(y, check_monotonic=True, scale=yscale)
else:
# we have to infer the intervals on both axes
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,16 @@ def test2d_1d_2d_coordinates_pcolormesh(self) -> None:
_, unique_counts = np.unique(v[:-1], axis=0, return_counts=True)
assert np.all(unique_counts == 1)

def test_str_coordinates_pcolormesh(self) -> None:
# test for #6775
x = DataArray(
[[1, 2, 3], [4, 5, 6]],
dims=("a", "b"),
coords={"a": [1, 2], "b": ["a", "b", "c"]},
)
x.plot.pcolormesh()
x.T.plot.pcolormesh()

def test_contourf_cmap_set(self) -> None:
a = DataArray(easy_array((4, 4)), dims=["z", "time"])

Expand Down
0