5
5
import itertools
6
6
import os
7
7
import string
8
+ import warnings
8
9
from distutils .version import LooseVersion
9
10
10
11
from datetime import datetime , date
@@ -1297,12 +1298,12 @@ def test_subplots_multiple_axes(self):
1297
1298
df = DataFrame (np .random .rand (10 , 3 ),
1298
1299
index = list (string .ascii_letters [:10 ]))
1299
1300
1300
- returned = df .plot (subplots = True , ax = axes [0 ])
1301
+ returned = df .plot (subplots = True , ax = axes [0 ], sharex = False , sharey = False )
1301
1302
self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
1302
1303
self .assertEqual (returned .shape , (3 , ))
1303
1304
self .assertIs (returned [0 ].figure , fig )
1304
1305
# 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 )
1306
1307
self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
1307
1308
self .assertEqual (returned .shape , (3 , ))
1308
1309
self .assertIs (returned [0 ].figure , fig )
@@ -1319,26 +1320,32 @@ def test_subplots_multiple_axes(self):
1319
1320
# (show warning is tested in
1320
1321
# TestDataFrameGroupByPlots.test_grouped_box_multiple_axes
1321
1322
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 )
1334
1340
self ._check_axes_shape (returned , axes_num = 4 , layout = (2 , 2 ))
1335
1341
self .assertEqual (returned .shape , (4 , ))
1336
1342
1337
1343
# single column
1338
1344
fig , axes = self .plt .subplots (1 , 1 )
1339
1345
df = DataFrame (np .random .rand (10 , 1 ),
1340
1346
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 )
1342
1349
self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
1343
1350
self .assertEqual (axes .shape , (1 , ))
1344
1351
@@ -3122,13 +3129,14 @@ class TestDataFrameGroupByPlots(TestPlotBase):
3122
3129
@slow
3123
3130
def test_boxplot (self ):
3124
3131
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' )
3126
3135
self ._check_axes_shape (list (axes .values ()), axes_num = 2 , layout = (1 , 2 ))
3127
3136
3128
3137
axes = _check_plot_works (grouped .boxplot , subplots = False ,
3129
3138
return_type = 'axes' )
3130
3139
self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
3131
-
3132
3140
tuples = lzip (string .ascii_letters [:10 ], range (10 ))
3133
3141
df = DataFrame (np .random .rand (10 , 3 ),
3134
3142
index = MultiIndex .from_tuples (tuples ))
@@ -3149,6 +3157,30 @@ def test_boxplot(self):
3149
3157
return_type = 'axes' )
3150
3158
self ._check_axes_shape (axes , axes_num = 1 , layout = (1 , 1 ))
3151
3159
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
+
3152
3184
def test_series_plot_color_kwargs (self ):
3153
3185
# GH1890
3154
3186
ax = Series (np .arange (12 ) + 1 ).plot (color = 'green' )
@@ -3219,6 +3251,21 @@ def test_grouped_hist(self):
3219
3251
with tm .assert_produces_warning (FutureWarning ):
3220
3252
df .hist (by = 'C' , figsize = 'default' )
3221
3253
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
+
3222
3269
@slow
3223
3270
def test_grouped_box_return_type (self ):
3224
3271
df = self .hist_df
@@ -3334,15 +3381,21 @@ def test_grouped_box_multiple_axes(self):
3334
3381
self ._check_axes_shape (self .plt .gcf ().axes , axes_num = 4 , layout = (2 , 2 ))
3335
3382
3336
3383
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 ])
3339
3388
returned = np .array (list (returned .values ()))
3340
3389
self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
3341
3390
self .assert_numpy_array_equal (returned , axes [0 ])
3342
3391
self .assertIs (returned [0 ].figure , fig )
3392
+
3343
3393
# 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 ])
3346
3399
returned = np .array (list (returned .values ()))
3347
3400
self ._check_axes_shape (returned , axes_num = 3 , layout = (1 , 3 ))
3348
3401
self .assert_numpy_array_equal (returned , axes [1 ])
@@ -3469,6 +3522,32 @@ def test_invalid_colormap(self):
3469
3522
with tm .assertRaises (ValueError ):
3470
3523
df .plot (colormap = 'invalid_colormap' )
3471
3524
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
+
3472
3551
3473
3552
def assert_is_valid_plot_return_object (objs ):
3474
3553
import matplotlib .pyplot as plt
0 commit comments