Skip to content

Commit eb22bf8

Browse files
Backport PR #60584 on branch 2.3.x (TST: Address matplotlib 3.10 deprecation of vert=) (#60586)
* Backport PR #60584: TST: Address matplotlib 3.10 deprecation of vert= * Add missing import * Ignore pre-commit check --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 3362822 commit eb22bf8

File tree

4 files changed

+76
-23
lines changed

4 files changed

+76
-23
lines changed

pandas/plotting/_matplotlib/boxplot.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
import warnings
99

10+
import matplotlib as mpl
1011
from matplotlib.artist import setp
1112
import numpy as np
1213

@@ -20,6 +21,7 @@
2021

2122
import pandas as pd
2223
import pandas.core.common as com
24+
from pandas.util.version import Version
2325

2426
from pandas.io.formats.printing import pprint_thing
2527
from pandas.plotting._matplotlib.core import (
@@ -54,7 +56,8 @@ def _set_ticklabels(ax: Axes, labels: list[str], is_vertical: bool, **kwargs) ->
5456
ticks = ax.get_xticks() if is_vertical else ax.get_yticks()
5557
if len(ticks) != len(labels):
5658
i, remainder = divmod(len(ticks), len(labels))
57-
assert remainder == 0, remainder
59+
if Version(mpl.__version__) < Version("3.10"):
60+
assert remainder == 0, remainder
5861
labels *= i
5962
if is_vertical:
6063
ax.set_xticklabels(labels, **kwargs)

pandas/plotting/_matplotlib/tools.py

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

6262

6363
def table(

pandas/tests/plotting/frame/test_frame.py

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

1062+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
1063+
@pytest.mark.xfail(
1064+
Version(mpl.__version__) >= Version("3.10"),
1065+
reason="Fails starting with matplotlib 3.10",
1066+
)
10621067
def test_boxplot_vertical(self, hist_df):
10631068
df = hist_df
10641069
numeric_cols = df._get_numeric_data().columns
10651070
labels = [pprint_thing(c) for c in numeric_cols]
10661071

10671072
# if horizontal, yticklabels are rotated
1068-
ax = df.plot.box(rot=50, fontsize=8, vert=False)
1073+
kwargs = (
1074+
{"vert": False}
1075+
if Version(mpl.__version__) < Version("3.10")
1076+
else {"orientation": "horizontal"}
1077+
)
1078+
ax = df.plot.box(rot=50, fontsize=8, **kwargs)
10691079
_check_ticks_props(ax, xrot=0, yrot=50, ylabelsize=8)
10701080
_check_text_labels(ax.get_yticklabels(), labels)
10711081
assert len(ax.lines) == 7 * len(numeric_cols)
10721082

1073-
@pytest.mark.filterwarnings("ignore:Attempt:UserWarning")
1083+
@pytest.mark.filterwarnings("ignore::UserWarning")
1084+
@pytest.mark.xfail(
1085+
Version(mpl.__version__) >= Version("3.10"),
1086+
reason="Fails starting with matplotlib version 3.10",
1087+
)
10741088
def test_boxplot_vertical_subplots(self, hist_df):
10751089
df = hist_df
10761090
numeric_cols = df._get_numeric_data().columns
10771091
labels = [pprint_thing(c) for c in numeric_cols]
1092+
kwargs = (
1093+
{"vert": False}
1094+
if Version(mpl.__version__) < Version("3.10")
1095+
else {"orientation": "horizontal"}
1096+
)
10781097
axes = _check_plot_works(
1079-
df.plot.box,
1080-
default_axes=True,
1081-
subplots=True,
1082-
vert=False,
1083-
logx=True,
1098+
df.plot.box, default_axes=True, subplots=True, logx=True, **kwargs
10841099
)
10851100
_check_axes_shape(axes, axes_num=3, layout=(1, 3))
10861101
_check_ax_scales(axes, xaxis="log")
10871102
for ax, label in zip(axes, labels):
10881103
_check_text_labels(ax.get_yticklabels(), [label])
10891104
assert len(ax.lines) == 7
10901105

1106+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
1107+
@pytest.mark.xfail(
1108+
Version(mpl.__version__) >= Version("3.10"),
1109+
reason="Fails starting with matplotlib 3.10",
1110+
)
10911111
def test_boxplot_vertical_positions(self, hist_df):
10921112
df = hist_df
10931113
numeric_cols = df._get_numeric_data().columns
10941114
labels = [pprint_thing(c) for c in numeric_cols]
10951115
positions = np.array([3, 2, 8])
1096-
ax = df.plot.box(positions=positions, vert=False)
1116+
kwargs = (
1117+
{"vert": False}
1118+
if Version(mpl.__version__) < Version("3.10")
1119+
else {"orientation": "horizontal"}
1120+
)
1121+
ax = df.plot.box(positions=positions, **kwargs)
10971122
_check_text_labels(ax.get_yticklabels(), labels)
10981123
tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), positions)
10991124
assert len(ax.lines) == 7 * len(numeric_cols)

pandas/tests/plotting/test_boxplot_method.py

+38-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
@@ -315,7 +329,7 @@ def test_specified_props_kwd(self, props, expected):
315329

316330
assert result[expected][0].get_color() == "C1"
317331

318-
@pytest.mark.parametrize("vert", [True, False])
332+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
319333
def test_plot_xlabel_ylabel(self, vert):
320334
df = DataFrame(
321335
{
@@ -325,11 +339,11 @@ def test_plot_xlabel_ylabel(self, vert):
325339
}
326340
)
327341
xlabel, ylabel = "x", "y"
328-
ax = df.plot(kind="box", vert=vert, xlabel=xlabel, ylabel=ylabel)
342+
ax = df.plot(kind="box", xlabel=xlabel, ylabel=ylabel, **vert)
329343
assert ax.get_xlabel() == xlabel
330344
assert ax.get_ylabel() == ylabel
331345

332-
@pytest.mark.parametrize("vert", [True, False])
346+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
333347
def test_plot_box(self, vert):
334348
# GH 54941
335349
rng = np.random.default_rng(2)
@@ -338,14 +352,14 @@ def test_plot_box(self, vert):
338352

339353
xlabel, ylabel = "x", "y"
340354
_, axs = plt.subplots(ncols=2, figsize=(10, 7), sharey=True)
341-
df1.plot.box(ax=axs[0], vert=vert, xlabel=xlabel, ylabel=ylabel)
342-
df2.plot.box(ax=axs[1], vert=vert, xlabel=xlabel, ylabel=ylabel)
355+
df1.plot.box(ax=axs[0], xlabel=xlabel, ylabel=ylabel, **vert)
356+
df2.plot.box(ax=axs[1], xlabel=xlabel, ylabel=ylabel, **vert)
343357
for ax in axs:
344358
assert ax.get_xlabel() == xlabel
345359
assert ax.get_ylabel() == ylabel
346360
mpl.pyplot.close()
347361

348-
@pytest.mark.parametrize("vert", [True, False])
362+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
349363
def test_boxplot_xlabel_ylabel(self, vert):
350364
df = DataFrame(
351365
{
@@ -355,11 +369,11 @@ def test_boxplot_xlabel_ylabel(self, vert):
355369
}
356370
)
357371
xlabel, ylabel = "x", "y"
358-
ax = df.boxplot(vert=vert, xlabel=xlabel, ylabel=ylabel)
372+
ax = df.boxplot(xlabel=xlabel, ylabel=ylabel, **vert)
359373
assert ax.get_xlabel() == xlabel
360374
assert ax.get_ylabel() == ylabel
361375

362-
@pytest.mark.parametrize("vert", [True, False])
376+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
363377
def test_boxplot_group_xlabel_ylabel(self, vert):
364378
df = DataFrame(
365379
{
@@ -369,24 +383,35 @@ def test_boxplot_group_xlabel_ylabel(self, vert):
369383
}
370384
)
371385
xlabel, ylabel = "x", "y"
372-
ax = df.boxplot(by="group", vert=vert, xlabel=xlabel, ylabel=ylabel)
386+
ax = df.boxplot(by="group", xlabel=xlabel, ylabel=ylabel, **vert)
373387
for subplot in ax:
374388
assert subplot.get_xlabel() == xlabel
375389
assert subplot.get_ylabel() == ylabel
376390
mpl.pyplot.close()
377391

378-
@pytest.mark.parametrize("vert", [True, False])
379-
def test_boxplot_group_no_xlabel_ylabel(self, vert):
392+
@pytest.mark.filterwarnings("ignore:set_ticklabels:UserWarning")
393+
def test_boxplot_group_no_xlabel_ylabel(self, vert, request):
394+
if Version(mpl.__version__) >= Version("3.10") and vert == {
395+
"orientation": "horizontal"
396+
}:
397+
request.applymarker(
398+
pytest.mark.xfail(reason=f"{vert} fails starting with matplotlib 3.10")
399+
)
380400
df = DataFrame(
381401
{
382402
"a": np.random.default_rng(2).standard_normal(10),
383403
"b": np.random.default_rng(2).standard_normal(10),
384404
"group": np.random.default_rng(2).choice(["group1", "group2"], 10),
385405
}
386406
)
387-
ax = df.boxplot(by="group", vert=vert)
407+
ax = df.boxplot(by="group", **vert)
388408
for subplot in ax:
389-
target_label = subplot.get_xlabel() if vert else subplot.get_ylabel()
409+
target_label = (
410+
subplot.get_xlabel()
411+
if vert == {"vert": True} # noqa: PLR1714
412+
or vert == {"orientation": "vertical"}
413+
else subplot.get_ylabel()
414+
)
390415
assert target_label == pprint_thing(["group"])
391416
mpl.pyplot.close()
392417

0 commit comments

Comments
 (0)