Skip to content

Commit 2e4a0fb

Browse files
committed
Relocate plotf
1 parent 5f49373 commit 2e4a0fb

File tree

1 file changed

+62
-51
lines changed

1 file changed

+62
-51
lines changed

pandas/tools/plotting.py

+62-51
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,64 @@ def plotf(ax, x, y, style=None, **kwds):
770770

771771
return plotf
772772

773+
774+
def _lineplot_plotf(f, stacked, subplots):
775+
def plotf(ax, x, y, style=None, column_num=None, **kwds):
776+
# column_num is used to get the target column from protf in line and area plots
777+
if not hasattr(ax, '_pos_prior') or column_num == 0:
778+
LinePlot._initialize_prior(ax, len(y))
779+
y_values = LinePlot._get_stacked_values(ax, y, kwds['label'], stacked)
780+
lines = f(ax, x, y_values, style=style, **kwds)
781+
LinePlot._update_prior(ax, y, stacked, subplots)
782+
return lines
783+
784+
return plotf
785+
786+
787+
def _areaplot_plotf(f, stacked, subplots):
788+
import matplotlib.pyplot as plt
789+
def plotf(ax, x, y, style=None, column_num=None, **kwds):
790+
if not hasattr(ax, '_pos_prior') or column_num == 0:
791+
LinePlot._initialize_prior(ax, len(y))
792+
y_values = LinePlot._get_stacked_values(ax, y, kwds['label'], stacked)
793+
lines = f(ax, x, y_values, style=style, **kwds)
794+
795+
# get data from the line to get coordinates for fill_between
796+
xdata, y_values = lines[0].get_data(orig=False)
797+
798+
if (y >= 0).all():
799+
start = ax._pos_prior
800+
elif (y <= 0).all():
801+
start = ax._neg_prior
802+
else:
803+
start = np.zeros(len(y))
804+
805+
if not 'color' in kwds:
806+
kwds['color'] = lines[0].get_color()
807+
808+
plt.Axes.fill_between(ax, xdata, start, y_values, **kwds)
809+
LinePlot._update_prior(ax, y, stacked, subplots)
810+
return lines
811+
812+
return plotf
813+
814+
815+
def _histplot_plotf(bins, bottom, stacked, subplots):
816+
import matplotlib.pyplot as plt
817+
def plotf(ax, y, style=None, column_num=None, **kwds):
818+
if not hasattr(ax, '_pos_prior') or column_num == 0:
819+
LinePlot._initialize_prior(ax, len(bins) - 1)
820+
y = y[~com.isnull(y)]
821+
new_bottom = ax._pos_prior + bottom
822+
# ignore style
823+
n, new_bins, patches = plt.Axes.hist(ax, y, bins=bins,
824+
bottom=new_bottom, **kwds)
825+
LinePlot._update_prior(ax, n, stacked, subplots)
826+
return patches
827+
828+
return plotf
829+
830+
773831
class MPLPlot(object):
774832
"""
775833
Base class for assembling a pandas plot using matplotlib
@@ -1640,17 +1698,8 @@ def _get_stacked_values(cls, ax, y, label, stacked):
16401698

16411699
def _get_plot_function(self):
16421700
f = MPLPlot._get_plot_function(self)
1643-
stacked = self.stacked
1644-
subplots = self.subplots
1645-
def plotf(ax, x, y, style=None, column_num=None, **kwds):
1646-
# column_num is used to get the target column from protf in line and area plots
1647-
if not hasattr(ax, '_pos_prior') or column_num == 0:
1648-
LinePlot._initialize_prior(ax, len(y))
1649-
y_values = LinePlot._get_stacked_values(ax, y, kwds['label'], stacked)
1650-
lines = f(ax, x, y_values, style=style, **kwds)
1651-
LinePlot._update_prior(ax, y, stacked, subplots)
1652-
return lines
1653-
return plotf
1701+
1702+
return _lineplot_plotf(f, self.stacked, self.subplots)
16541703

16551704
def _get_ts_plot_function(self):
16561705
from pandas.tseries.plotting import tsplot
@@ -1739,35 +1788,12 @@ def __init__(self, data, **kwargs):
17391788
self.kwds.setdefault('alpha', 0.5)
17401789

17411790
def _get_plot_function(self):
1742-
import matplotlib.pyplot as plt
17431791
if self.logy or self.loglog:
17441792
raise ValueError("Log-y scales are not supported in area plot")
17451793
else:
17461794
f = MPLPlot._get_plot_function(self)
1747-
stacked = self.stacked
1748-
subplots = self.subplots
1749-
def plotf(ax, x, y, style=None, column_num=None, **kwds):
1750-
if not hasattr(ax, '_pos_prior') or column_num == 0:
1751-
LinePlot._initialize_prior(ax, len(y))
1752-
y_values = LinePlot._get_stacked_values(ax, y, kwds['label'], stacked)
1753-
lines = f(ax, x, y_values, style=style, **kwds)
1754-
1755-
# get data from the line to get coordinates for fill_between
1756-
xdata, y_values = lines[0].get_data(orig=False)
1757-
1758-
if (y >= 0).all():
1759-
start = ax._pos_prior
1760-
elif (y <= 0).all():
1761-
start = ax._neg_prior
1762-
else:
1763-
start = np.zeros(len(y))
1764-
1765-
if not 'color' in kwds:
1766-
kwds['color'] = lines[0].get_color()
17671795

1768-
plt.Axes.fill_between(ax, xdata, start, y_values, **kwds)
1769-
LinePlot._update_prior(ax, y, stacked, subplots)
1770-
return lines
1796+
return _areaplot_plotf(f, self.stacked, self.subplots)
17711797

17721798
return plotf
17731799

@@ -1957,22 +1983,7 @@ def _args_adjust(self):
19571983
self.bottom = np.array(self.bottom)
19581984

19591985
def _get_plot_function(self):
1960-
import matplotlib.pyplot as plt
1961-
bins = self.bins
1962-
bottom = self.bottom
1963-
stacked = self.stacked
1964-
subplots = self.subplots
1965-
def plotf(ax, y, style=None, column_num=None, **kwds):
1966-
if not hasattr(ax, '_pos_prior') or column_num == 0:
1967-
LinePlot._initialize_prior(ax, len(self.bins) - 1)
1968-
y = y[~com.isnull(y)]
1969-
new_bottom = ax._pos_prior + bottom
1970-
# ignore style
1971-
n, new_bins, patches = plt.Axes.hist(ax, y, bins=bins,
1972-
bottom=new_bottom, **kwds)
1973-
LinePlot._update_prior(ax, n, stacked, subplots)
1974-
return patches
1975-
return plotf
1986+
return _histplot_plotf(self.bins, self.bottom, self.stacked, self.subplots)
19761987

19771988
def _make_plot(self):
19781989
plotf = self._get_plot_function()

0 commit comments

Comments
 (0)