Skip to content

Commit d68c013

Browse files
authored
Fix .plot error when using positional args with col and row (#11111)
* Ensure plotting returns even with some positional args * Test that warnings and errors happen * Don't allow positional args in scatter since they don't work
1 parent 7e11188 commit d68c013

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ Deprecations
2626
Bug Fixes
2727
~~~~~~~~~
2828

29+
- Fix ``.plot`` error when using positional args with ``col`` and
30+
``row`` (:issue:`11104` :pull:`11111`).
31+
By `Julia Signell <https://github.com/jsignell>`_.
2932
- Slightly amend `Xarray's Zarr Encoding Specification doc <https://docs.xarray.dev/en/latest/internals/zarr-encoding-spec.html>`_ for clarity, and provide a code comment in ``xarray.backends.zarr._get_zarr_dims_and_attrs`` referencing the doc (:issue:`8749` :pull:`11013`).
3033
By `Ewan Short <https://github.com/eshort0401>`_.
3134
- Fix silent data corruption when writing dask arrays to sharded Zarr stores.
3235
Dask chunk boundaries must now align with shard boundaries, not just internal
3336
Zarr chunk boundaries (:issue:`10831`).
3437

35-
3638
Documentation
3739
~~~~~~~~~~~~~
3840

xarray/plot/dataarray_plot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def step(
641641

642642
def hist(
643643
darray: DataArray,
644-
*args: Any,
644+
*,
645645
figsize: Iterable[float] | None = None,
646646
size: float | None = None,
647647
aspect: AspectOptions = None,
@@ -695,8 +695,6 @@ def hist(
695695
Additional keyword arguments to :py:func:`matplotlib:matplotlib.pyplot.hist`.
696696
697697
"""
698-
assert len(args) == 0
699-
700698
if darray.ndim == 0 or darray.size == 0:
701699
# TypeError to be consistent with pandas
702700
raise TypeError("No numeric data to plot.")
@@ -928,6 +926,7 @@ def newplotfunc(
928926
raise ValueError(msg)
929927
else:
930928
warnings.warn(msg, DeprecationWarning, stacklevel=2)
929+
del msg
931930
del args
932931

933932
if hue_style is not None:
@@ -1461,6 +1460,7 @@ def newplotfunc(
14611460
raise ValueError(msg)
14621461
else:
14631462
warnings.warn(msg, DeprecationWarning, stacklevel=2)
1463+
del msg
14641464
del args
14651465

14661466
# Decide on a default for the colorbar before facetgrids

xarray/plot/dataset_plot.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def newplotfunc(
213213
raise ValueError(msg)
214214
else:
215215
warnings.warn(msg, DeprecationWarning, stacklevel=2)
216+
del msg
216217
del args
217218

218219
_is_facetgrid = kwargs.pop("_is_facetgrid", False)
@@ -752,7 +753,7 @@ def _temp_dataarray(ds: Dataset, y: Hashable, locals_: dict[str, Any]) -> DataAr
752753
@overload
753754
def scatter( # type: ignore[misc,unused-ignore] # None is hashable :(
754755
ds: Dataset,
755-
*args: Any,
756+
*,
756757
x: Hashable | None = None,
757758
y: Hashable | None = None,
758759
z: Hashable | None = None,
@@ -793,7 +794,7 @@ def scatter( # type: ignore[misc,unused-ignore] # None is hashable :(
793794
@overload
794795
def scatter(
795796
ds: Dataset,
796-
*args: Any,
797+
*,
797798
x: Hashable | None = None,
798799
y: Hashable | None = None,
799800
z: Hashable | None = None,
@@ -834,7 +835,7 @@ def scatter(
834835
@overload
835836
def scatter(
836837
ds: Dataset,
837-
*args: Any,
838+
*,
838839
x: Hashable | None = None,
839840
y: Hashable | None = None,
840841
z: Hashable | None = None,
@@ -875,7 +876,7 @@ def scatter(
875876
@_update_doc_to_dataset(dataarray_plot.scatter)
876877
def scatter(
877878
ds: Dataset,
878-
*args: Any,
879+
*,
879880
x: Hashable | None = None,
880881
y: Hashable | None = None,
881882
z: Hashable | None = None,

xarray/tests/test_plot.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,14 @@ def test_slice_in_title_single_item_array(self) -> None:
834834
title = plt.gca().get_title()
835835
assert "d = [10.009]" == title
836836

837+
def test_warns_for_few_positional_args(self) -> None:
838+
with pytest.warns(DeprecationWarning, match="Using positional arguments"):
839+
self.darray.plot.scatter("period")
840+
841+
def test_raises_for_too_many_positional_args(self) -> None:
842+
with pytest.raises(ValueError, match="Using positional arguments"):
843+
self.darray.plot.scatter("period", "foo", "bar", "blue", {})
844+
837845

838846
class TestPlotStep(PlotTestCase):
839847
@pytest.fixture(autouse=True)
@@ -1730,6 +1738,19 @@ def test_colormap_error_norm_and_vmin_vmax(self) -> None:
17301738
with pytest.raises(ValueError):
17311739
self.darray.plot(norm=norm, vmax=2) # type: ignore[call-arg]
17321740

1741+
def test_plot_warns_for_2_positional_args(self) -> None:
1742+
da = xr.DataArray(
1743+
np.random.randn(2, 6, 6),
1744+
dims=("time", "x", "y"),
1745+
coords={"x": np.arange(6), "y": np.arange(6)},
1746+
)
1747+
with pytest.warns(DeprecationWarning, match="Using positional arguments"):
1748+
self.plotfunc(da, "x", "y", col="time")
1749+
1750+
def test_plot_raises_too_many_for_positional_args(self) -> None:
1751+
with pytest.raises(ValueError, match="Using positional arguments"):
1752+
self.plotmethod("x", "y", (12, 4))
1753+
17331754

17341755
@pytest.mark.slow
17351756
class TestContourf(Common2dMixin, PlotTestCase):
@@ -2890,6 +2911,10 @@ def test_bad_args(
28902911
x=x, y=y, hue=hue, add_legend=add_legend, add_colorbar=add_colorbar
28912912
)
28922913

2914+
def test_does_not_allow_positional_args(self) -> None:
2915+
with pytest.raises(TypeError, match="takes 1 positional argument"):
2916+
self.ds.plot.scatter("A", "B")
2917+
28932918
def test_datetime_hue(self) -> None:
28942919
ds2 = self.ds.copy()
28952920

0 commit comments

Comments
 (0)