@@ -859,6 +859,13 @@ def test_plot(self):
859859 axes = _check_plot_works (df .plot , kind = 'bar' , subplots = True )
860860 self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
861861
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+
862869 def test_nonnumeric_exclude (self ):
863870 df = DataFrame ({'A' : ["x" , "y" , "z" ], 'B' : [1 , 2 , 3 ]})
864871 ax = df .plot ()
@@ -1419,17 +1426,23 @@ def test_boxplot(self):
14191426
14201427 df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
14211428 df ['X' ] = Series (['A' , 'A' , 'A' , 'A' , 'A' , 'B' , 'B' , 'B' , 'B' , 'B' ])
1429+ df ['Y' ] = Series (['A' ] * 10 )
14221430 _check_plot_works (df .boxplot , by = 'X' )
14231431
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:
14251434 fig , ax = self .plt .subplots ()
14261435 axes = df .boxplot ('Col1' , by = 'X' , ax = ax )
14271436 self .assertIs (ax .get_axes (), axes )
14281437
1429- # Multiple columns with an ax argument is not supported
14301438 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 )
14331446
14341447 # When by is None, check that all relevant lines are present in the dict
14351448 fig , ax = self .plt .subplots ()
@@ -2180,32 +2193,32 @@ class TestDataFrameGroupByPlots(TestPlotBase):
21802193 @slow
21812194 def test_boxplot (self ):
21822195 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 ))
21852198
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 ))
21892202
21902203 tuples = lzip (string .ascii_letters [:10 ], range (10 ))
21912204 df = DataFrame (np .random .rand (10 , 3 ),
21922205 index = MultiIndex .from_tuples (tuples ))
21932206
21942207 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 ))
21972210
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 ))
22012214
22022215 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 ))
22052218
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 ))
22092222
22102223 def test_series_plot_color_kwargs (self ):
22112224 # GH1890
0 commit comments