diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 0128e70caed..f499cbe3d21 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -34,7 +34,8 @@ Deprecations Bug fixes ~~~~~~~~~ - +- Fix plot.line crash for data of shape ``(1, N)`` in _title_for_slice on format_item (:pull:`5948`). + By `Sebastian Weigand `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/formatting.py b/xarray/core/formatting.py index ab1cde860f9..3f65cce4f68 100644 --- a/xarray/core/formatting.py +++ b/xarray/core/formatting.py @@ -143,7 +143,7 @@ def format_item(x, timedelta_format=None, quote_strings=True): elif isinstance(x, (str, bytes)): return repr(x) if quote_strings else x elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating): - return f"{x:.4}" + return f"{x.item():.4}" else: return str(x) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 774f90dbb04..0052178ad68 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -754,6 +754,13 @@ def test_slice_in_title(self): title = plt.gca().get_title() assert "d = 10.01" == title + def test_slice_in_title_single_item_array(self): + """Edge case for data of shape (1, N) or (N, 1).""" + darray = self.darray.expand_dims({"d": np.array([10.009])}) + darray.plot.line(x="period") + title = plt.gca().get_title() + assert "d = 10.01" == title + class TestPlotStep(PlotTestCase): @pytest.fixture(autouse=True)