@@ -883,6 +883,13 @@ def test_plot(self):
883883 axes = _check_plot_works (df .plot , kind = 'bar' , subplots = True )
884884 self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
885885
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+
886893 def test_nonnumeric_exclude (self ):
887894 df = DataFrame ({'A' : ["x" , "y" , "z" ], 'B' : [1 , 2 , 3 ]})
888895 ax = df .plot ()
@@ -1443,17 +1450,23 @@ def test_boxplot(self):
14431450
14441451 df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
14451452 df ['X' ] = Series (['A' , 'A' , 'A' , 'A' , 'A' , 'B' , 'B' , 'B' , 'B' , 'B' ])
1453+ df ['Y' ] = Series (['A' ] * 10 )
14461454 _check_plot_works (df .boxplot , by = 'X' )
14471455
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:
14491458 fig , ax = self .plt .subplots ()
14501459 axes = df .boxplot ('Col1' , by = 'X' , ax = ax )
14511460 self .assertIs (ax .get_axes (), axes )
14521461
1453- # Multiple columns with an ax argument is not supported
14541462 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 )
14571470
14581471 # When by is None, check that all relevant lines are present in the dict
14591472 fig , ax = self .plt .subplots ()
@@ -2204,32 +2217,32 @@ class TestDataFrameGroupByPlots(TestPlotBase):
22042217 @slow
22052218 def test_boxplot (self ):
22062219 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 ))
22092222
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 ))
22132226
22142227 tuples = lzip (string .ascii_letters [:10 ], range (10 ))
22152228 df = DataFrame (np .random .rand (10 , 3 ),
22162229 index = MultiIndex .from_tuples (tuples ))
22172230
22182231 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 ))
22212234
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 ))
22252238
22262239 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 ))
22292242
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 ))
22332246
22342247 def test_series_plot_color_kwargs (self ):
22352248 # GH1890
0 commit comments