@@ -1594,7 +1594,6 @@ def _is_ts_plot(self):
1594
1594
return not self .x_compat and self .use_index and self ._use_dynamic_x ()
1595
1595
1596
1596
def _make_plot (self ):
1597
- self ._initialize_prior (len (self .data ))
1598
1597
1599
1598
if self ._is_ts_plot ():
1600
1599
data = self ._maybe_convert_index (self .data )
@@ -1626,12 +1625,13 @@ def _make_plot(self):
1626
1625
left , right = _get_xlim (lines )
1627
1626
ax .set_xlim (left , right )
1628
1627
1629
- def _get_stacked_values (self , y , label ):
1630
- if self .stacked :
1628
+ @classmethod
1629
+ def _get_stacked_values (cls , ax , y , label , stacked ):
1630
+ if stacked :
1631
1631
if (y >= 0 ).all ():
1632
- return self ._pos_prior + y
1632
+ return ax ._pos_prior + y
1633
1633
elif (y <= 0 ).all ():
1634
- return self ._neg_prior + y
1634
+ return ax ._neg_prior + y
1635
1635
else :
1636
1636
raise ValueError ('When stacked is True, each column must be either all positive or negative.'
1637
1637
'{0} contains both positive and negative values' .format (label ))
@@ -1640,13 +1640,15 @@ def _get_stacked_values(self, y, label):
1640
1640
1641
1641
def _get_plot_function (self ):
1642
1642
f = MPLPlot ._get_plot_function (self )
1643
+ stacked = self .stacked
1644
+ subplots = self .subplots
1643
1645
def plotf (ax , x , y , style = None , column_num = None , ** kwds ):
1644
1646
# column_num is used to get the target column from protf in line and area plots
1645
- if column_num == 0 :
1646
- self ._initialize_prior (len (self . data ))
1647
- y_values = self ._get_stacked_values (y , kwds ['label' ])
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 )
1648
1650
lines = f (ax , x , y_values , style = style , ** kwds )
1649
- self ._update_prior (y )
1651
+ LinePlot ._update_prior (ax , y , stacked , subplots )
1650
1652
return lines
1651
1653
return plotf
1652
1654
@@ -1660,19 +1662,21 @@ def _plot(ax, x, data, style=None, **kwds):
1660
1662
return lines
1661
1663
return _plot
1662
1664
1663
- def _initialize_prior (self , n ):
1664
- self ._pos_prior = np .zeros (n )
1665
- self ._neg_prior = np .zeros (n )
1665
+ @classmethod
1666
+ def _initialize_prior (cls , ax , n ):
1667
+ ax ._pos_prior = np .zeros (n )
1668
+ ax ._neg_prior = np .zeros (n )
1666
1669
1667
- def _update_prior (self , y ):
1668
- if self .stacked and not self .subplots :
1670
+ @classmethod
1671
+ def _update_prior (cls , ax , y , stacked , subplots ):
1672
+ if stacked and not subplots :
1669
1673
# tsplot resample may changedata length
1670
- if len (self ._pos_prior ) != len (y ):
1671
- self ._initialize_prior (len (y ))
1674
+ if len (ax ._pos_prior ) != len (y ):
1675
+ cls ._initialize_prior (ax , len (y ))
1672
1676
if (y >= 0 ).all ():
1673
- self ._pos_prior += y
1677
+ ax ._pos_prior += y
1674
1678
elif (y <= 0 ).all ():
1675
- self ._neg_prior += y
1679
+ ax ._neg_prior += y
1676
1680
1677
1681
def _maybe_convert_index (self , data ):
1678
1682
# tsplot converts automatically, but don't want to convert index
@@ -1735,31 +1739,34 @@ def __init__(self, data, **kwargs):
1735
1739
self .kwds .setdefault ('alpha' , 0.5 )
1736
1740
1737
1741
def _get_plot_function (self ):
1742
+ import matplotlib .pyplot as plt
1738
1743
if self .logy or self .loglog :
1739
1744
raise ValueError ("Log-y scales are not supported in area plot" )
1740
1745
else :
1741
1746
f = MPLPlot ._get_plot_function (self )
1747
+ stacked = self .stacked
1748
+ subplots = self .subplots
1742
1749
def plotf (ax , x , y , style = None , column_num = None , ** kwds ):
1743
- if column_num == 0 :
1744
- self ._initialize_prior (len (self . data ))
1745
- y_values = self ._get_stacked_values (y , kwds ['label' ])
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 )
1746
1753
lines = f (ax , x , y_values , style = style , ** kwds )
1747
1754
1748
1755
# get data from the line to get coordinates for fill_between
1749
1756
xdata , y_values = lines [0 ].get_data (orig = False )
1750
1757
1751
1758
if (y >= 0 ).all ():
1752
- start = self ._pos_prior
1759
+ start = ax ._pos_prior
1753
1760
elif (y <= 0 ).all ():
1754
- start = self ._neg_prior
1761
+ start = ax ._neg_prior
1755
1762
else :
1756
1763
start = np .zeros (len (y ))
1757
1764
1758
1765
if not 'color' in kwds :
1759
1766
kwds ['color' ] = lines [0 ].get_color ()
1760
1767
1761
- self . plt .Axes .fill_between (ax , xdata , start , y_values , ** kwds )
1762
- self ._update_prior (y )
1768
+ plt .Axes .fill_between (ax , xdata , start , y_values , ** kwds )
1769
+ LinePlot ._update_prior (ax , y , stacked , subplots )
1763
1770
return lines
1764
1771
1765
1772
return plotf
@@ -1950,15 +1957,20 @@ def _args_adjust(self):
1950
1957
self .bottom = np .array (self .bottom )
1951
1958
1952
1959
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
1953
1965
def plotf (ax , y , style = None , column_num = None , ** kwds ):
1954
- if column_num == 0 :
1955
- self ._initialize_prior (len (self .bins ) - 1 )
1966
+ if not hasattr ( ax , '_pos_prior' ) or column_num == 0 :
1967
+ LinePlot ._initialize_prior (ax , len (self .bins ) - 1 )
1956
1968
y = y [~ com .isnull (y )]
1957
- bottom = self ._pos_prior + self . bottom
1969
+ new_bottom = ax ._pos_prior + bottom
1958
1970
# ignore style
1959
- n , bins , patches = self . plt .Axes .hist (ax , y , bins = self . bins ,
1960
- bottom = bottom , ** kwds )
1961
- self ._update_prior (n )
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 )
1962
1974
return patches
1963
1975
return plotf
1964
1976
0 commit comments