55import itertools
66import os
77import string
8+ import warnings
89from distutils .version import LooseVersion
910
1011from datetime import datetime , date
@@ -1297,12 +1298,12 @@ def test_subplots_multiple_axes(self):
12971298 df = DataFrame (np .random .rand (10 , 3 ),
12981299 index = list (string .ascii_letters [:10 ]))
12991300
1300- returned = df .plot (subplots = True , ax = axes [0 ])
1301+ returned = df .plot (subplots = True , ax = axes [0 ], sharex = False , sharey = False )
13011302 self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
13021303 self .assertEqual (returned .shape , (3 , ))
13031304 self .assertIs (returned [0 ].figure , fig )
13041305 # draw on second row
1305- returned = df .plot (subplots = True , ax = axes [1 ])
1306+ returned = df .plot (subplots = True , ax = axes [1 ], sharex = False , sharey = False )
13061307 self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
13071308 self .assertEqual (returned .shape , (3 , ))
13081309 self .assertIs (returned [0 ].figure , fig )
@@ -1319,26 +1320,32 @@ def test_subplots_multiple_axes(self):
13191320 # (show warning is tested in
13201321 # TestDataFrameGroupByPlots.test_grouped_box_multiple_axes
13211322 fig , axes = self .plt .subplots (2 , 2 )
1322- df = DataFrame (np .random .rand (10 , 4 ),
1323- index = list (string .ascii_letters [:10 ]))
1324-
1325- returned = df .plot (subplots = True , ax = axes , layout = (2 , 1 ))
1326- self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
1327- self .assertEqual (returned .shape , (4 , ))
1328-
1329- returned = df .plot (subplots = True , ax = axes , layout = (2 , - 1 ))
1330- self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
1331- self .assertEqual (returned .shape , (4 , ))
1332-
1333- returned = df .plot (subplots = True , ax = axes , layout = (- 1 , 2 ))
1323+ with warnings .catch_warnings ():
1324+ warnings .simplefilter ('ignore' )
1325+ df = DataFrame (np .random .rand (10 , 4 ),
1326+ index = list (string .ascii_letters [:10 ]))
1327+
1328+ returned = df .plot (subplots = True , ax = axes , layout = (2 , 1 ),
1329+ sharex = False , sharey = False )
1330+ self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
1331+ self .assertEqual (returned .shape , (4 , ))
1332+
1333+ returned = df .plot (subplots = True , ax = axes , layout = (2 , - 1 ),
1334+ sharex = False , sharey = False )
1335+ self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
1336+ self .assertEqual (returned .shape , (4 , ))
1337+
1338+ returned = df .plot (subplots = True , ax = axes , layout = (- 1 , 2 ),
1339+ sharex = False , sharey = False )
13341340 self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
13351341 self .assertEqual (returned .shape , (4 , ))
13361342
13371343 # single column
13381344 fig , axes = self .plt .subplots (1 , 1 )
13391345 df = DataFrame (np .random .rand (10 , 1 ),
13401346 index = list (string .ascii_letters [:10 ]))
1341- axes = df .plot (subplots = True , ax = [axes ])
1347+
1348+ axes = df .plot (subplots = True , ax = [axes ], sharex = False , sharey = False )
13421349 self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
13431350 self .assertEqual (axes .shape , (1 , ))
13441351
@@ -3122,13 +3129,14 @@ class TestDataFrameGroupByPlots(TestPlotBase):
31223129 @slow
31233130 def test_boxplot (self ):
31243131 grouped = self .hist_df .groupby (by = 'gender' )
3125- axes = _check_plot_works (grouped .boxplot , return_type = 'axes' )
3132+ with warnings .catch_warnings ():
3133+ warnings .simplefilter ('ignore' )
3134+ axes = _check_plot_works (grouped .boxplot , return_type = 'axes' )
31263135 self ._check_axes_shape (list (axes .values ()), axes_num = 2 , layout = (1 , 2 ))
31273136
31283137 axes = _check_plot_works (grouped .boxplot , subplots = False ,
31293138 return_type = 'axes' )
31303139 self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
3131-
31323140 tuples = lzip (string .ascii_letters [:10 ], range (10 ))
31333141 df = DataFrame (np .random .rand (10 , 3 ),
31343142 index = MultiIndex .from_tuples (tuples ))
@@ -3149,6 +3157,30 @@ def test_boxplot(self):
31493157 return_type = 'axes' )
31503158 self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
31513159
3160+ @slow
3161+ def test_grouped_plot_fignums (self ):
3162+ n = 10
3163+ weight = Series (np .random .normal (166 , 20 , size = n ))
3164+ height = Series (np .random .normal (60 , 10 , size = n ))
3165+ with tm .RNGContext (42 ):
3166+ gender = tm .choice (['male' , 'female' ], size = n )
3167+ df = DataFrame ({'height' : height , 'weight' : weight , 'gender' : gender })
3168+ gb = df .groupby ('gender' )
3169+
3170+ res = gb .plot ()
3171+ self .assertEqual (len (self .plt .get_fignums ()), 2 )
3172+ self .assertEqual (len (res ), 2 )
3173+ tm .close ()
3174+
3175+ res = gb .boxplot (return_type = 'axes' )
3176+ self .assertEqual (len (self .plt .get_fignums ()), 1 )
3177+ self .assertEqual (len (res ), 2 )
3178+ tm .close ()
3179+
3180+ # now works with GH 5610 as gender is excluded
3181+ res = df .groupby ('gender' ).hist ()
3182+ tm .close ()
3183+
31523184 def test_series_plot_color_kwargs (self ):
31533185 # GH1890
31543186 ax = Series (np .arange (12 ) + 1 ).plot (color = 'green' )
@@ -3219,6 +3251,21 @@ def test_grouped_hist(self):
32193251 with tm .assert_produces_warning (FutureWarning ):
32203252 df .hist (by = 'C' , figsize = 'default' )
32213253
3254+ @slow
3255+ def test_grouped_hist2 (self ):
3256+ n = 10
3257+ weight = Series (np .random .normal (166 , 20 , size = n ))
3258+ height = Series (np .random .normal (60 , 10 , size = n ))
3259+ with tm .RNGContext (42 ):
3260+ gender_int = tm .choice ([0 , 1 ], size = n )
3261+ df_int = DataFrame ({'height' : height , 'weight' : weight ,
3262+ 'gender' : gender_int })
3263+ gb = df_int .groupby ('gender' )
3264+ axes = gb .hist ()
3265+ self .assertEqual (len (axes ), 2 )
3266+ self .assertEqual (len (self .plt .get_fignums ()), 2 )
3267+ tm .close ()
3268+
32223269 @slow
32233270 def test_grouped_box_return_type (self ):
32243271 df = self .hist_df
@@ -3334,15 +3381,21 @@ def test_grouped_box_multiple_axes(self):
33343381 self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 , layout = (2 , 2 ))
33353382
33363383 fig , axes = self .plt .subplots (2 , 3 )
3337- returned = df .boxplot (column = ['height' , 'weight' , 'category' ], by = 'gender' ,
3338- return_type = 'axes' , ax = axes [0 ])
3384+ with warnings .catch_warnings ():
3385+ warnings .simplefilter ('ignore' )
3386+ returned = df .boxplot (column = ['height' , 'weight' , 'category' ],
3387+ by = 'gender' , return_type = 'axes' , ax = axes [0 ])
33393388 returned = np .array (list (returned .values ()))
33403389 self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
33413390 self .assert_numpy_array_equal (returned , axes [0 ])
33423391 self .assertIs (returned [0 ].figure , fig )
3392+
33433393 # draw on second row
3344- returned = df .groupby ('classroom' ).boxplot (column = ['height' , 'weight' , 'category' ],
3345- return_type = 'axes' , ax = axes [1 ])
3394+ with warnings .catch_warnings ():
3395+ warnings .simplefilter ('ignore' )
3396+ returned = df .groupby ('classroom' ).boxplot (
3397+ column = ['height' , 'weight' , 'category' ],
3398+ return_type = 'axes' , ax = axes [1 ])
33463399 returned = np .array (list (returned .values ()))
33473400 self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
33483401 self .assert_numpy_array_equal (returned , axes [1 ])
@@ -3469,6 +3522,32 @@ def test_invalid_colormap(self):
34693522 with tm .assertRaises (ValueError ):
34703523 df .plot (colormap = 'invalid_colormap' )
34713524
3525+ def test_series_groupby_plotting_nominally_works (self ):
3526+ n = 10
3527+ weight = Series (np .random .normal (166 , 20 , size = n ))
3528+ height = Series (np .random .normal (60 , 10 , size = n ))
3529+ with tm .RNGContext (42 ):
3530+ gender = tm .choice (['male' , 'female' ], size = n )
3531+
3532+ weight .groupby (gender ).plot ()
3533+ tm .close ()
3534+ height .groupby (gender ).hist ()
3535+ tm .close ()
3536+ #Regression test for GH8733
3537+ height .groupby (gender ).plot (alpha = 0.5 )
3538+ tm .close ()
3539+
3540+ def test_plotting_with_float_index_works (self ):
3541+ # GH 7025
3542+ df = DataFrame ({'def' : [1 ,1 ,1 ,2 ,2 ,2 ,3 ,3 ,3 ],
3543+ 'val' : np .random .randn (9 )},
3544+ index = [1.0 ,2.0 ,3.0 ,1.0 ,2.0 ,3.0 ,1.0 ,2.0 ,3.0 ])
3545+
3546+ df .groupby ('def' )['val' ].plot ()
3547+ tm .close ()
3548+ df .groupby ('def' )['val' ].apply (lambda x : x .plot ())
3549+ tm .close ()
3550+
34723551
34733552def assert_is_valid_plot_return_object (objs ):
34743553 import matplotlib .pyplot as plt
0 commit comments