@@ -26,7 +26,6 @@ def _skip_if_no_scipy():
26
26
27
27
28
28
class TestSeriesPlots (unittest .TestCase ):
29
-
30
29
@classmethod
31
30
def setUpClass (cls ):
32
31
try :
@@ -45,6 +44,10 @@ def setUp(self):
45
44
self .iseries = tm .makePeriodSeries ()
46
45
self .iseries .name = 'iseries'
47
46
47
+ def tearDown (self ):
48
+ import matplotlib .pyplot as plt
49
+ plt .close ('all' )
50
+
48
51
@slow
49
52
def test_plot (self ):
50
53
_check_plot_works (self .ts .plot , label = 'foo' )
@@ -178,6 +181,19 @@ def test_hist(self):
178
181
_check_plot_works (self .ts .hist , figsize = (8 , 10 ))
179
182
_check_plot_works (self .ts .hist , by = self .ts .index .month )
180
183
184
+ import matplotlib .pyplot as plt
185
+ fig , ax = plt .subplots (1 , 1 )
186
+ _check_plot_works (self .ts .hist , ax = ax )
187
+ _check_plot_works (self .ts .hist , ax = ax , figure = fig )
188
+ _check_plot_works (self .ts .hist , figure = fig )
189
+ plt .close ('all' )
190
+
191
+ fig , (ax1 , ax2 ) = plt .subplots (1 , 2 )
192
+ _check_plot_works (self .ts .hist , figure = fig , ax = ax1 )
193
+ _check_plot_works (self .ts .hist , figure = fig , ax = ax2 )
194
+ self .assertRaises (ValueError , self .ts .hist , by = self .ts .index ,
195
+ figure = fig )
196
+
181
197
def test_plot_fails_when_ax_differs_from_figure (self ):
182
198
from pylab import figure
183
199
fig1 = figure ()
@@ -196,11 +212,10 @@ def test_kde(self):
196
212
@slow
197
213
def test_kde_color (self ):
198
214
_skip_if_no_scipy ()
199
- _check_plot_works (self .ts .plot , kind = 'kde' )
200
- _check_plot_works (self .ts .plot , kind = 'density' )
201
215
ax = self .ts .plot (kind = 'kde' , logy = True , color = 'r' )
202
- self .assert_ (ax .get_lines ()[0 ].get_color () == 'r' )
203
- self .assert_ (ax .get_lines ()[1 ].get_color () == 'r' )
216
+ lines = ax .get_lines ()
217
+ self .assertEqual (len (lines ), 1 )
218
+ self .assertEqual (lines [0 ].get_color (), 'r' )
204
219
205
220
@slow
206
221
def test_autocorrelation_plot (self ):
@@ -228,7 +243,6 @@ def test_invalid_plot_data(self):
228
243
229
244
@slow
230
245
def test_valid_object_plot (self ):
231
- from pandas .io .common import PerformanceWarning
232
246
s = Series (range (10 ), dtype = object )
233
247
kinds = 'line' , 'bar' , 'barh' , 'kde' , 'density'
234
248
@@ -262,6 +276,10 @@ def setUpClass(cls):
262
276
except ImportError :
263
277
raise nose .SkipTest
264
278
279
+ def tearDown (self ):
280
+ import matplotlib .pyplot as plt
281
+ plt .close ('all' )
282
+
265
283
@slow
266
284
def test_plot (self ):
267
285
df = tm .makeTimeDataFrame ()
@@ -804,19 +822,18 @@ def test_invalid_kind(self):
804
822
805
823
806
824
class TestDataFrameGroupByPlots (unittest .TestCase ):
807
-
808
825
@classmethod
809
826
def setUpClass (cls ):
810
- # import sys
811
- # if 'IPython' in sys.modules:
812
- # raise nose.SkipTest
813
-
814
827
try :
815
828
import matplotlib as mpl
816
829
mpl .use ('Agg' , warn = False )
817
830
except ImportError :
818
831
raise nose .SkipTest
819
832
833
+ def tearDown (self ):
834
+ import matplotlib .pyplot as plt
835
+ plt .close ('all' )
836
+
820
837
@slow
821
838
def test_boxplot (self ):
822
839
df = DataFrame (np .random .rand (10 , 2 ), columns = ['Col1' , 'Col2' ])
@@ -906,12 +923,6 @@ def test_grouped_hist(self):
906
923
by = df .C , foo = 'bar' )
907
924
908
925
def test_option_mpl_style (self ):
909
- # just a sanity check
910
- try :
911
- import matplotlib
912
- except :
913
- raise nose .SkipTest
914
-
915
926
set_option ('display.mpl_style' , 'default' )
916
927
set_option ('display.mpl_style' , None )
917
928
set_option ('display.mpl_style' , False )
@@ -925,22 +936,43 @@ def test_invalid_colormap(self):
925
936
926
937
self .assertRaises (ValueError , df .plot , colormap = 'invalid_colormap' )
927
938
939
+
940
+ def assert_is_valid_plot_return_object (objs ):
941
+ import matplotlib .pyplot as plt
942
+ if isinstance (objs , np .ndarray ):
943
+ for el in objs .flat :
944
+ assert isinstance (el , plt .Axes ), ('one of \' objs\' is not a '
945
+ 'matplotlib Axes instance, '
946
+ 'type encountered {0!r}'
947
+ '' .format (el .__class__ .__name__ ))
948
+ else :
949
+ assert isinstance (objs , (plt .Artist , tuple , dict )), \
950
+ ('objs is neither an ndarray of Artist instances nor a '
951
+ 'single Artist instance, tuple, or dict, "objs" is a {0!r} '
952
+ '' .format (objs .__class__ .__name__ ))
953
+
954
+
928
955
def _check_plot_works (f , * args , ** kwargs ):
929
956
import matplotlib .pyplot as plt
930
957
931
- fig = plt .gcf ()
958
+ try :
959
+ fig = kwargs ['figure' ]
960
+ except KeyError :
961
+ fig = plt .gcf ()
932
962
plt .clf ()
933
- ax = fig .add_subplot (211 )
963
+ ax = kwargs . get ( 'ax' , fig .add_subplot (211 ) )
934
964
ret = f (* args , ** kwargs )
935
- assert ret is not None # do something more intelligent
936
965
937
- ax = fig .add_subplot (212 )
966
+ assert ret is not None
967
+ assert_is_valid_plot_return_object (ret )
968
+
938
969
try :
939
- kwargs ['ax' ] = ax
970
+ kwargs ['ax' ] = fig . add_subplot ( 212 )
940
971
ret = f (* args , ** kwargs )
941
- assert (ret is not None ) # do something more intelligent
942
972
except Exception :
943
973
pass
974
+ else :
975
+ assert_is_valid_plot_return_object (ret )
944
976
945
977
with ensure_clean () as path :
946
978
plt .savefig (path )
0 commit comments