diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 86c2106a796..b09e797c55a 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -47,7 +47,8 @@ Bug fixes By `Mathias Hauser `_. - Fix grouped and resampled ``first``, ``last`` with datetimes (:issue:`10169`, :pull:`10173`) By `Deepak Cherian `_. - +- FacetGrid plots now include units in their axis labels when available (:issue:`10184`, :pull:`10185`) + By `Andre Wendlinger `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/plot/facetgrid.py b/xarray/plot/facetgrid.py index 82105d5fb6a..2c50c5e1176 100644 --- a/xarray/plot/facetgrid.py +++ b/xarray/plot/facetgrid.py @@ -353,6 +353,8 @@ def map_dataarray( if k not in {"cmap", "colors", "cbar_kwargs", "levels"} } func_kwargs.update(cmap_params) + # to avoid redundant calling, colorbar and labelling is instead handled + # by `_finalize_grid` at the end func_kwargs["add_colorbar"] = False if func.__name__ != "surface": func_kwargs["add_labels"] = False @@ -375,7 +377,10 @@ def map_dataarray( ) self._mappables.append(mappable) - self._finalize_grid(x, y) + xlabel = label_from_attrs(self.data[x]) + ylabel = label_from_attrs(self.data[y]) + + self._finalize_grid(xlabel, ylabel) if kwargs.get("add_colorbar", True): self.add_colorbar(**cbar_kwargs) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index b9adda5b52d..bfa87386dbc 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2429,6 +2429,26 @@ def test_facetgrid_polar(self) -> None: col="z", subplot_kws=dict(projection="polar"), sharex=False, sharey=False ) + @pytest.mark.slow + def test_units_appear_somewhere(self) -> None: + # assign coordinates to all dims so we can test for units + darray = self.darray.assign_coords( + {"x": np.arange(self.darray.x.size), "y": np.arange(self.darray.y.size)} + ) + + darray.x.attrs["units"] = "x_unit" + darray.y.attrs["units"] = "y_unit" + + g = xplt.FacetGrid(darray, col="z") + + g.map_dataarray(xplt.contourf, "x", "y") + + alltxt = text_in_fig() + + # unit should appear as e.g. 'x [x_unit]' + for unit_name in ["x_unit", "y_unit"]: + assert unit_name in "".join(alltxt) + @pytest.mark.filterwarnings("ignore:tight_layout cannot") class TestFacetGrid4d(PlotTestCase): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy