8000 Fix: Correct axis labelling with units for FacetGrid plots by andrewendlinger · Pull Request #10185 · pydata/xarray · GitHub
[go: up one dir, main page]

Skip to content

Fix: Correct axis labelling with units for FacetGrid plots #10185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 8000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Bug fixes
By `Mathias Hauser <https://github.com/mathause>`_.
- Fix grouped and resampled ``first``, ``last`` with datetimes (:issue:`10169`, :pull:`10173`)
By `Deepak Cherian <https://github.com/dcherian>`_.

- FacetGrid plots now include units in their axis labels when available (:issue:`10184`, :pull:`10185`)
By `Andre Wendlinger <https://github.com/andrewendlinger>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
7 changes: 6 additions & 1 deletion xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading
0