@@ -859,6 +859,13 @@ def test_plot(self):
859
859
axes = _check_plot_works (df .plot , kind = 'bar' , subplots = True )
860
860
self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
861
861
862
+ # When ax is supplied and required number of axes is 1,
863
+ # passed ax should be used:
864
+ fig , ax = self .plt .subplots ()
865
+ axes = df .plot (kind = 'bar' , subplots = True , ax = ax )
866
+ self .assertEqual (len (axes ), 1 )
867
+ self .assertIs (ax .get_axes (), axes [0 ])
868
+
862
869
def test_nonnumeric_exclude (self ):
863
870
df = DataFrame ({'A' : ["x" , "y" , "z" ], 'B' : [1 , 2 , 3 ]})
864
871
ax = df .plot ()
@@ -1419,17 +1426,23 @@ def test_boxplot(self):
1419
1426
1420
1427
df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
1421
1428
df ['X' ] = Series (['A' , 'A' , 'A' , 'A' , 'A' , 'B' , 'B' , 'B' , 'B' , 'B' ])
1429
+ df ['Y' ] = Series (['A' ] * 10 )
1422
1430
_check_plot_works (df .boxplot , by = 'X' )
1423
1431
1424
- # When ax is supplied, existing axes should be used:
1432
+ # When ax is supplied and required number of axes is 1,
1433
+ # passed ax should be used:
1425
1434
fig , ax = self .plt .subplots ()
1426
1435
axes = df .boxplot ('Col1' , by = 'X' , ax = ax )
1427
1436
self .assertIs (ax .get_axes (), axes )
1428
1437
1429
- # Multiple columns with an ax argument is not supported
1430
1438
fig , ax = self .plt .subplots ()
1431
- with tm .assertRaisesRegexp (ValueError , 'existing axis' ):
1432
- df .boxplot (column = ['Col1' , 'Col2' ], by = 'X' , ax = ax )
1439
+ axes = df .groupby ('Y' ).boxplot (ax = ax , return_type = 'axes' )
1440
+ self .assertIs (ax .get_axes (), axes ['A' ])
1441
+
1442
+ # Multiple columns with an ax argument should use same figure
1443
+ fig , ax = self .plt .subplots ()
1444
+ axes = df .boxplot (column = ['Col1' , 'Col2' ], by = 'X' , ax = ax , return_type = 'axes' )
1445
+ self .assertIs (axes ['Col1' ].get_figure (), fig )
1433
1446
1434
1447
# When by is None, check that all relevant lines are present in the dict
1435
1448
fig , ax = self .plt .subplots ()
@@ -2180,32 +2193,32 @@ class TestDataFrameGroupByPlots(TestPlotBase):
2180
2193
@slow
2181
2194
def test_boxplot (self ):
2182
2195
grouped = self .hist_df .groupby (by = 'gender' )
2183
- box = _check_plot_works (grouped .boxplot , return_type = 'dict ' )
2184
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 2 , layout = (1 , 2 ))
2196
+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes ' )
2197
+ self ._check_axes_shape (axes . values () , axes_num = 2 , layout = (1 , 2 ))
2185
2198
2186
- box = _check_plot_works (grouped .boxplot , subplots = False ,
2187
- return_type = 'dict ' )
2188
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 2 , layout = (1 , 2 ))
2199
+ axes = _check_plot_works (grouped .boxplot , subplots = False ,
2200
+ return_type = 'axes ' )
2201
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
2189
2202
2190
2203
tuples = lzip (string .ascii_letters [:10 ], range (10 ))
2191
2204
df = DataFrame (np .random .rand (10 , 3 ),
2192
2205
index = MultiIndex .from_tuples (tuples ))
2193
2206
2194
2207
grouped = df .groupby (level = 1 )
2195
- box = _check_plot_works (grouped .boxplot , return_type = 'dict ' )
2196
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 10 , layout = (4 , 3 ))
2208
+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes ' )
2209
+ self ._check_axes_shape (axes . values () , axes_num = 10 , layout = (4 , 3 ))
2197
2210
2198
- box = _check_plot_works (grouped .boxplot , subplots = False ,
2199
- return_type = 'dict ' )
2200
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 10 , layout = (4 , 3 ))
2211
+ axes = _check_plot_works (grouped .boxplot , subplots = False ,
2212
+ return_type = 'axes ' )
2213
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
2201
2214
2202
2215
grouped = df .unstack (level = 1 ).groupby (level = 0 , axis = 1 )
2203
- box = _check_plot_works (grouped .boxplot , return_type = 'dict ' )
2204
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 3 , layout = (2 , 2 ))
2216
+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes ' )
2217
+ self ._check_axes_shape (axes . values () , axes_num = 3 , layout = (2 , 2 ))
2205
2218
2206
- box = _check_plot_works (grouped .boxplot , subplots = False ,
2207
- return_type = 'dict ' )
2208
- self ._check_axes_shape (self . plt . gcf (). axes , axes_num = 3 , layout = (2 , 2 ))
2219
+ axes = _check_plot_works (grouped .boxplot , subplots = False ,
2220
+ return_type = 'axes ' )
2221
+ self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
2209
2222
2210
2223
def test_series_plot_color_kwargs (self ):
2211
2224
# GH1890
0 commit comments