|
5 | 5 | import pandas as pd
|
6 | 6 | import pytest
|
7 | 7 |
|
| 8 | +try: |
| 9 | + import matplotlib.pyplot as plt |
| 10 | +except ImportError: |
| 11 | + pass |
| 12 | + |
8 | 13 | import xarray as xr
|
9 | 14 | from xarray.core import dtypes, duck_array_ops
|
10 | 15 |
|
11 |
| -from . import assert_allclose, assert_duckarray_allclose, assert_equal, assert_identical |
| 16 | +from . import ( |
| 17 | + assert_allclose, |
| 18 | + assert_duckarray_allclose, |
| 19 | + assert_equal, |
| 20 | + assert_identical, |
| 21 | + requires_matplotlib, |
| 22 | +) |
| 23 | +from .test_plot import PlotTestCase |
12 | 24 | from .test_variable import _PAD_XR_NP_ARGS
|
13 | 25 |
|
14 | 26 | pint = pytest.importorskip("pint")
|
@@ -5564,3 +5576,29 @@ def test_merge(self, variant, unit, error, dtype):
|
5564 | 5576 |
|
5565 | 5577 | assert_units_equal(expected, actual)
|
5566 | 5578 | assert_equal(expected, actual)
|
| 5579 | + |
| 5580 | + |
| 5581 | +@requires_matplotlib |
| 5582 | +class TestPlots(PlotTestCase): |
| 5583 | + def test_units_in_line_plot_labels(self): |
| 5584 | + arr = np.linspace(1, 10, 3) * unit_registry.Pa |
| 5585 | + # TODO make coord a Quantity once unit-aware indexes supported |
| 5586 | + x_coord = xr.DataArray( |
| 5587 | + np.linspace(1, 3, 3), dims="x", attrs={"units": "meters"} |
| 5588 | + ) |
| 5589 | + da = xr.DataArray(data=arr, dims="x", coords={"x": x_coord}, name="pressure") |
| 5590 | + |
| 5591 | + da.plot.line() |
| 5592 | + |
| 5593 | + ax = plt.gca() |
| 5594 | + assert ax.get_ylabel() == "pressure [pascal]" |
| 5595 | + assert ax.get_xlabel() == "x [meters]" |
| 5596 | + |
| 5597 | + def test_units_in_2d_plot_labels(self): |
| 5598 | + arr = np.ones((2, 3)) * unit_registry.Pa |
| 5599 | + da = xr.DataArray(data=arr, dims=["x", "y"], name="pressure") |
| 5600 | + |
| 5601 | + fig, (ax, cax) = plt.subplots(1, 2) |
| 5602 | + ax = da.plot.contourf(ax=ax, cbar_ax=cax, add_colorbar=True) |
| 5603 | + |
| 5604 | + assert cax.get_ylabel() == "pressure [pascal]" |
0 commit comments