@@ -62,9 +62,10 @@ def test_plot(self):
62
62
_check_plot_works (self .series [:10 ].plot , kind = 'barh' )
63
63
_check_plot_works (Series (randn (10 )).plot , kind = 'bar' , color = 'black' )
64
64
65
+ @slow
66
+ def test_plot_figsize_and_title (self ):
65
67
# figsize and title
66
68
import matplotlib .pyplot as plt
67
- plt .close ('all' )
68
69
ax = self .series .plot (title = 'Test' , figsize = (16 , 8 ))
69
70
70
71
self .assertEqual (ax .title .get_text (), 'Test' )
@@ -79,7 +80,6 @@ def test_bar_colors(self):
79
80
default_colors = plt .rcParams .get ('axes.color_cycle' )
80
81
custom_colors = 'rgcby'
81
82
82
- plt .close ('all' )
83
83
df = DataFrame (randn (5 , 5 ))
84
84
ax = df .plot (kind = 'bar' )
85
85
@@ -91,7 +91,7 @@ def test_bar_colors(self):
91
91
rs = rect .get_facecolor ()
92
92
self .assertEqual (xp , rs )
93
93
94
- plt .close ('all' )
94
+ tm .close ()
95
95
96
96
ax = df .plot (kind = 'bar' , color = custom_colors )
97
97
@@ -103,8 +103,7 @@ def test_bar_colors(self):
103
103
rs = rect .get_facecolor ()
104
104
self .assertEqual (xp , rs )
105
105
106
- plt .close ('all' )
107
-
106
+ tm .close ()
108
107
from matplotlib import cm
109
108
110
109
# Test str -> colormap functionality
@@ -118,7 +117,7 @@ def test_bar_colors(self):
118
117
rs = rect .get_facecolor ()
119
118
self .assertEqual (xp , rs )
120
119
121
- plt .close ('all' )
120
+ tm .close ()
122
121
123
122
# Test colormap functionality
124
123
ax = df .plot (kind = 'bar' , colormap = cm .jet )
@@ -131,8 +130,7 @@ def test_bar_colors(self):
131
130
rs = rect .get_facecolor ()
132
131
self .assertEqual (xp , rs )
133
132
134
- plt .close ('all' )
135
-
133
+ tm .close ()
136
134
df .ix [:, [0 ]].plot (kind = 'bar' , color = 'DodgerBlue' )
137
135
138
136
@slow
@@ -192,7 +190,7 @@ def test_hist(self):
192
190
_check_plot_works (self .ts .hist , ax = ax )
193
191
_check_plot_works (self .ts .hist , ax = ax , figure = fig )
194
192
_check_plot_works (self .ts .hist , figure = fig )
195
- plt .close ('all' )
193
+ tm .close ()
196
194
197
195
fig , (ax1 , ax2 ) = plt .subplots (1 , 2 )
198
196
_check_plot_works (self .ts .hist , figure = fig , ax = ax1 )
@@ -204,9 +202,8 @@ def test_hist(self):
204
202
@slow
205
203
def test_hist_layout (self ):
206
204
n = 10
207
- df = DataFrame ({'gender' : np .array (['Male' ,
208
- 'Female' ])[random .randint (2 ,
209
- size = n )],
205
+ gender = tm .choice (['Male' , 'Female' ], size = n )
206
+ df = DataFrame ({'gender' : gender ,
210
207
'height' : random .normal (66 , 4 , size = n ), 'weight' :
211
208
random .normal (161 , 32 , size = n )})
212
209
with tm .assertRaises (ValueError ):
@@ -219,23 +216,22 @@ def test_hist_layout(self):
219
216
def test_hist_layout_with_by (self ):
220
217
import matplotlib .pyplot as plt
221
218
n = 10
222
- df = DataFrame ({'gender' : np .array (['Male' ,
223
- 'Female' ])[random .randint (2 ,
224
- size = n )],
219
+ gender = tm .choice (['Male' , 'Female' ], size = n )
220
+ df = DataFrame ({'gender' : gender ,
225
221
'height' : random .normal (66 , 4 , size = n ), 'weight' :
226
222
random .normal (161 , 32 , size = n ),
227
223
'category' : random .randint (4 , size = n )})
228
224
_check_plot_works (df .height .hist , by = df .gender , layout = (2 , 1 ))
229
- plt .close ('all' )
225
+ tm .close ()
230
226
231
227
_check_plot_works (df .height .hist , by = df .gender , layout = (1 , 2 ))
232
- plt .close ('all' )
228
+ tm .close ()
233
229
234
230
_check_plot_works (df .weight .hist , by = df .category , layout = (1 , 4 ))
235
- plt .close ('all' )
231
+ tm .close ()
236
232
237
233
_check_plot_works (df .weight .hist , by = df .category , layout = (4 , 1 ))
238
- plt .close ('all' )
234
+ tm .close ()
239
235
240
236
@slow
241
237
def test_hist_no_overlap (self ):
@@ -256,6 +252,15 @@ def test_plot_fails_with_dupe_color_and_style(self):
256
252
with tm .assertRaises (ValueError ):
257
253
x .plot (style = 'k--' , color = 'k' )
258
254
255
+ @slow
256
+ def test_hist_by_no_extra_plots (self ):
257
+ import matplotlib .pyplot as plt
258
+ n = 10
259
+ df = DataFrame ({'gender' : tm .choice (['Male' , 'Female' ], size = n ),
260
+ 'height' : random .normal (66 , 4 , size = n )})
261
+ axes = df .height .hist (by = df .gender )
262
+ self .assertEqual (len (plt .get_fignums ()), 1 )
263
+
259
264
def test_plot_fails_when_ax_differs_from_figure (self ):
260
265
from pylab import figure , close
261
266
fig1 = figure ()
@@ -436,7 +441,6 @@ def test_plot_xy(self):
436
441
self ._check_data (df .plot (y = 1 ), df [1 ].plot ())
437
442
438
443
# figsize and title
439
- plt .close ('all' )
440
444
ax = df .plot (x = 1 , y = 2 , title = 'Test' , figsize = (16 , 8 ))
441
445
442
446
self .assertEqual (ax .title .get_text (), 'Test' )
@@ -456,26 +460,26 @@ def test_xcompat(self):
456
460
lines = ax .get_lines ()
457
461
self .assert_ (not isinstance (lines [0 ].get_xdata (), PeriodIndex ))
458
462
459
- plt .close ('all' )
463
+ tm .close ()
460
464
pd .plot_params ['xaxis.compat' ] = True
461
465
ax = df .plot ()
462
466
lines = ax .get_lines ()
463
467
self .assert_ (not isinstance (lines [0 ].get_xdata (), PeriodIndex ))
464
468
465
- plt .close ('all' )
469
+ tm .close ()
466
470
pd .plot_params ['x_compat' ] = False
467
471
ax = df .plot ()
468
472
lines = ax .get_lines ()
469
473
tm .assert_isinstance (lines [0 ].get_xdata (), PeriodIndex )
470
474
471
- plt .close ('all' )
475
+ tm .close ()
472
476
# useful if you're plotting a bunch together
473
477
with pd .plot_params .use ('x_compat' , True ):
474
478
ax = df .plot ()
475
479
lines = ax .get_lines ()
476
480
self .assert_ (not isinstance (lines [0 ].get_xdata (), PeriodIndex ))
477
481
478
- plt .close ('all' )
482
+ tm .close ()
479
483
ax = df .plot ()
480
484
lines = ax .get_lines ()
481
485
tm .assert_isinstance (lines [0 ].get_xdata (), PeriodIndex )
@@ -499,6 +503,7 @@ def check_line(xpl, rsl):
499
503
assert_array_equal (xpdata , rsdata )
500
504
501
505
[check_line (xpl , rsl ) for xpl , rsl in zip (xp_lines , rs_lines )]
506
+ tm .close ()
502
507
503
508
@slow
504
509
def test_subplots (self ):
@@ -537,19 +542,14 @@ def test_plot_bar(self):
537
542
columns = ['one' , 'two' , 'three' , 'four' ])
538
543
539
544
_check_plot_works (df .plot , kind = 'bar' )
540
- close ('all' )
541
545
_check_plot_works (df .plot , kind = 'bar' , legend = False )
542
- close ('all' )
543
546
_check_plot_works (df .plot , kind = 'bar' , subplots = True )
544
- close ('all' )
545
547
_check_plot_works (df .plot , kind = 'bar' , stacked = True )
546
- close ('all' )
547
548
548
549
df = DataFrame (randn (10 , 15 ),
549
550
index = list (string .ascii_letters [:10 ]),
550
551
columns = lrange (15 ))
551
552
_check_plot_works (df .plot , kind = 'bar' )
552
- close ('all' )
553
553
554
554
df = DataFrame ({'a' : [0 , 1 ], 'b' : [1 , 0 ]})
555
555
_check_plot_works (df .plot , kind = 'bar' )
@@ -678,18 +678,18 @@ def test_hist(self):
678
678
self .assertAlmostEqual (xtick .get_fontsize (), xf )
679
679
self .assertAlmostEqual (xtick .get_rotation (), xrot )
680
680
681
- plt .close ('all' )
681
+ tm .close ()
682
682
# make sure kwargs to hist are handled
683
683
ax = ser .hist (normed = True , cumulative = True , bins = 4 )
684
684
# height of last bin (index 5) must be 1.0
685
685
self .assertAlmostEqual (ax .get_children ()[5 ].get_height (), 1.0 )
686
686
687
- plt .close ('all' )
687
+ tm .close ()
688
688
ax = ser .hist (log = True )
689
689
# scale of y must be 'log'
690
690
self .assertEqual (ax .get_yscale (), 'log' )
691
691
692
- plt .close ('all' )
692
+ tm .close ()
693
693
694
694
# propagate attr exception from matplotlib.Axes.hist
695
695
with tm .assertRaises (AttributeError ):
@@ -698,7 +698,6 @@ def test_hist(self):
698
698
@slow
699
699
def test_hist_layout (self ):
700
700
import matplotlib .pyplot as plt
701
- plt .close ('all' )
702
701
df = DataFrame (randn (100 , 4 ))
703
702
704
703
layout_to_expected_size = (
@@ -847,15 +846,15 @@ def test_line_colors(self):
847
846
tmp = sys .stderr
848
847
sys .stderr = StringIO ()
849
848
try :
850
- plt .close ('all' )
849
+ tm .close ()
851
850
ax2 = df .plot (colors = custom_colors )
852
851
lines2 = ax2 .get_lines ()
853
852
for l1 , l2 in zip (lines , lines2 ):
854
853
self .assertEqual (l1 .get_color (), l2 .get_color ())
855
854
finally :
856
855
sys .stderr = tmp
857
856
858
- plt .close ('all' )
857
+ tm .close ()
859
858
860
859
ax = df .plot (colormap = 'jet' )
861
860
@@ -867,7 +866,7 @@ def test_line_colors(self):
867
866
rs = l .get_color ()
868
867
self .assertEqual (xp , rs )
869
868
870
- plt .close ('all' )
869
+ tm .close ()
871
870
872
871
ax = df .plot (colormap = cm .jet )
873
872
@@ -881,14 +880,13 @@ def test_line_colors(self):
881
880
882
881
# make color a list if plotting one column frame
883
882
# handles cases like df.plot(color='DodgerBlue')
884
- plt .close ('all' )
883
+ tm .close ()
885
884
df .ix [:, [0 ]].plot (color = 'DodgerBlue' )
886
885
887
886
def test_default_color_cycle (self ):
888
887
import matplotlib .pyplot as plt
889
888
plt .rcParams ['axes.color_cycle' ] = list ('rgbk' )
890
889
891
- plt .close ('all' )
892
890
df = DataFrame (randn (5 , 3 ))
893
891
ax = df .plot ()
894
892
@@ -992,15 +990,15 @@ def test_grouped_hist(self):
992
990
axes = plotting .grouped_hist (df .A , by = df .C )
993
991
self .assertEqual (len (axes .ravel ()), 4 )
994
992
995
- plt .close ('all' )
993
+ tm .close ()
996
994
axes = df .hist (by = df .C )
997
995
self .assertEqual (axes .ndim , 2 )
998
996
self .assertEqual (len (axes .ravel ()), 4 )
999
997
1000
998
for ax in axes .ravel ():
1001
999
self .assert_ (len (ax .patches ) > 0 )
1002
1000
1003
- plt .close ('all' )
1001
+ tm .close ()
1004
1002
# make sure kwargs to hist are handled
1005
1003
axes = plotting .grouped_hist (df .A , by = df .C , normed = True ,
1006
1004
cumulative = True , bins = 4 )
@@ -1010,14 +1008,13 @@ def test_grouped_hist(self):
1010
1008
height = ax .get_children ()[5 ].get_height ()
1011
1009
self .assertAlmostEqual (height , 1.0 )
1012
1010
1013
- plt .close ('all' )
1011
+ tm .close ()
1014
1012
axes = plotting .grouped_hist (df .A , by = df .C , log = True )
1015
1013
# scale of y must be 'log'
1016
1014
for ax in axes .ravel ():
1017
1015
self .assertEqual (ax .get_yscale (), 'log' )
1018
1016
1019
- plt .close ('all' )
1020
-
1017
+ tm .close ()
1021
1018
# propagate attr exception from matplotlib.Axes.hist
1022
1019
with tm .assertRaises (AttributeError ):
1023
1020
plotting .grouped_hist (df .A , by = df .C , foo = 'bar' )
@@ -1026,9 +1023,8 @@ def test_grouped_hist(self):
1026
1023
def test_grouped_hist_layout (self ):
1027
1024
import matplotlib .pyplot as plt
1028
1025
n = 100
1029
- df = DataFrame ({'gender' : np .array (['Male' ,
1030
- 'Female' ])[random .randint (2 ,
1031
- size = n )],
1026
+ gender = tm .choice (['Male' , 'Female' ], size = n )
1027
+ df = DataFrame ({'gender' : gender ,
1032
1028
'height' : random .normal (66 , 4 , size = n ),
1033
1029
'weight' : random .normal (161 , 32 , size = n ),
1034
1030
'category' : random .randint (4 , size = n )})
@@ -1042,10 +1038,10 @@ def test_grouped_hist_layout(self):
1042
1038
layout = (2 , 1 ))
1043
1039
self .assertEqual (df .hist (column = 'height' , by = df .gender ,
1044
1040
layout = (2 , 1 )).shape , (2 ,))
1045
- plt .close ('all' )
1041
+ tm .close ()
1046
1042
self .assertEqual (df .hist (column = 'height' , by = df .category ,
1047
1043
layout = (4 , 1 )).shape , (4 ,))
1048
- plt .close ('all' )
1044
+ tm .close ()
1049
1045
self .assertEqual (df .hist (column = 'height' , by = df .category ,
1050
1046
layout = (4 , 2 )).shape , (4 , 2 ))
1051
1047
0 commit comments