Skip to content

Commit 61d4e26

Browse files
Fix: Correct axis labelling with units for FacetGrid plots (#10185)
* Add test to check units appear in FacetGrid plot - appended test to `TestFacetGrid` class inside test_plot.py - checks that units are added to the plot axis labelling * fix: ensure axis labels include units in FacetGrid plots - Fixed an issue where axis labels for FacetGrid plots did not display units when provided. - Now, both the dimension name and its corresponding unit (if available) are shown on the axis label. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * added whats-new documentation --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d008e33 commit 61d4e26

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Bug fixes
4747
By `Mathias Hauser <https://github.com/mathause>`_.
4848
- Fix grouped and resampled ``first``, ``last`` with datetimes (:issue:`10169`, :pull:`10173`)
4949
By `Deepak Cherian <https://github.com/dcherian>`_.
50-
50+
- FacetGrid plots now include units in their axis labels when available (:issue:`10184`, :pull:`10185`)
51+
By `Andre Wendlinger <https://github.com/andrewendlinger>`_.
5152

5253
Documentation
5354
~~~~~~~~~~~~~

xarray/plot/facetgrid.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def map_dataarray(
353353
if k not in {"cmap", "colors", "cbar_kwargs", "levels"}
354354
}
355355
func_kwargs.update(cmap_params)
356+
# to avoid redundant calling, colorbar and labelling is instead handled
357+
# by `_finalize_grid` at the end
356358
func_kwargs["add_colorbar"] = False
357359
if func.__name__ != "surface":
358360
func_kwargs["add_labels"] = False
@@ -375,7 +377,10 @@ def map_dataarray(
375377
)
376378
self._mappables.append(mappable)
377379

378-
self._finalize_grid(x, y)
380+
xlabel = label_from_attrs(self.data[x])
381+
ylabel = label_from_attrs(self.data[y])
382+
383+
self._finalize_grid(xlabel, ylabel)
379384

380385
if kwargs.get("add_colorbar", True):
381386
self.add_colorbar(**cbar_kwargs)

xarray/tests/test_plot.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,26 @@ def test_facetgrid_polar(self) -> None:
24292429
col="z", subplot_kws=dict(projection="polar"), sharex=False, sharey=False
24302430
)
24312431

2432+
@pytest.mark.slow
2433+
def test_units_appear_somewhere(self) -> None:
2434+
# assign coordinates to all dims so we can test for units
2435+
darray = self.darray.assign_coords(
2436+
{"x": np.arange(self.darray.x.size), "y": np.arange(self.darray.y.size)}
2437+
)
2438+
2439+
darray.x.attrs["units"] = "x_unit"
2440+
darray.y.attrs["units"] = "y_unit"
2441+
2442+
g = xplt.FacetGrid(darray, col="z")
2443+
2444+
g.map_dataarray(xplt.contourf, "x", "y")
2445+
2446+
alltxt = text_in_fig()
2447+
2448+
# unit should appear as e.g. 'x [x_unit]'
2449+
for unit_name in ["x_unit", "y_unit"]:
2450+
assert unit_name in "".join(alltxt)
2451+
24322452

24332453
@pytest.mark.filterwarnings("ignore:tight_layout cannot")
24342454
class TestFacetGrid4d(PlotTestCase):

0 commit comments

Comments
 (0)