@@ -909,13 +909,9 @@ def _args_adjust(self):
909909 pass
910910
911911 def _maybe_right_yaxis (self , ax ):
912- _types = (list , tuple , np .ndarray )
913- sec_true = isinstance (self .secondary_y , bool ) and self .secondary_y
914- list_sec = isinstance (self .secondary_y , _types )
915- has_sec = list_sec and len (self .secondary_y ) > 0
916- all_sec = list_sec and len (self .secondary_y ) == self .nseries
917-
918- if (sec_true or has_sec ) and not hasattr (ax , 'right_ax' ):
912+ if hasattr (ax , 'right_ax' ):
913+ return ax .right_ax
914+ else :
919915 orig_ax , new_ax = ax , ax .twinx ()
920916 new_ax ._get_lines .color_cycle = orig_ax ._get_lines .color_cycle
921917
@@ -924,38 +920,25 @@ def _maybe_right_yaxis(self, ax):
924920
925921 if len (orig_ax .get_lines ()) == 0 : # no data on left y
926922 orig_ax .get_yaxis ().set_visible (False )
927-
928- if len (new_ax .get_lines ()) == 0 :
929- new_ax .get_yaxis ().set_visible (False )
930-
931- if sec_true or all_sec :
932- ax = new_ax
933- else :
934- ax .get_yaxis ().set_visible (True )
935-
936- return ax
923+ return new_ax
937924
938925 def _setup_subplots (self ):
939926 if self .subplots :
940927 nrows , ncols = self ._get_layout ()
941928 fig , axes = _subplots (nrows = nrows , ncols = ncols ,
942929 sharex = self .sharex , sharey = self .sharey ,
943- figsize = self .figsize , ax = self .ax ,
944- secondary_y = self .secondary_y ,
945- data = self .data )
930+ figsize = self .figsize , ax = self .ax )
946931 if not com .is_list_like (axes ):
947932 axes = np .array ([axes ])
948933 else :
949934 if self .ax is None :
950935 fig = self .plt .figure (figsize = self .figsize )
951936 ax = fig .add_subplot (111 )
952- ax = self ._maybe_right_yaxis (ax )
953937 else :
954938 fig = self .ax .get_figure ()
955939 if self .figsize is not None :
956940 fig .set_size_inches (self .figsize )
957- ax = self ._maybe_right_yaxis (self .ax )
958-
941+ ax = self .ax
959942 axes = [ax ]
960943
961944 if self .logx or self .loglog :
@@ -1182,14 +1165,21 @@ def _get_ax(self, i):
11821165 # get the twinx ax if appropriate
11831166 if self .subplots :
11841167 ax = self .axes [i ]
1168+
1169+ if self .on_right (i ):
1170+ ax = self ._maybe_right_yaxis (ax )
1171+ self .axes [i ] = ax
11851172 else :
11861173 ax = self .axes [0 ]
11871174
1188- if self .on_right (i ):
1189- if hasattr (ax , 'right_ax' ):
1190- ax = ax .right_ax
1191- elif hasattr (ax , 'left_ax' ):
1192- ax = ax .left_ax
1175+ if self .on_right (i ):
1176+ ax = self ._maybe_right_yaxis (ax )
1177+
1178+ sec_true = isinstance (self .secondary_y , bool ) and self .secondary_y
1179+ all_sec = (com .is_list_like (self .secondary_y ) and
1180+ len (self .secondary_y ) == self .nseries )
1181+ if sec_true or all_sec :
1182+ self .axes [0 ] = ax
11931183
11941184 ax .get_yaxis ().set_visible (True )
11951185 return ax
@@ -1550,8 +1540,6 @@ def _make_plot(self):
15501540 data = self ._maybe_convert_index (self .data )
15511541 self ._make_ts_plot (data )
15521542 else :
1553- from pandas .core .frame import DataFrame
1554- lines = []
15551543 x = self ._get_xticks (convert_period = True )
15561544
15571545 plotf = self ._get_plot_function ()
@@ -1563,8 +1551,6 @@ def _make_plot(self):
15631551 kwds = self .kwds .copy ()
15641552 self ._maybe_add_color (colors , kwds , style , i )
15651553
1566- lines += _get_all_lines (ax )
1567-
15681554 errors = self ._get_errorbars (label = label , index = i )
15691555 kwds = dict (kwds , ** errors )
15701556
@@ -1588,15 +1574,13 @@ def _make_plot(self):
15881574 newlines = plotf (* args , ** kwds )
15891575 self ._add_legend_handle (newlines [0 ], label , index = i )
15901576
1591- lines .append (newlines [0 ])
1592-
15931577 if self .stacked and not self .subplots :
15941578 if (y >= 0 ).all ():
15951579 self ._pos_prior += y
15961580 elif (y <= 0 ).all ():
15971581 self ._neg_prior += y
15981582
1599- if self . _is_datetype ():
1583+ lines = _get_all_lines ( ax )
16001584 left , right = _get_xlim (lines )
16011585 ax .set_xlim (left , right )
16021586
@@ -2253,14 +2237,7 @@ def plot_series(series, label=None, kind='line', use_index=True, rot=None,
22532237 import matplotlib .pyplot as plt
22542238 if ax is None and len (plt .get_fignums ()) > 0 :
22552239 ax = _gca ()
2256- if ax .get_yaxis ().get_ticks_position ().strip ().lower () == 'right' :
2257- fig = _gcf ()
2258- axes = fig .get_axes ()
2259- for i in reversed (range (len (axes ))):
2260- ax = axes [i ]
2261- ypos = ax .get_yaxis ().get_ticks_position ().strip ().lower ()
2262- if ypos == 'left' :
2263- break
2240+ ax = getattr (ax , 'left_ax' , ax )
22642241
22652242 # is there harm in this?
22662243 if label is None :
@@ -2890,8 +2867,7 @@ def _get_layout(nplots, layout=None):
28902867
28912868
28922869def _subplots (nrows = 1 , ncols = 1 , naxes = None , sharex = False , sharey = False , squeeze = True ,
2893- subplot_kw = None , ax = None , secondary_y = False , data = None ,
2894- ** fig_kw ):
2870+ subplot_kw = None , ax = None , ** fig_kw ):
28952871 """Create a figure with a set of subplots already made.
28962872
28972873 This utility wrapper makes it convenient to create common layouts of
@@ -2932,12 +2908,6 @@ def _subplots(nrows=1, ncols=1, naxes=None, sharex=False, sharey=False, squeeze=
29322908
29332909 ax : Matplotlib axis object, optional
29342910
2935- secondary_y : boolean or sequence of ints, default False
2936- If True then y-axis will be on the right
2937-
2938- data : DataFrame, optional
2939- If secondary_y is a sequence, data is used to select columns.
2940-
29412911 fig_kw : Other keyword arguments to be passed to the figure() call.
29422912 Note that all keywords not recognized above will be
29432913 automatically included here.
@@ -2993,22 +2963,8 @@ def _subplots(nrows=1, ncols=1, naxes=None, sharex=False, sharey=False, squeeze=
29932963
29942964 axarr = np .empty (nplots , dtype = object )
29952965
2996- def on_right (i ):
2997- if isinstance (secondary_y , bool ):
2998- return secondary_y
2999- if isinstance (data , DataFrame ):
3000- return data .columns [i ] in secondary_y
3001-
30022966 # Create first subplot separately, so we can share it if requested
30032967 ax0 = fig .add_subplot (nrows , ncols , 1 , ** subplot_kw )
3004- if on_right (0 ):
3005- orig_ax = ax0
3006- ax0 = ax0 .twinx ()
3007- ax0 ._get_lines .color_cycle = orig_ax ._get_lines .color_cycle
3008-
3009- orig_ax .get_yaxis ().set_visible (False )
3010- orig_ax .right_ax = ax0
3011- ax0 .left_ax = orig_ax
30122968
30132969 if sharex :
30142970 subplot_kw ['sharex' ] = ax0
@@ -3020,12 +2976,6 @@ def on_right(i):
30202976 # convention.
30212977 for i in range (1 , nplots ):
30222978 ax = fig .add_subplot (nrows , ncols , i + 1 , ** subplot_kw )
3023- if on_right (i ):
3024- orig_ax = ax
3025- ax = ax .twinx ()
3026- ax ._get_lines .color_cycle = orig_ax ._get_lines .color_cycle
3027-
3028- orig_ax .get_yaxis ().set_visible (False )
30292979 axarr [i ] = ax
30302980
30312981 if nplots > 1 :
0 commit comments