@@ -828,6 +828,58 @@ def plotf(ax, y, style=None, column_num=None, **kwds):
828
828
return plotf
829
829
830
830
831
+ def _boxplot_plotf (return_type ):
832
+ def plotf (ax , y , column_num = None , ** kwds ):
833
+ if y .ndim == 2 :
834
+ y = [remove_na (v ) for v in y ]
835
+ # Boxplot fails with empty arrays, so need to add a NaN
836
+ # if any cols are empty
837
+ # GH 8181
838
+ y = [v if v .size > 0 else np .array ([np .nan ]) for v in y ]
839
+ else :
840
+ y = remove_na (y )
841
+ bp = ax .boxplot (y , ** kwds )
842
+
843
+ if return_type == 'dict' :
844
+ return bp , bp
845
+ elif return_type == 'both' :
846
+ return BoxPlot .BP (ax = ax , lines = bp ), bp
847
+ else :
848
+ return ax , bp
849
+
850
+ return plotf
851
+
852
+
853
+ def _kdeplot_plotf (f , bw_method , ind ):
854
+ from scipy .stats import gaussian_kde
855
+ from scipy import __version__ as spv
856
+
857
+ def plotf (ax , y , style = None , column_num = None , ** kwds ):
858
+ y = remove_na (y )
859
+ if LooseVersion (spv ) >= '0.11.0' :
860
+ gkde = gaussian_kde (y , bw_method = bw_method )
861
+ else :
862
+ gkde = gaussian_kde (y )
863
+ if bw_method is not None :
864
+ msg = ('bw_method was added in Scipy 0.11.0.' +
865
+ ' Scipy version in use is %s.' % spv )
866
+ warnings .warn (msg )
867
+
868
+ if ind is None :
869
+ sample_range = max (y ) - min (y )
870
+ ind_local = np .linspace (min (y ) - 0.5 * sample_range ,
871
+ max (y ) + 0.5 * sample_range , 1000 )
872
+ else :
873
+ ind_local = ind
874
+
875
+ y = gkde .evaluate (ind_local )
876
+ lines = f (ax , ind_local , y , style = style , ** kwds )
877
+ return lines
878
+
879
+ return plotf
880
+
881
+
882
+
831
883
class MPLPlot (object ):
832
884
"""
833
885
Base class for assembling a pandas plot using matplotlib
@@ -1258,14 +1310,15 @@ def _is_datetype(self):
1258
1310
index .inferred_type in ('datetime' , 'date' , 'datetime64' ,
1259
1311
'time' ))
1260
1312
1313
+ def _plot_errors (self ):
1314
+ return any (e is not None for e in self .errors .values ())
1315
+
1261
1316
def _get_plot_function (self ):
1262
1317
'''
1263
1318
Returns the matplotlib plotting function (plot or errorbar) based on
1264
1319
the presence of errorbar keywords.
1265
1320
'''
1266
- errorbar = any (e is not None for e in self .errors .values ())
1267
-
1268
- return _mplplot_plotf (errorbar )
1321
+ return _mplplot_plotf (self ._plot_errors ())
1269
1322
1270
1323
def _get_index_name (self ):
1271
1324
if isinstance (self .data .index , MultiIndex ):
@@ -2030,35 +2083,9 @@ def __init__(self, data, bw_method=None, ind=None, **kwargs):
2030
2083
def _args_adjust (self ):
2031
2084
pass
2032
2085
2033
- def _get_ind (self , y ):
2034
- if self .ind is None :
2035
- sample_range = max (y ) - min (y )
2036
- ind = np .linspace (min (y ) - 0.5 * sample_range ,
2037
- max (y ) + 0.5 * sample_range , 1000 )
2038
- else :
2039
- ind = self .ind
2040
- return ind
2041
-
2042
2086
def _get_plot_function (self ):
2043
- from scipy .stats import gaussian_kde
2044
- from scipy import __version__ as spv
2045
2087
f = MPLPlot ._get_plot_function (self )
2046
- def plotf (ax , y , style = None , column_num = None , ** kwds ):
2047
- y = remove_na (y )
2048
- if LooseVersion (spv ) >= '0.11.0' :
2049
- gkde = gaussian_kde (y , bw_method = self .bw_method )
2050
- else :
2051
- gkde = gaussian_kde (y )
2052
- if self .bw_method is not None :
2053
- msg = ('bw_method was added in Scipy 0.11.0.' +
2054
- ' Scipy version in use is %s.' % spv )
2055
- warnings .warn (msg )
2056
-
2057
- ind = self ._get_ind (y )
2058
- y = gkde .evaluate (ind )
2059
- lines = f (ax , ind , y , style = style , ** kwds )
2060
- return lines
2061
- return plotf
2088
+ return _kdeplot_plotf (f , self .bw_method , self .ind )
2062
2089
2063
2090
def _post_plot_logic (self ):
2064
2091
for ax in self .axes :
@@ -2153,24 +2180,7 @@ def _args_adjust(self):
2153
2180
self .sharey = False
2154
2181
2155
2182
def _get_plot_function (self ):
2156
- def plotf (ax , y , column_num = None , ** kwds ):
2157
- if y .ndim == 2 :
2158
- y = [remove_na (v ) for v in y ]
2159
- # Boxplot fails with empty arrays, so need to add a NaN
2160
- # if any cols are empty
2161
- # GH 8181
2162
- y = [v if v .size > 0 else np .array ([np .nan ]) for v in y ]
2163
- else :
2164
- y = remove_na (y )
2165
- bp = ax .boxplot (y , ** kwds )
2166
-
2167
- if self .return_type == 'dict' :
2168
- return bp , bp
2169
- elif self .return_type == 'both' :
2170
- return self .BP (ax = ax , lines = bp ), bp
2171
- else :
2172
- return ax , bp
2173
- return plotf
2183
+ return _boxplot_plotf (self .return_type )
2174
2184
2175
2185
def _validate_color_args (self ):
2176
2186
if 'color' in self .kwds :
0 commit comments