From 703ea1d6db76313e37fd2f4c955598f05881e8bf Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:56:08 +0200 Subject: [PATCH 1/6] Fix typing for test_plot.py --- xarray/tests/test_plot.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index fa08e9975ab..e114f6f0d3d 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -158,9 +158,10 @@ def setup(self) -> Generator: plt.close("all") def pass_in_axis(self, plotmethod, subplot_kw=None) -> None: - fig, axs = plt.subplots(ncols=2, subplot_kw=subplot_kw) - plotmethod(ax=axs[0]) - assert axs[0].has_data() + fig, axs = plt.subplots(ncols=2, subplot_kw=subplot_kw, squeeze=False) + ax = axs[0, 0] + plotmethod(ax=ax) + assert ax.has_data() @pytest.mark.slow def imshow_called(self, plotmethod) -> bool: @@ -240,9 +241,9 @@ def test_1d_x_y_kw(self) -> None: xy: list[list[None | str]] = [[None, None], [None, "z"], ["z", None]] - f, ax = plt.subplots(3, 1) + f, axs = plt.subplots(3, 1, squeeze=False) for aa, (x, y) in enumerate(xy): - da.plot(x=x, y=y, ax=ax.flat[aa]) + da.plot(x=x, y=y, ax=axs.flat[aa]) with pytest.raises(ValueError, match=r"Cannot specify both"): da.plot(x="z", y="z") @@ -1566,7 +1567,9 @@ def test_colorbar_kwargs(self) -> None: assert "MyLabel" in alltxt assert "testvar" not in alltxt # change cbar ax - fig, (ax, cax) = plt.subplots(1, 2) + fig, axs = plt.subplots(1, 2, squeeze=False) + ax = axs[0, 0] + cax = axs[0, 1] self.plotmethod( ax=ax, cbar_ax=cax, add_colorbar=True, cbar_kwargs={"label": "MyBar"} ) @@ -1576,7 +1579,9 @@ def test_colorbar_kwargs(self) -> None: assert "MyBar" in alltxt assert "testvar" not in alltxt # note that there are two ways to achieve this - fig, (ax, cax) = plt.subplots(1, 2) + fig, axs = plt.subplots(1, 2, squeeze=False) + ax = axs[0, 0] + cax = axs[0, 1] self.plotmethod( ax=ax, add_colorbar=True, cbar_kwargs={"label": "MyBar", "cax": cax} ) From 08208829d6dee1dbcdad269418eeb5f448eedb39 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:06:37 +0200 Subject: [PATCH 2/6] Update test_plot.py --- xarray/tests/test_plot.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index e114f6f0d3d..3bd3bbb0328 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -3376,16 +3376,16 @@ def test_plot1d_default_rcparams() -> None: # see overlapping markers: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax) - np.testing.assert_allclose( - ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("w") - ) + actual: np.ndarray = mpl.colors.to_rgba_array("w") + expected: np.ndarray = ax.collections[0].get_edgecolor() + np.testing.assert_allclose(actual, expected) # Facetgrids should have the default value as well: fg = ds.plot.scatter(x="A", y="B", col="x", marker="o") ax = fg.axs.ravel()[0] - np.testing.assert_allclose( - ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("w") - ) + actual = mpl.colors.to_rgba_array("w") + expected = ax.collections[0].get_edgecolor() + np.testing.assert_allclose(actual, expected) # scatter should not emit any warnings when using unfilled markers: with assert_no_warnings(): @@ -3395,9 +3395,9 @@ def test_plot1d_default_rcparams() -> None: # Prioritize edgecolor argument over default plot1d values: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax, edgecolor="k") - np.testing.assert_allclose( - ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("k") - ) + actual = mpl.colors.to_rgba_array("k") + expected = ax.collections[0].get_edgecolor() + np.testing.assert_allclose(actual, expected) @requires_matplotlib From 621713a9ffe25390e3ddf4b775b34a7be54f9449 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:17:47 +0200 Subject: [PATCH 3/6] make sure we actually get ndarrays here, I get it locally at least --- xarray/tests/test_plot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 3bd3bbb0328..5e75260c457 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -3377,7 +3377,9 @@ def test_plot1d_default_rcparams() -> None: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax) actual: np.ndarray = mpl.colors.to_rgba_array("w") + assert isinstance(actual, np.ndarray) expected: np.ndarray = ax.collections[0].get_edgecolor() + assert isinstance(expected, np.ndarray) np.testing.assert_allclose(actual, expected) # Facetgrids should have the default value as well: From e9e94ece3d431e6e0edeb6d29b35e7cf528061eb Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:31:25 +0200 Subject: [PATCH 4/6] Add a minimal test and ignore in real test --- xarray/tests/test_plot.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 5e75260c457..9926a118210 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -3365,6 +3365,20 @@ def test_facetgrid_axes_raises_deprecation_warning() -> None: g.axes +@requires_matplotlib +def test_debug() -> None: + import matplotlib as mpl + + with figure_context(): + fig, ax = plt.subplots(1, 1) + ax.scatter(x=np.array([1, 2, 3]), y=np.array([6, 7, 8]), color="k") + actual: np.ndarray = mpl.colors.to_rgba_array("k") + assert isinstance(actual, np.ndarray) + expected: np.ndarray = ax.collections[0].get_edgecolor() + assert isinstance(expected, np.ndarray) + np.testing.assert_allclose(actual, expected) + + @requires_matplotlib def test_plot1d_default_rcparams() -> None: import matplotlib as mpl @@ -3376,17 +3390,15 @@ def test_plot1d_default_rcparams() -> None: # see overlapping markers: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax) - actual: np.ndarray = mpl.colors.to_rgba_array("w") - assert isinstance(actual, np.ndarray) - expected: np.ndarray = ax.collections[0].get_edgecolor() - assert isinstance(expected, np.ndarray) + actual: np.ndarray = mpl.colors.to_rgba_array("w") # type: ignore[assignment] # mpl error? + expected: np.ndarray = ax.collections[0].get_edgecolor() # type: ignore[assignment] # mpl error? np.testing.assert_allclose(actual, expected) # Facetgrids should have the default value as well: fg = ds.plot.scatter(x="A", y="B", col="x", marker="o") ax = fg.axs.ravel()[0] - actual = mpl.colors.to_rgba_array("w") - expected = ax.collections[0].get_edgecolor() + actual = mpl.colors.to_rgba_array("w") # type: ignore[assignment] # mpl error? + expected = ax.collections[0].get_edgecolor() # type: ignore[assignment] # mpl error? np.testing.assert_allclose(actual, expected) # scatter should not emit any warnings when using unfilled markers: @@ -3397,8 +3409,8 @@ def test_plot1d_default_rcparams() -> None: # Prioritize edgecolor argument over default plot1d values: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax, edgecolor="k") - actual = mpl.colors.to_rgba_array("k") - expected = ax.collections[0].get_edgecolor() + actual = mpl.colors.to_rgba_array("k") # type: ignore[assignment] # mpl error? + expected = ax.collections[0].get_edgecolor() # type: ignore[assignment] # mpl error? np.testing.assert_allclose(actual, expected) From f3a0890b555f55cd7bb986a9837a88df39484574 Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:35:23 +0200 Subject: [PATCH 5/6] Update test_plot.py --- xarray/tests/test_plot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 9926a118210..afcf9d9d58a 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -3390,14 +3390,14 @@ def test_plot1d_default_rcparams() -> None: # see overlapping markers: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax) - actual: np.ndarray = mpl.colors.to_rgba_array("w") # type: ignore[assignment] # mpl error? + actual: np.ndarray = mpl.colors.to_rgba_array("w") expected: np.ndarray = ax.collections[0].get_edgecolor() # type: ignore[assignment] # mpl error? np.testing.assert_allclose(actual, expected) # Facetgrids should have the default value as well: fg = ds.plot.scatter(x="A", y="B", col="x", marker="o") ax = fg.axs.ravel()[0] - actual = mpl.colors.to_rgba_array("w") # type: ignore[assignment] # mpl error? + actual = mpl.colors.to_rgba_array("w") expected = ax.collections[0].get_edgecolor() # type: ignore[assignment] # mpl error? np.testing.assert_allclose(actual, expected) @@ -3409,7 +3409,7 @@ def test_plot1d_default_rcparams() -> None: # Prioritize edgecolor argument over default plot1d values: fig, ax = plt.subplots(1, 1) ds.plot.scatter(x="A", y="B", marker="o", ax=ax, edgecolor="k") - actual = mpl.colors.to_rgba_array("k") # type: ignore[assignment] # mpl error? + actual = mpl.colors.to_rgba_array("k") expected = ax.collections[0].get_edgecolor() # type: ignore[assignment] # mpl error? np.testing.assert_allclose(actual, expected) From e1e80ecf4bc75e3471637c38b73e851828e6f47d Mon Sep 17 00:00:00 2001 From: Illviljan <14371165+Illviljan@users.noreply.github.com> Date: Thu, 11 Jul 2024 14:54:41 +0200 Subject: [PATCH 6/6] Update test_plot.py --- xarray/tests/test_plot.py | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index afcf9d9d58a..578e6bcc18e 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -3365,20 +3365,6 @@ def test_facetgrid_axes_raises_deprecation_warning() -> None: g.axes -@requires_matplotlib -def test_debug() -> None: - import matplotlib as mpl - - with figure_context(): - fig, ax = plt.subplots(1, 1) - ax.scatter(x=np.array([1, 2, 3]), y=np.array([6, 7, 8]), color="k") - actual: np.ndarray = mpl.colors.to_rgba_array("k") - assert isinstance(actual, np.ndarray) - expected: np.ndarray = ax.collections[0].get_edgecolor() - assert isinstance(expected, np.ndarray) - np.testing.assert_allclose(actual, expected) - - @requires_matplotlib def test_plot1d_default_rcparams() -> None: import matplotlib as mpl