28
28
mpl = pytest .importorskip ("matplotlib" )
29
29
30
30
31
+ def _check_ax_limits (col , ax ):
32
+ y_min , y_max = ax .get_ylim ()
33
+ assert y_min <= col .min ()
34
+ assert y_max >= col .max ()
35
+
36
+
31
37
class TestDataFramePlots :
32
38
def test_stacked_boxplot_set_axis (self ):
33
39
# GH2980
@@ -88,18 +94,30 @@ def test_boxplot_legacy2(self):
88
94
with tm .assert_produces_warning (UserWarning , check_stacklevel = False ):
89
95
_check_plot_works (df .boxplot , by = "X" )
90
96
97
+ def test_boxplot_legacy2_with_ax (self ):
98
+ df = DataFrame (np .random .rand (10 , 2 ), columns = ["Col1" , "Col2" ])
99
+ df ["X" ] = Series (["A" , "A" , "A" , "A" , "A" , "B" , "B" , "B" , "B" , "B" ])
100
+ df ["Y" ] = Series (["A" ] * 10 )
91
101
# When ax is supplied and required number of axes is 1,
92
102
# passed ax should be used:
93
- fig , ax = mpl .pyplot .subplots ()
103
+ _ , ax = mpl .pyplot .subplots ()
94
104
axes = df .boxplot ("Col1" , by = "X" , ax = ax )
95
105
ax_axes = ax .axes
96
106
assert ax_axes is axes
97
107
108
+ def test_boxplot_legacy2_with_ax_return_type (self ):
109
+ df = DataFrame (np .random .rand (10 , 2 ), columns = ["Col1" , "Col2" ])
110
+ df ["X" ] = Series (["A" , "A" , "A" , "A" , "A" , "B" , "B" , "B" , "B" , "B" ])
111
+ df ["Y" ] = Series (["A" ] * 10 )
98
112
fig , ax = mpl .pyplot .subplots ()
99
113
axes = df .groupby ("Y" ).boxplot (ax = ax , return_type = "axes" )
100
114
ax_axes = ax .axes
101
115
assert ax_axes is axes ["A" ]
102
116
117
+ def test_boxplot_legacy2_with_multi_col (self ):
118
+ df = DataFrame (np .random .rand (10 , 2 ), columns = ["Col1" , "Col2" ])
119
+ df ["X" ] = Series (["A" , "A" , "A" , "A" , "A" , "B" , "B" , "B" , "B" , "B" ])
120
+ df ["Y" ] = Series (["A" ] * 10 )
103
121
# Multiple columns with an ax argument should use same figure
104
122
fig , ax = mpl .pyplot .subplots ()
105
123
with tm .assert_produces_warning (UserWarning ):
@@ -108,9 +126,13 @@ def test_boxplot_legacy2(self):
108
126
)
109
127
assert axes ["Col1" ].get_figure () is fig
110
128
129
+ def test_boxplot_legacy2_by_none (self ):
130
+ df = DataFrame (np .random .rand (10 , 2 ), columns = ["Col1" , "Col2" ])
131
+ df ["X" ] = Series (["A" , "A" , "A" , "A" , "A" , "B" , "B" , "B" , "B" , "B" ])
132
+ df ["Y" ] = Series (["A" ] * 10 )
111
133
# When by is None, check that all relevant lines are present in the
112
134
# dict
113
- fig , ax = mpl .pyplot .subplots ()
135
+ _ , ax = mpl .pyplot .subplots ()
114
136
d = df .boxplot (ax = ax , return_type = "dict" )
115
137
lines = list (itertools .chain .from_iterable (d .values ()))
116
138
assert len (ax .get_lines ()) == len (lines )
@@ -135,24 +157,20 @@ def test_boxplot_return_type_legacy(self):
135
157
result = df .boxplot ()
136
158
_check_box_return_type (result , "axes" )
137
159
138
- with tm .assert_produces_warning (False ):
139
- result = df .boxplot (return_type = "dict" )
140
- _check_box_return_type (result , "dict" )
141
-
142
- with tm .assert_produces_warning (False ):
143
- result = df .boxplot (return_type = "axes" )
144
- _check_box_return_type (result , "axes" )
160
+ @pytest .mark .parametrize ("return_type" , ["dict" , "axes" , "both" ])
161
+ def test_boxplot_return_type_legacy_return_type (self , return_type ):
162
+ # API change in https://github.com/pandas-dev/pandas/pull/7096
145
163
164
+ df = DataFrame (
165
+ np .random .randn (6 , 4 ),
166
+ index = list (string .ascii_letters [:6 ]),
167
+ columns = ["one" , "two" , "three" , "four" ],
168
+ )
146
169
with tm .assert_produces_warning (False ):
147
- result = df .boxplot (return_type = "both" )
148
- _check_box_return_type (result , "both" )
170
+ result = df .boxplot (return_type = return_type )
171
+ _check_box_return_type (result , return_type )
149
172
150
173
def test_boxplot_axis_limits (self , hist_df ):
151
- def _check_ax_limits (col , ax ):
152
- y_min , y_max = ax .get_ylim ()
153
- assert y_min <= col .min ()
154
- assert y_max >= col .max ()
155
-
156
174
df = hist_df .copy ()
157
175
df ["age" ] = np .random .randint (1 , 20 , df .shape [0 ])
158
176
# One full row
@@ -161,6 +179,9 @@ def _check_ax_limits(col, ax):
161
179
_check_ax_limits (df ["weight" ], weight_ax )
162
180
assert weight_ax ._sharey == height_ax
163
181
182
+ def test_boxplot_axis_limits_two_rows (self , hist_df ):
183
+ df = hist_df .copy ()
184
+ df ["age" ] = np .random .randint (1 , 20 , df .shape [0 ])
164
185
# Two rows, one partial
165
186
p = df .boxplot (["height" , "weight" , "age" ], by = "category" )
166
187
height_ax , weight_ax , age_ax = p [0 , 0 ], p [0 , 1 ], p [1 , 0 ]
@@ -275,7 +296,7 @@ def test_color_kwd_errors(self, dict_colors, msg):
275
296
)
276
297
def test_specified_props_kwd (self , props , expected ):
277
298
# GH 30346
278
- df = DataFrame ({k : np .random .random (100 ) for k in "ABC" })
299
+ df = DataFrame ({k : np .random .random (10 ) for k in "ABC" })
279
300
kwd = {props : {"color" : "C1" }}
280
301
result = df .boxplot (return_type = "dict" , ** kwd )
281
302
@@ -285,9 +306,9 @@ def test_specified_props_kwd(self, props, expected):
285
306
def test_plot_xlabel_ylabel (self , vert ):
286
307
df = DataFrame (
287
308
{
288
- "a" : np .random .randn (100 ),
289
- "b" : np .random .randn (100 ),
290
- "group" : np .random .choice (["group1" , "group2" ], 100 ),
309
+ "a" : np .random .randn (10 ),
310
+ "b" : np .random .randn (10 ),
311
+ "group" : np .random .choice (["group1" , "group2" ], 10 ),
291
312
}
292
313
)
293
314
xlabel , ylabel = "x" , "y"
@@ -299,9 +320,9 @@ def test_plot_xlabel_ylabel(self, vert):
299
320
def test_boxplot_xlabel_ylabel (self , vert ):
300
321
df = DataFrame (
301
322
{
302
- "a" : np .random .randn (100 ),
303
- "b" : np .random .randn (100 ),
304
- "group" : np .random .choice (["group1" , "group2" ], 100 ),
323
+ "a" : np .random .randn (10 ),
324
+ "b" : np .random .randn (10 ),
325
+ "group" : np .random .choice (["group1" , "group2" ], 10 ),
305
326
}
306
327
)
307
328
xlabel , ylabel = "x" , "y"
@@ -313,9 +334,9 @@ def test_boxplot_xlabel_ylabel(self, vert):
313
334
def test_boxplot_group_xlabel_ylabel (self , vert ):
314
335
df = DataFrame (
315
336
{
316
- "a" : np .random .randn (100 ),
317
- "b" : np .random .randn (100 ),
318
- "group" : np .random .choice (["group1" , "group2" ], 100 ),
337
+ "a" : np .random .randn (10 ),
338
+ "b" : np .random .randn (10 ),
339
+ "group" : np .random .choice (["group1" , "group2" ], 10 ),
319
340
}
320
341
)
321
342
xlabel , ylabel = "x" , "y"
@@ -325,6 +346,15 @@ def test_boxplot_group_xlabel_ylabel(self, vert):
325
346
assert subplot .get_ylabel () == ylabel
326
347
mpl .pyplot .close ()
327
348
349
+ @pytest .mark .parametrize ("vert" , [True , False ])
350
+ def test_boxplot_group_no_xlabel_ylabel (self , vert ):
351
+ df = DataFrame (
352
+ {
353
+ "a" : np .random .randn (10 ),
354
+ "b" : np .random .randn (10 ),
355
+ "group" : np .random .choice (["group1" , "group2" ], 10 ),
356
+ }
357
+ )
328
358
ax = df .boxplot (by = "group" , vert = vert )
329
359
for subplot in ax :
330
360
target_label = subplot .get_xlabel () if vert else subplot .get_ylabel ()
@@ -338,6 +368,9 @@ def test_boxplot_legacy1(self, hist_df):
338
368
with tm .assert_produces_warning (UserWarning , check_stacklevel = False ):
339
369
axes = _check_plot_works (grouped .boxplot , return_type = "axes" )
340
370
_check_axes_shape (list (axes .values ), axes_num = 2 , layout = (1 , 2 ))
371
+
372
+ def test_boxplot_legacy1_return_type (self , hist_df ):
373
+ grouped = hist_df .groupby (by = "gender" )
341
374
axes = _check_plot_works (grouped .boxplot , subplots = False , return_type = "axes" )
342
375
_check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
343
376
@@ -350,6 +383,11 @@ def test_boxplot_legacy2(self):
350
383
axes = _check_plot_works (grouped .boxplot , return_type = "axes" )
351
384
_check_axes_shape (list (axes .values ), axes_num = 10 , layout = (4 , 3 ))
352
385
386
+ @pytest .mark .slow
387
+ def test_boxplot_legacy2_return_type (self ):
388
+ tuples = zip (string .ascii_letters [:10 ], range (10 ))
389
+ df = DataFrame (np .random .rand (10 , 3 ), index = MultiIndex .from_tuples (tuples ))
390
+ grouped = df .groupby (level = 1 )
353
391
axes = _check_plot_works (grouped .boxplot , subplots = False , return_type = "axes" )
354
392
_check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
355
393
@@ -387,8 +425,14 @@ def test_grouped_plot_fignums(self):
387
425
assert len (res ) == 2
388
426
tm .close ()
389
427
428
+ def test_grouped_plot_fignums_excluded_col (self ):
429
+ n = 10
430
+ weight = Series (np .random .normal (166 , 20 , size = n ))
431
+ height = Series (np .random .normal (60 , 10 , size = n ))
432
+ gender = np .random .RandomState (42 ).choice (["male" , "female" ], size = n )
433
+ df = DataFrame ({"height" : height , "weight" : weight , "gender" : gender })
390
434
# now works with GH 5610 as gender is excluded
391
- res = df .groupby ("gender" ).hist ()
435
+ df .groupby ("gender" ).hist ()
392
436
tm .close ()
393
437
394
438
@pytest .mark .slow
@@ -545,10 +589,14 @@ def test_grouped_box_multiple_axes(self, hist_df):
545
589
# location should be changed if other test is added
546
590
# which has earlier alphabetical order
547
591
with tm .assert_produces_warning (UserWarning ):
548
- fig , axes = mpl .pyplot .subplots (2 , 2 )
592
+ _ , axes = mpl .pyplot .subplots (2 , 2 )
549
593
df .groupby ("category" ).boxplot (column = "height" , return_type = "axes" , ax = axes )
550
594
_check_axes_shape (mpl .pyplot .gcf ().axes , axes_num = 4 , layout = (2 , 2 ))
551
595
596
+ @pytest .mark .slow
597
+ def test_grouped_box_multiple_axes_on_fig (self , hist_df ):
598
+ # GH 6970, GH 7069
599
+ df = hist_df
552
600
fig , axes = mpl .pyplot .subplots (2 , 3 )
553
601
with tm .assert_produces_warning (UserWarning ):
554
602
returned = df .boxplot (
@@ -572,6 +620,10 @@ def test_grouped_box_multiple_axes(self, hist_df):
572
620
tm .assert_numpy_array_equal (returned , axes [1 ])
573
621
assert returned [0 ].figure is fig
574
622
623
+ @pytest .mark .slow
624
+ def test_grouped_box_multiple_axes_ax_error (self , hist_df ):
625
+ # GH 6970, GH 7069
626
+ df = hist_df
575
627
msg = "The number of passed axes must be 3, the same as the output plot"
576
628
with pytest .raises (ValueError , match = msg ):
577
629
fig , axes = mpl .pyplot .subplots (2 , 3 )
0 commit comments