1
1
""" Test cases for .boxplot method """
2
2
3
+ from __future__ import annotations
4
+
3
5
import itertools
4
6
import string
5
7
22
24
_check_ticks_props ,
23
25
_check_visible ,
24
26
)
27
+ from pandas .util .version import Version
25
28
26
29
from pandas .io .formats .printing import pprint_thing
27
30
@@ -35,6 +38,17 @@ def _check_ax_limits(col, ax):
35
38
assert y_max >= col .max ()
36
39
37
40
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
+
38
52
class TestDataFramePlots :
39
53
def test_stacked_boxplot_set_axis (self ):
40
54
# GH2980
@@ -315,7 +329,7 @@ def test_specified_props_kwd(self, props, expected):
315
329
316
330
assert result [expected ][0 ].get_color () == "C1"
317
331
318
- @pytest .mark .parametrize ( "vert" , [ True , False ] )
332
+ @pytest .mark .filterwarnings ( "ignore:set_ticklabels:UserWarning" )
319
333
def test_plot_xlabel_ylabel (self , vert ):
320
334
df = DataFrame (
321
335
{
@@ -325,11 +339,11 @@ def test_plot_xlabel_ylabel(self, vert):
325
339
}
326
340
)
327
341
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 )
329
343
assert ax .get_xlabel () == xlabel
330
344
assert ax .get_ylabel () == ylabel
331
345
332
- @pytest .mark .parametrize ( "vert" , [ True , False ] )
346
+ @pytest .mark .filterwarnings ( "ignore:set_ticklabels:UserWarning" )
333
347
def test_plot_box (self , vert ):
334
348
# GH 54941
335
349
rng = np .random .default_rng (2 )
@@ -338,14 +352,14 @@ def test_plot_box(self, vert):
338
352
339
353
xlabel , ylabel = "x" , "y"
340
354
_ , 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 )
343
357
for ax in axs :
344
358
assert ax .get_xlabel () == xlabel
345
359
assert ax .get_ylabel () == ylabel
346
360
mpl .pyplot .close ()
347
361
348
- @pytest .mark .parametrize ( "vert" , [ True , False ] )
362
+ @pytest .mark .filterwarnings ( "ignore:set_ticklabels:UserWarning" )
349
363
def test_boxplot_xlabel_ylabel (self , vert ):
350
364
df = DataFrame (
351
365
{
@@ -355,11 +369,11 @@ def test_boxplot_xlabel_ylabel(self, vert):
355
369
}
356
370
)
357
371
xlabel , ylabel = "x" , "y"
358
- ax = df .boxplot (vert = vert , xlabel = xlabel , ylabel = ylabel )
372
+ ax = df .boxplot (xlabel = xlabel , ylabel = ylabel , ** vert )
359
373
assert ax .get_xlabel () == xlabel
360
374
assert ax .get_ylabel () == ylabel
361
375
362
- @pytest .mark .parametrize ( "vert" , [ True , False ] )
376
+ @pytest .mark .filterwarnings ( "ignore:set_ticklabels:UserWarning" )
363
377
def test_boxplot_group_xlabel_ylabel (self , vert ):
364
378
df = DataFrame (
365
379
{
@@ -369,24 +383,35 @@ def test_boxplot_group_xlabel_ylabel(self, vert):
369
383
}
370
384
)
371
385
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 )
373
387
for subplot in ax :
374
388
assert subplot .get_xlabel () == xlabel
375
389
assert subplot .get_ylabel () == ylabel
376
390
mpl .pyplot .close ()
377
391
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
+ )
380
400
df = DataFrame (
381
401
{
382
402
"a" : np .random .default_rng (2 ).standard_normal (10 ),
383
403
"b" : np .random .default_rng (2 ).standard_normal (10 ),
384
404
"group" : np .random .default_rng (2 ).choice (["group1" , "group2" ], 10 ),
385
405
}
386
406
)
387
- ax = df .boxplot (by = "group" , vert = vert )
407
+ ax = df .boxplot (by = "group" , ** vert )
388
408
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
+ )
390
415
assert target_label == pprint_thing (["group" ])
391
416
mpl .pyplot .close ()
392
417
0 commit comments