@@ -883,6 +883,13 @@ def test_plot(self):
883
883
axes = _check_plot_works (df .plot , kind = 'bar' , subplots = True )
884
884
self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
885
885
886
+ # When ax is supplied and required number of axes is 1,
887
+ # passed ax should be used:
888
+ fig , ax = self .plt .subplots ()
889
+ axes = df .plot (kind = 'bar' , subplots = True , ax = ax )
890
+ self .assertEqual (len (axes ), 1 )
891
+ self .assertIs (ax .get_axes (), axes [0 ])
892
+
886
893
def test_nonnumeric_exclude (self ):
887
894
df = DataFrame ({'A' : ["x" , "y" , "z" ], 'B' : [1 , 2 , 3 ]})
888
895
ax = df .plot ()
@@ -1443,17 +1450,23 @@ def test_boxplot(self):
1443
1450
1444
1451
df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
1445
1452
df ['X' ] = Series (['A' , 'A' , 'A' , 'A' , 'A' , 'B' , 'B' , 'B' , 'B' , 'B' ])
1453
+ df ['Y' ] = Series (['A' ] * 10 )
1446
1454
_check_plot_works (df .boxplot , by = 'X' )
1447
1455
1448
- # When ax is supplied, existing axes should be used:
1456
+ # When ax is supplied and required number of axes is 1,
1457
+ # passed ax should be used:
1449
1458
fig , ax = self .plt .subplots ()
1450
1459
axes = df .boxplot ('Col1' , by = 'X' , ax = ax )
1451
1460
self .assertIs (ax .get_axes (), axes )
1452
1461
1453
- # Multiple columns with an ax argument is not supported
1454
1462
fig , ax = self .plt .subplots ()
1455
- with tm .assertRaisesRegexp (ValueError , 'existing axis' ):
1456
- df .boxplot (column = ['Col1' , 'Col2' ], by = 'X' , ax = ax )
1463
+ axes = df .groupby ('Y' ).boxplot (ax = ax , return_type = 'axes' )
1464
+ self .assertIs (ax .get_axes (), axes ['A' ])
1465
+
1466
+ # Multiple columns with an ax argument should use same figure
1467
+ fig , ax = self .plt .subplots ()
1468
+ axes = df .boxplot (column = ['Col1' , 'Col2' ], by = 'X' , ax = ax , return_type = 'axes' )
1469
+ self .assertIs (axes ['Col1' ].get_figure (), fig )
1457
1470
1458
1471
# When by is None, check that all relevant lines are present in the dict
1459
1472
fig , ax = self .plt .subplots ()
@@ -2204,32 +2217,32 @@ class TestDataFrameGroupByPlots(TestPlotBase):
2204
2217
@slow
2205
2218
def test_boxplot (self ):
2206
2219
grouped = self .hist_df .groupby (by = 'gender' )
2207
- box = _check_plot_works (grouped .boxplot , return_type = 'dict ' )
2208
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 2 , layout = (1 , 2 ))
2220
+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes ' )
2221
+ self ._check_axes_shape (axes . values () , axes_num = 2 , layout = (1 , 2 ))
2209
2222
2210
- box = _check_plot_works (grouped .boxplot , subplots = False ,
2211
- return_type = 'dict ' )
2212
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 2 , layout = (1 , 2 ))
2223
+ axes = _check_plot_works (grouped .boxplot , subplots = False ,
2224
+ return_type = 'axes ' )
2225
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
2213
2226
2214
2227
tuples = lzip (string .ascii_letters [:10 ], range (10 ))
2215
2228
df = DataFrame (np .random .rand (10 , 3 ),
2216
2229
index = MultiIndex .from_tuples (tuples ))
2217
2230
2218
2231
grouped = df .groupby (level = 1 )
2219
- box = _check_plot_works (grouped .boxplot , return_type = 'dict ' )
2220
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 10 , layout = (4 , 3 ))
2232
+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes ' )
2233
+ self ._check_axes_shape (axes . values () , axes_num = 10 , layout = (4 , 3 ))
2221
2234
2222
- box = _check_plot_works (grouped .boxplot , subplots = False ,
2223
- return_type = 'dict ' )
2224
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 10 , layout = (4 , 3 ))
2235
+ axes = _check_plot_works (grouped .boxplot , subplots = False ,
2236
+ return_type = 'axes ' )
2237
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
2225
2238
2226
2239
grouped = df .unstack (level = 1 ).groupby (level = 0 , axis = 1 )
2227
- box = _check_plot_works (grouped .boxplot , return_type = 'dict ' )
2228
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 3 , layout = (2 , 2 ))
2240
+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes ' )
2241
+ self ._check_axes_shape (axes . values () , axes_num = 3 , layout = (2 , 2 ))
2229
2242
2230
- box = _check_plot_works (grouped .boxplot , subplots = False ,
2231
- return_type = 'dict ' )
2232
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 3 , layout = (2 , 2 ))
2243
+ axes = _check_plot_works (grouped .boxplot , subplots = False ,
2244
+ return_type = 'axes ' )
2245
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
2233
2246
2234
2247
def test_series_plot_color_kwargs (self ):
2235
2248
# GH1890
0 commit comments