Skip to content

Commit fa882fc

Browse files
authored
Better tests and fix .cf.plot() (#11)
* Add proper plotting tests * xfail coarsen tests. See xarray GH4120. Coarsen reductions modify attrs on the original object, so this needs to be last to prevent other tests from failing. This test will also fail. * fix .cf.plot(...) * slightly nicer
1 parent 25a063c commit fa882fc

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

cf_xarray/accessor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ def __init__(self, obj, accessor):
8787
self._keys = ("x", "y", "hue", "col", "row")
8888

8989
def __call__(self, *args, **kwargs):
90-
return _getattr(
90+
plot = _getattr(
9191
obj=self._obj, attr="plot", accessor=self.accessor, keys=self._keys
9292
)
93+
return plot(*args, **kwargs)
9394

9495
def __getattr__(self, attr):
9596
return _getattr(

cf_xarray/tests/test_accessor.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@
2121
(
2222
("resample", {"time": "M"}, {"T": "M"}),
2323
("rolling", {"lat": 5}, {"Y": 5}),
24-
("coarsen", {"lon": 2, "lat": 5}, {"X": 2, "Y": 5}),
25-
("groupby", {"group": "time"}, {"group": "T"})
24+
("groupby", {"group": "time"}, {"group": "T"}),
25+
pytest.param(
26+
"coarsen",
27+
{"lon": 2, "lat": 5},
28+
{"X": 2, "Y": 5},
29+
marks=pytest.mark.skip(
30+
reason="xarray GH4120. any test after this will fail since attrs are lost"
31+
),
32+
),
33+
# order of above tests is important: See xarray GH4120
2634
# groupby("time.day")?
2735
# groupby_bins
2836
# weighted
@@ -68,19 +76,24 @@ def test_args_methods(obj):
6876
@pytest.mark.parametrize("obj", dataarrays)
6977
def test_dataarray_plot(obj):
7078

71-
obj.isel(time=1).cf.plot(x="X", y="Y")
79+
rv = obj.isel(time=1).cf.plot(x="X", y="Y")
80+
assert isinstance(rv, mpl.collections.QuadMesh)
7281
plt.close()
7382

74-
obj.isel(time=1).cf.plot.contourf(x="X", y="Y")
83+
rv = obj.isel(time=1).cf.plot.contourf(x="X", y="Y")
84+
assert isinstance(rv, mpl.contour.QuadContourSet)
7585
plt.close()
7686

77-
obj.cf.plot(x="X", y="Y", col="T")
87+
rv = obj.cf.plot(x="X", y="Y", col="T")
88+
assert isinstance(rv, xr.plot.FacetGrid)
7889
plt.close()
7990

80-
obj.cf.plot.contourf(x="X", y="Y", col="T")
91+
rv = obj.cf.plot.contourf(x="X", y="Y", col="T")
92+
assert isinstance(rv, xr.plot.FacetGrid)
8193
plt.close()
8294

83-
obj.isel(lat=[0, 1], lon=1).cf.plot.line(x="T", hue="Y")
95+
rv = obj.isel(lat=[0, 1], lon=1).cf.plot.line(x="T", hue="Y")
96+
assert all([isinstance(line, mpl.lines.Line2D) for line in rv])
8497
plt.close()
8598

8699

0 commit comments

Comments
 (0)