Skip to content

Commit edf00e9

Browse files
authored
TST: Address matplotlib 3.10 deprecation of vert= (#60584)
* TST: Address matplotlib 3.10 deprecation of vert= * Type in ._version * Address other failures * more test faillures * Add more xfails * mypy error
1 parent 9fe33bc commit edf00e9

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

pandas/plotting/_matplotlib/boxplot.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import pandas as pd
2222
import pandas.core.common as com
23+
from pandas.util.version import Version
2324

2425
from pandas.io.formats.printing import pprint_thing
2526
from pandas.plotting._matplotlib.core import (
@@ -54,7 +55,8 @@ def _set_ticklabels(ax: Axes, labels: list[str], is_vertical: bool, **kwargs) ->
5455
ticks = ax.get_xticks() if is_vertical else ax.get_yticks()
5556
if len(ticks) != len(labels):
5657
i, remainder = divmod(len(ticks), len(labels))
57-
assert remainder == 0, remainder
58+
if Version(mpl.__version__) < Version("3.10"):
59+
assert remainder == 0, remainder
5860
labels *= i
5961
if is_vertical:
6062
ax.set_xticklabels(labels, **kwargs)

pandas/plotting/_matplotlib/tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def format_date_labels(ax: Axes, rot) -> None:
5656
fig = ax.get_figure()
5757
if fig is not None:
5858
# should always be a Figure but can technically be None
59-
maybe_adjust_figure(fig, bottom=0.2)
59+
maybe_adjust_figure(fig, bottom=0.2) # type: ignore[arg-type]
6060

6161

6262
def table(

pandas/tests/plotting/frame/test_frame.py

+33-8
Original file line numberDiff line numberDiff line change
@@ -1070,41 +1070,66 @@ def test_boxplot_series_positions(self, hist_df):
10701070
tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), positions)
10711071
assert len(ax.lines) == 7 * len(numeric_cols)
10721072

1073+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
1074+
@pytest.mark.xfail(
1075+
Version(mpl.__version__) >= Version("3.10"),
1076+
reason="Fails starting with matplotlib 3.10",
1077+
)
10731078
def test_boxplot_vertical(self, hist_df):
10741079
df = hist_df
10751080
numeric_cols = df._get_numeric_data().columns
10761081
labels = [pprint_thing(c) for c in numeric_cols]
10771082

10781083
# if horizontal, yticklabels are rotated
1079-
ax = df.plot.box(rot=50, fontsize=8, vert=False)
1084+
kwargs = (
1085+
{"vert": False}
1086+
if Version(mpl.__version__) < Version("3.10")
1087+
else {"orientation": "horizontal"}
1088+
)
1089+
ax = df.plot.box(rot=50, fontsize=8, **kwargs)
10801090
_check_ticks_props(ax, xrot=0, yrot=50, ylabelsize=8)
10811091
_check_text_labels(ax.get_yticklabels(), labels)
10821092
assert len(ax.lines) == 7 * len(numeric_cols)
10831093

1084-
@pytest.mark.filterwarnings("ignore:Attempt:UserWarning")
1094+
@pytest.mark.filterwarnings("ignore::UserWarning")
1095+
@pytest.mark.xfail(
1096+
Version(mpl.__version__) >= Version("3.10"),
1097+
reason="Fails starting with matplotlib version 3.10",
1098+
)
10851099
def test_boxplot_vertical_subplots(self, hist_df):
10861100
df = hist_df
10871101
numeric_cols = df._get_numeric_data().columns
10881102
labels = [pprint_thing(c) for c in numeric_cols]
1103+
kwargs = (
1104+
{"vert": False}
1105+
if Version(mpl.__version__) < Version("3.10")
1106+
else {"orientation": "horizontal"}
1107+
)
10891108
axes = _check_plot_works(
1090-
df.plot.box,
1091-
default_axes=True,
1092-
subplots=True,
1093-
vert=False,
1094-
logx=True,
1109+
df.plot.box, default_axes=True, subplots=True, logx=True, **kwargs
10951110
)
10961111
_check_axes_shape(axes, axes_num=3, layout=(1, 3))
10971112
_check_ax_scales(axes, xaxis="log")
10981113
for ax, label in zip(axes, labels):
10991114
_check_text_labels(ax.get_yticklabels(), [label])
11001115
assert len(ax.lines) == 7
11011116

1117+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
1118+
@pytest.mark.xfail(
1119+
Version(mpl.__version__) >= Version("3.10"),
1120+
reason="Fails starting with matplotlib 3.10",
1121+
)
11021122
def test_boxplot_vertical_positions(self, hist_df):
11031123
df = hist_df
11041124
numeric_cols = df._get_numeric_data().columns
11051125
labels = [pprint_thing(c) for c in numeric_cols]
11061126
positions = np.array([3, 2, 8])
1107-
ax = df.plot.box(positions=positions, vert=False)
1127+
kwargs = (
1128+
{"vert": False}
1129+
if Version(mpl.__version__) < Version("3.10")
1130+
else {"orientation": "horizontal"}
1131+
)
1132+
ax = df.plot.box(positions=positions, **kwargs)
11081133
_check_text_labels(ax.get_yticklabels(), labels)
11091134
tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), positions)
11101135
assert len(ax.lines) == 7 * len(numeric_cols)

pandas/tests/plotting/test_boxplot_method.py

+37-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test cases for .boxplot method"""
22

3+
from __future__ import annotations
4+
35
import itertools
46
import string
57

@@ -22,6 +24,7 @@
2224
_check_ticks_props,
2325
_check_visible,
2426
)
27+
from pandas.util.version import Version
2528

2629
from pandas.io.formats.printing import pprint_thing
2730

@@ -35,6 +38,17 @@ def _check_ax_limits(col, ax):
3538
assert y_max >= col.max()
3639

3740

41+
if Version(mpl.__version__) < Version("3.10"):
42+
verts: list[dict[str, bool | str]] = [{"vert": False}, {"vert": True}]
43+
else:
44+
verts = [{"orientation": "horizontal"}, {"orientation": "vertical"}]
45+
46+
47+
@pytest.fixture(params=verts)
48+
def vert(request):
49+
return request.param
50+
51+
3852
class TestDataFramePlots:
3953
def test_stacked_boxplot_set_axis(self):
4054
# GH2980
@@ -312,7 +326,7 @@ def test_specified_props_kwd(self, props, expected):
312326

313327
assert result[expected][0].get_color() == "C1"
314328

315-
@pytest.mark.parametrize("vert", [True, False])
329+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
316330
def test_plot_xlabel_ylabel(self, vert):
317331
df = DataFrame(
318332
{
@@ -322,11 +336,11 @@ def test_plot_xlabel_ylabel(self, vert):
322336
}
323337
)
324338
xlabel, ylabel = "x", "y"
325-
ax = df.plot(kind="box", vert=vert, xlabel=xlabel, ylabel=ylabel)
339+
ax = df.plot(kind="box", xlabel=xlabel, ylabel=ylabel, **vert)
326340
assert ax.get_xlabel() == xlabel
327341
assert ax.get_ylabel() == ylabel
328342

329-
@pytest.mark.parametrize("vert", [True, False])
343+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
330344
def test_plot_box(self, vert):
331345
# GH 54941
332346
rng = np.random.default_rng(2)
@@ -335,13 +349,13 @@ def test_plot_box(self, vert):
335349

336350
xlabel, ylabel = "x", "y"
337351
_, axs = plt.subplots(ncols=2, figsize=(10, 7), sharey=True)
338-
df1.plot.box(ax=axs[0], vert=vert, xlabel=xlabel, ylabel=ylabel)
339-
df2.plot.box(ax=axs[1], vert=vert, xlabel=xlabel, ylabel=ylabel)
352+
df1.plot.box(ax=axs[0], xlabel=xlabel, ylabel=ylabel, **vert)
353+
df2.plot.box(ax=axs[1], xlabel=xlabel, ylabel=ylabel, **vert)
340354
for ax in axs:
341355
assert ax.get_xlabel() == xlabel
342356
assert ax.get_ylabel() == ylabel
343357

344-
@pytest.mark.parametrize("vert", [True, False])
358+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
345359
def test_boxplot_xlabel_ylabel(self, vert):
346360
df = DataFrame(
347361
{
@@ -351,11 +365,11 @@ def test_boxplot_xlabel_ylabel(self, vert):
351365
}
352366
)
353367
xlabel, ylabel = "x", "y"
354-
ax = df.boxplot(vert=vert, xlabel=xlabel, ylabel=ylabel)
368+
ax = df.boxplot(xlabel=xlabel, ylabel=ylabel, **vert)
355369
assert ax.get_xlabel() == xlabel
356370
assert ax.get_ylabel() == ylabel
357371

358-
@pytest.mark.parametrize("vert", [True, False])
372+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
359373
def test_boxplot_group_xlabel_ylabel(self, vert):
360374
df = DataFrame(
361375
{
@@ -365,23 +379,33 @@ def test_boxplot_group_xlabel_ylabel(self, vert):
365379
}
366380
)
367381
xlabel, ylabel = "x", "y"
368-
ax = df.boxplot(by="group", vert=vert, xlabel=xlabel, ylabel=ylabel)
382+
ax = df.boxplot(by="group", xlabel=xlabel, ylabel=ylabel, **vert)
369383
for subplot in ax:
370384
assert subplot.get_xlabel() == xlabel
371385
assert subplot.get_ylabel() == ylabel
372386

373-
@pytest.mark.parametrize("vert", [True, False])
374-
def test_boxplot_group_no_xlabel_ylabel(self, vert):
387+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
388+
def test_boxplot_group_no_xlabel_ylabel(self, vert, request):
389+
if Version(mpl.__version__) >= Version("3.10") and vert == {
390+
"orientation": "horizontal"
391+
}:
392+
request.applymarker(
393+
pytest.mark.xfail(reason=f"{vert} fails starting with matplotlib 3.10")
394+
)
375395
df = DataFrame(
376396
{
377397
"a": np.random.default_rng(2).standard_normal(10),
378398
"b": np.random.default_rng(2).standard_normal(10),
379399
"group": np.random.default_rng(2).choice(["group1", "group2"], 10),
380400
}
381401
)
382-
ax = df.boxplot(by="group", vert=vert)
402+
ax = df.boxplot(by="group", **vert)
383403
for subplot in ax:
384-
target_label = subplot.get_xlabel() if vert else subplot.get_ylabel()
404+
target_label = (
405+
subplot.get_xlabel()
406+
if vert == {"vert": True} or vert == {"orientation": "vertical"}
407+
else subplot.get_ylabel()
408+
)
385409
assert target_label == pprint_thing(["group"])
386410

387411

0 commit comments

Comments
 (0)