Skip to content

Commit 9d9743e

Browse files
committed
Make equivalent changes to BoxPlot and KdePlot
1 parent 2e4a0fb commit 9d9743e

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

pandas/tools/plotting.py

+58-48
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,58 @@ def plotf(ax, y, style=None, column_num=None, **kwds):
828828
return plotf
829829

830830

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+
831883
class MPLPlot(object):
832884
"""
833885
Base class for assembling a pandas plot using matplotlib
@@ -1258,14 +1310,15 @@ def _is_datetype(self):
12581310
index.inferred_type in ('datetime', 'date', 'datetime64',
12591311
'time'))
12601312

1313+
def _plot_errors(self):
1314+
return any(e is not None for e in self.errors.values())
1315+
12611316
def _get_plot_function(self):
12621317
'''
12631318
Returns the matplotlib plotting function (plot or errorbar) based on
12641319
the presence of errorbar keywords.
12651320
'''
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())
12691322

12701323
def _get_index_name(self):
12711324
if isinstance(self.data.index, MultiIndex):
@@ -2030,35 +2083,9 @@ def __init__(self, data, bw_method=None, ind=None, **kwargs):
20302083
def _args_adjust(self):
20312084
pass
20322085

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-
20422086
def _get_plot_function(self):
2043-
from scipy.stats import gaussian_kde
2044-
from scipy import __version__ as spv
20452087
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)
20622089

20632090
def _post_plot_logic(self):
20642091
for ax in self.axes:
@@ -2153,24 +2180,7 @@ def _args_adjust(self):
21532180
self.sharey = False
21542181

21552182
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)
21742184

21752185
def _validate_color_args(self):
21762186
if 'color' in self.kwds:

0 commit comments

Comments
 (0)