From c439432ff89dcdabc8ab38a6ceecf540b034311d Mon Sep 17 00:00:00 2001 From: wkrasnicki <38408316+wkrasnicki@users.noreply.github.com> Date: Sun, 17 Sep 2023 07:08:05 +0200 Subject: [PATCH] Exclude dimensions used in faceting from squeeze (#8174) * Exclude dimensions used in faceting from squeeze * Add unit test for facetting singleton dim * Add bug fix to changelog * Move test to proper class * Update doc/whats-new.rst * Apply suggestions from code review * Update doc/whats-new.rst --------- Co-authored-by: Mathias Hauser Co-authored-by: Deepak Cherian --- doc/whats-new.rst | 3 +++ xarray/plot/dataarray_plot.py | 4 +++- xarray/tests/test_plot.py | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 04b06fff221..863451b6bd0 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -73,6 +73,9 @@ Bug fixes special case ``NaT`` handling in :py:meth:`~core.accessor_dt.DatetimeAccessor.isocalendar()` (:issue:`7928`, :pull:`8084`). By `Kai Mühlbauer `_. +- Calling plot with kwargs ``col``, ``row`` or ``hue`` no longer squeezes dimensions passed via these arguments + (:issue:`7552`, :pull:`8174`). + By `Wiktor Kraśnicki `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index 8afd87ea64a..54f3c657b6c 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -264,7 +264,9 @@ def plot( -------- xarray.DataArray.squeeze """ - darray = darray.squeeze().compute() + darray = darray.squeeze( + d for d, s in darray.sizes.items() if s == 1 and d not in (row, col, hue) + ).compute() plot_dims = set(darray.dims) plot_dims.discard(row) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index d80445af329..6cc061a7ee5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -729,6 +729,13 @@ def test_labels_with_units_with_interval(self, dim) -> None: expected = "dim_0_bins_center [m]" assert actual == expected + def test_multiplot_over_length_one_dim(self) -> None: + a = easy_array((3, 1, 1, 1)) + d = DataArray(a, dims=("x", "col", "row", "hue")) + d.plot(col="col") + d.plot(row="row") + d.plot(hue="hue") + class TestPlot1D(PlotTestCase): @pytest.fixture(autouse=True)