Skip to content

Commit c92102b

Browse files
Rename FacetGrid.axes to FacetGrid.axs in tests (#7218)
* Rename FacetGrid.axes to FacetGrid.axs * Update test_units.py * Add warning test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b9aedd0 commit c92102b

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
lines changed

xarray/tests/test_plot.py

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ def setup(self):
152152
plt.close("all")
153153

154154
def pass_in_axis(self, plotmethod, subplot_kw=None):
155-
fig, axes = plt.subplots(ncols=2, subplot_kw=subplot_kw)
156-
plotmethod(ax=axes[0])
157-
assert axes[0].has_data()
155+
fig, axs = plt.subplots(ncols=2, subplot_kw=subplot_kw)
156+
plotmethod(ax=axs[0])
157+
assert axs[0].has_data()
158158

159159
@pytest.mark.slow
160160
def imshow_called(self, plotmethod):
@@ -602,8 +602,8 @@ def test_convenient_facetgrid(self) -> None:
602602
d.coords["z"] = list("abcd")
603603
g = d.plot(x="x", y="y", col="z", col_wrap=2, cmap="cool")
604604

605-
assert_array_equal(g.axes.shape, [2, 2])
606-
for ax in g.axes.flat:
605+
assert_array_equal(g.axs.shape, [2, 2])
606+
for ax in g.axs.flat:
607607
assert ax.has_data()
608608

609609
with pytest.raises(ValueError, match=r"[Ff]acet"):
@@ -625,7 +625,7 @@ def test_subplot_kws(self) -> None:
625625
cmap="cool",
626626
subplot_kws=dict(facecolor="r"),
627627
)
628-
for ax in g.axes.flat:
628+
for ax in g.axs.flat:
629629
# mpl V2
630630
assert ax.get_facecolor()[0:3] == mpl.colors.to_rgb("r")
631631

@@ -662,8 +662,8 @@ def test_convenient_facetgrid_4d(self) -> None:
662662
d = DataArray(a, dims=["y", "x", "columns", "rows"])
663663
g = d.plot(x="x", y="y", col="columns", row="rows")
664664

665-
assert_array_equal(g.axes.shape, [3, 2])
666-
for ax in g.axes.flat:
665+
assert_array_equal(g.axs.shape, [3, 2])
666+
for ax in g.axs.flat:
667667
assert ax.has_data()
668668

669669
with pytest.raises(ValueError, match=r"[Ff]acet"):
@@ -1529,7 +1529,7 @@ def test_verbose_facetgrid(self) -> None:
15291529
d = DataArray(a, dims=["y", "x", "z"])
15301530
g = xplt.FacetGrid(d, col="z", subplot_kws=self.subplot_kws)
15311531
g.map_dataarray(self.plotfunc, "x", "y")
1532-
for ax in g.axes.flat:
1532+
for ax in g.axs.flat:
15331533
assert ax.has_data()
15341534

15351535
def test_2d_function_and_method_signature_same(self) -> None:
@@ -1544,8 +1544,8 @@ def test_convenient_facetgrid(self) -> None:
15441544
d = DataArray(a, dims=["y", "x", "z"])
15451545
g = self.plotfunc(d, x="x", y="y", col="z", col_wrap=2)
15461546

1547-
assert_array_equal(g.axes.shape, [2, 2])
1548-
for (y, x), ax in np.ndenumerate(g.axes):
1547+
assert_array_equal(g.axs.shape, [2, 2])
1548+
for (y, x), ax in np.ndenumerate(g.axs):
15491549
assert ax.has_data()
15501550
if x == 0:
15511551
assert "y" == ax.get_ylabel()
@@ -1558,8 +1558,8 @@ def test_convenient_facetgrid(self) -> None:
15581558

15591559
# Inferring labels
15601560
g = self.plotfunc(d, col="z", col_wrap=2)
1561-
assert_array_equal(g.axes.shape, [2, 2])
1562-
for (y, x), ax in np.ndenumerate(g.axes):
1561+
assert_array_equal(g.axs.shape, [2, 2])
1562+
for (y, x), ax in np.ndenumerate(g.axs):
15631563
assert ax.has_data()
15641564
if x == 0:
15651565
assert "y" == ax.get_ylabel()
@@ -1576,8 +1576,8 @@ def test_convenient_facetgrid_4d(self) -> None:
15761576
d = DataArray(a, dims=["y", "x", "columns", "rows"])
15771577
g = self.plotfunc(d, x="x", y="y", col="columns", row="rows")
15781578

1579-
assert_array_equal(g.axes.shape, [3, 2])
1580-
for ax in g.axes.flat:
1579+
assert_array_equal(g.axs.shape, [3, 2])
1580+
for ax in g.axs.flat:
15811581
assert ax.has_data()
15821582

15831583
@pytest.mark.filterwarnings("ignore:This figure includes")
@@ -2035,16 +2035,16 @@ def test_convenient_facetgrid(self) -> None:
20352035
d = DataArray(a, dims=["y", "x", "z"])
20362036
g = self.plotfunc(d, x="x", y="y", col="z", col_wrap=2)
20372037

2038-
assert_array_equal(g.axes.shape, [2, 2])
2039-
for (y, x), ax in np.ndenumerate(g.axes):
2038+
assert_array_equal(g.axs.shape, [2, 2])
2039+
for (y, x), ax in np.ndenumerate(g.axs):
20402040
assert ax.has_data()
20412041
assert "y" == ax.get_ylabel()
20422042
assert "x" == ax.get_xlabel()
20432043

20442044
# Inferring labels
20452045
g = self.plotfunc(d, col="z", col_wrap=2)
2046-
assert_array_equal(g.axes.shape, [2, 2])
2047-
for (y, x), ax in np.ndenumerate(g.axes):
2046+
assert_array_equal(g.axs.shape, [2, 2])
2047+
for (y, x), ax in np.ndenumerate(g.axs):
20482048
assert ax.has_data()
20492049
assert "y" == ax.get_ylabel()
20502050
assert "x" == ax.get_xlabel()
@@ -2077,14 +2077,14 @@ def test_no_args(self) -> None:
20772077
alltxt = text_in_fig()
20782078
assert "None" not in alltxt
20792079

2080-
for ax in self.g.axes.flat:
2080+
for ax in self.g.axs.flat:
20812081
assert ax.has_data()
20822082

20832083
@pytest.mark.slow
20842084
def test_names_appear_somewhere(self) -> None:
20852085
self.darray.name = "testvar"
20862086
self.g.map_dataarray(xplt.contourf, "x", "y")
2087-
for k, ax in zip("abc", self.g.axes.flat):
2087+
for k, ax in zip("abc", self.g.axs.flat):
20882088
assert f"z = {k}" == ax.get_title()
20892089

20902090
alltxt = text_in_fig()
@@ -2101,7 +2101,7 @@ def test_text_not_super_long(self) -> None:
21012101
maxlen = max(len(txt) for txt in alltxt)
21022102
assert maxlen < 50
21032103

2104-
t0 = g.axes[0, 0].get_title()
2104+
t0 = g.axs[0, 0].get_title()
21052105
assert t0.endswith("...")
21062106

21072107
@pytest.mark.slow
@@ -2123,7 +2123,7 @@ def test_empty_cell(self) -> None:
21232123
g = xplt.FacetGrid(self.darray, col="z", col_wrap=2)
21242124
g.map_dataarray(xplt.imshow, "x", "y")
21252125

2126-
bottomright = g.axes[-1, -1]
2126+
bottomright = g.axs[-1, -1]
21272127
assert not bottomright.has_data()
21282128
assert not bottomright.get_visible()
21292129

@@ -2230,7 +2230,7 @@ def test_num_ticks(self) -> None:
22302230
self.g.map_dataarray(xplt.imshow, "x", "y")
22312231
self.g.set_ticks(max_xticks=nticks, max_yticks=nticks)
22322232

2233-
for ax in self.g.axes.flat:
2233+
for ax in self.g.axs.flat:
22342234
xticks = len(ax.get_xticks())
22352235
yticks = len(ax.get_yticks())
22362236
assert xticks <= maxticks
@@ -2314,36 +2314,36 @@ def test_title_kwargs(self) -> None:
23142314
g.set_titles(template="{value}", weight="bold")
23152315

23162316
# Rightmost column titles should be bold
2317-
for label, ax in zip(self.darray.coords["row"].values, g.axes[:, -1]):
2317+
for label, ax in zip(self.darray.coords["row"].values, g.axs[:, -1]):
23182318
assert property_in_axes_text("weight", "bold", label, ax)
23192319

23202320
# Top row titles should be bold
2321-
for label, ax in zip(self.darray.coords["col"].values, g.axes[0, :]):
2321+
for label, ax in zip(self.darray.coords["col"].values, g.axs[0, :]):
23222322
assert property_in_axes_text("weight", "bold", label, ax)
23232323

23242324
@pytest.mark.slow
23252325
def test_default_labels(self) -> None:
23262326
g = xplt.FacetGrid(self.darray, col="col", row="row")
2327-
assert (2, 3) == g.axes.shape
2327+
assert (2, 3) == g.axs.shape
23282328

23292329
g.map_dataarray(xplt.imshow, "x", "y")
23302330

23312331
# Rightmost column should be labeled
2332-
for label, ax in zip(self.darray.coords["row"].values, g.axes[:, -1]):
2332+
for label, ax in zip(self.darray.coords["row"].values, g.axs[:, -1]):
23332333
assert substring_in_axes(label, ax)
23342334

23352335
# Top row should be labeled
2336-
for label, ax in zip(self.darray.coords["col"].values, g.axes[0, :]):
2336+
for label, ax in zip(self.darray.coords["col"].values, g.axs[0, :]):
23372337
assert substring_in_axes(label, ax)
23382338

23392339
# ensure that row & col labels can be changed
23402340
g.set_titles("abc={value}")
2341-
for label, ax in zip(self.darray.coords["row"].values, g.axes[:, -1]):
2341+
for label, ax in zip(self.darray.coords["row"].values, g.axs[:, -1]):
23422342
assert substring_in_axes(f"abc={label}", ax)
23432343
# previous labels were "row=row0" etc.
23442344
assert substring_not_in_axes("row=", ax)
23452345

2346-
for label, ax in zip(self.darray.coords["col"].values, g.axes[0, :]):
2346+
for label, ax in zip(self.darray.coords["col"].values, g.axs[0, :]):
23472347
assert substring_in_axes(f"abc={label}", ax)
23482348
# previous labels were "col=row0" etc.
23492349
assert substring_not_in_axes("col=", ax)
@@ -2381,15 +2381,15 @@ def setUp(self) -> None:
23812381

23822382
def test_facetgrid_shape(self) -> None:
23832383
g = self.darray.plot(row="row", col="col", hue="hue")
2384-
assert g.axes.shape == (len(self.darray.row), len(self.darray.col))
2384+
assert g.axs.shape == (len(self.darray.row), len(self.darray.col))
23852385

23862386
g = self.darray.plot(row="col", col="row", hue="hue")
2387-
assert g.axes.shape == (len(self.darray.col), len(self.darray.row))
2387+
assert g.axs.shape == (len(self.darray.col), len(self.darray.row))
23882388

23892389
def test_unnamed_args(self) -> None:
23902390
g = self.darray.plot.line("o--", row="row", col="col", hue="hue")
23912391
lines = [
2392-
q for q in g.axes.flat[0].get_children() if isinstance(q, mpl.lines.Line2D)
2392+
q for q in g.axs.flat[0].get_children() if isinstance(q, mpl.lines.Line2D)
23932393
]
23942394
# passing 'o--' as argument should set marker and linestyle
23952395
assert lines[0].get_marker() == "o"
@@ -2398,15 +2398,15 @@ def test_unnamed_args(self) -> None:
23982398
def test_default_labels(self) -> None:
23992399
g = self.darray.plot(row="row", col="col", hue="hue")
24002400
# Rightmost column should be labeled
2401-
for label, ax in zip(self.darray.coords["row"].values, g.axes[:, -1]):
2401+
for label, ax in zip(self.darray.coords["row"].values, g.axs[:, -1]):
24022402
assert substring_in_axes(label, ax)
24032403

24042404
# Top row should be labeled
2405-
for label, ax in zip(self.darray.coords["col"].values, g.axes[0, :]):
2405+
for label, ax in zip(self.darray.coords["col"].values, g.axs[0, :]):
24062406
assert substring_in_axes(str(label), ax)
24072407

24082408
# Leftmost column should have array name
2409-
for ax in g.axes[:, 0]:
2409+
for ax in g.axs[:, 0]:
24102410
assert substring_in_axes(self.darray.name, ax)
24112411

24122412
def test_test_empty_cell(self) -> None:
@@ -2415,7 +2415,7 @@ def test_test_empty_cell(self) -> None:
24152415
.drop_vars("row")
24162416
.plot(col="col", hue="hue", col_wrap=2)
24172417
)
2418-
bottomright = g.axes[-1, -1]
2418+
bottomright = g.axs[-1, -1]
24192419
assert not bottomright.has_data()
24202420
assert not bottomright.get_visible()
24212421

@@ -2641,24 +2641,24 @@ def test_add_guide(
26412641

26422642
def test_facetgrid_shape(self) -> None:
26432643
g = self.ds.plot.scatter(x="A", y="B", row="row", col="col")
2644-
assert g.axes.shape == (len(self.ds.row), len(self.ds.col))
2644+
assert g.axs.shape == (len(self.ds.row), len(self.ds.col))
26452645

26462646
g = self.ds.plot.scatter(x="A", y="B", row="col", col="row")
2647-
assert g.axes.shape == (len(self.ds.col), len(self.ds.row))
2647+
assert g.axs.shape == (len(self.ds.col), len(self.ds.row))
26482648

26492649
def test_default_labels(self) -> None:
26502650
g = self.ds.plot.scatter(x="A", y="B", row="row", col="col", hue="hue")
26512651

26522652
# Top row should be labeled
2653-
for label, ax in zip(self.ds.coords["col"].values, g.axes[0, :]):
2653+
for label, ax in zip(self.ds.coords["col"].values, g.axs[0, :]):
26542654
assert substring_in_axes(str(label), ax)
26552655

26562656
# Bottom row should have name of x array name and units
2657-
for ax in g.axes[-1, :]:
2657+
for ax in g.axs[-1, :]:
26582658
assert ax.get_xlabel() == "A [Aunits]"
26592659

26602660
# Leftmost column should have name of y array name and units
2661-
for ax in g.axes[:, 0]:
2661+
for ax in g.axs[:, 0]:
26622662
assert ax.get_ylabel() == "B [Bunits]"
26632663

26642664
def test_axes_in_faceted_plot(self) -> None:
@@ -3178,3 +3178,18 @@ def test_assert_valid_xy() -> None:
31783178
# A hashable that is not valid should error:
31793179
with pytest.raises(ValueError, match="x must be one of"):
31803180
_assert_valid_xy(darray=darray, xy="error_now", name="x")
3181+
3182+
3183+
@requires_matplotlib
3184+
def test_facetgrid_axes_raises_deprecation_warning():
3185+
with pytest.warns(
3186+
DeprecationWarning,
3187+
match=(
3188+
"self.axes is deprecated since 2022.11 in order to align with "
3189+
"matplotlibs plt.subplots, use self.axs instead."
3190+
),
3191+
):
3192+
with figure_context():
3193+
ds = xr.tutorial.scatter_example_dataset()
3194+
g = ds.plot.scatter(x="A", y="B", col="x")
3195+
g.axes

xarray/tests/test_units.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5734,7 +5734,7 @@ def test_units_facetgrid_plot_labels(self):
57345734
fig, (ax, cax) = plt.subplots(1, 2)
57355735
fgrid = da.plot.line(x="x", col="y")
57365736

5737-
assert fgrid.axes[0, 0].get_ylabel() == "pressure [pascal]"
5737+
assert fgrid.axs[0, 0].get_ylabel() == "pressure [pascal]"
57385738

57395739
def test_units_facetgrid_2d_imshow_plot_colorbar_labels(self):
57405740
arr = np.ones((2, 3, 4, 5)) * unit_registry.Pa

0 commit comments

Comments
 (0)