@@ -363,34 +363,43 @@ def generate(self):
363
363
def _args_adjust (self ):
364
364
pass
365
365
366
+ def _maybe_right_yaxis (self , ax ):
367
+ ypos = ax .get_yaxis ().get_ticks_position ().strip ().lower ()
368
+
369
+ if self .secondary_y and ypos != 'right' :
370
+ orig_ax = ax
371
+ ax = ax .twinx ()
372
+ if len (orig_ax .get_lines ()) == 0 : # no data on left y
373
+ orig_ax .get_yaxis ().set_visible (False )
374
+ else :
375
+ ax .get_yaxis ().set_visible (True )
376
+
377
+ return ax
378
+
366
379
def _setup_subplots (self ):
367
380
if self .subplots :
368
381
nrows , ncols = self ._get_layout ()
369
382
if self .ax is None :
370
383
fig , axes = _subplots (nrows = nrows , ncols = ncols ,
371
384
sharex = self .sharex , sharey = self .sharey ,
372
385
figsize = self .figsize ,
373
- secondary_y = self .secondary_y )
386
+ secondary_y = self .secondary_y ,
387
+ data = self .data )
374
388
else :
375
389
fig , axes = _subplots (nrows = nrows , ncols = ncols ,
376
390
sharex = self .sharex , sharey = self .sharey ,
377
391
figsize = self .figsize , ax = self .ax ,
378
- secondary_y = self .secondary_y )
392
+ secondary_y = self .secondary_y ,
393
+ data = self .data )
379
394
else :
380
395
if self .ax is None :
381
396
fig = self .plt .figure (figsize = self .figsize )
382
397
ax = fig .add_subplot (111 )
383
- ypos = ax .get_yaxis ().get_ticks_position ().strip ().lower ()
384
- if self .secondary_y and ypos != 'right' :
385
- ax = ax .twinx ()
398
+ ax = self ._maybe_right_yaxis (ax )
386
399
self .ax = ax
387
400
else :
388
- ax = self .ax
389
401
fig = self .ax .get_figure ()
390
- ypos = ax .get_yaxis ().get_ticks_position ().strip ().lower ()
391
- if self .secondary_y and ypos != 'right' :
392
- ax = ax .twinx ()
393
- self .ax = ax
402
+ self .ax = self ._maybe_right_yaxis (self .ax )
394
403
395
404
axes = [self .ax ]
396
405
@@ -640,21 +649,15 @@ def _make_ts_plot(self, data, **kwargs):
640
649
plotf = self ._get_plot_function ()
641
650
642
651
if isinstance (data , Series ):
643
- if self .subplots : # shouldn't even allow users to specify
644
- ax = self .axes [0 ]
645
- else :
646
- ax = self .ax
652
+ ax , _ = self ._get_ax_and_style (0 ) #self.axes[0]
647
653
648
654
label = com ._stringify (self .label )
649
655
tsplot (data , plotf , ax = ax , label = label , style = self .style ,
650
656
** kwargs )
651
657
ax .grid (self .grid )
652
658
else :
653
659
for i , col in enumerate (data .columns ):
654
- if self .subplots :
655
- ax = self .axes [i ]
656
- else :
657
- ax = self .ax
660
+ ax , _ = self ._get_ax_and_style (i )
658
661
label = com ._stringify (col )
659
662
tsplot (data [col ], plotf , ax = ax , label = label , ** kwargs )
660
663
ax .grid (self .grid )
@@ -722,7 +725,7 @@ def _make_plot(self):
722
725
rects = []
723
726
labels = []
724
727
725
- ax = self .axes [0 ]
728
+ ax , _ = self . _get_ax_and_style ( 0 ) # self.axes[0]
726
729
727
730
bar_f = self .bar_f
728
731
@@ -737,7 +740,7 @@ def _make_plot(self):
737
740
kwds ['color' ] = colors [i % len (colors )]
738
741
739
742
if self .subplots :
740
- ax = self .axes [i ]
743
+ ax , _ = self . _get_ax_and_style ( i ) # self.axes[i]
741
744
rect = bar_f (ax , self .ax_pos , y , 0.5 , start = pos_prior ,
742
745
linewidth = 1 , ** kwds )
743
746
ax .set_title (label )
@@ -846,6 +849,9 @@ def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
846
849
ylim : 2-tuple/list
847
850
rot : int, default None
848
851
Rotation for ticks
852
+ secondary_y : boolean or sequence, default False
853
+ Whether to plot on the secondary y-axis
854
+ If dict then can select which columns to plot on secondary y-axis
849
855
kwds : keywords
850
856
Options to pass to matplotlib plotting method
851
857
@@ -868,7 +874,8 @@ def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
868
874
use_index = use_index , sharex = sharex , sharey = sharey ,
869
875
xticks = xticks , yticks = yticks , xlim = xlim , ylim = ylim ,
870
876
title = title , grid = grid , figsize = figsize , logy = logy ,
871
- sort_columns = sort_columns , ** kwds )
877
+ sort_columns = sort_columns , secondary_y = secondary_y ,
878
+ ** kwds )
872
879
plot_obj .generate ()
873
880
plot_obj .draw ()
874
881
if subplots :
@@ -929,6 +936,14 @@ def plot_series(series, label=None, kind='line', use_index=True, rot=None,
929
936
930
937
if ax is None :
931
938
ax = _gca ()
939
+ if ax .get_yaxis ().get_ticks_position ().strip ().lower () == 'right' :
940
+ fig = _gcf ()
941
+ axes = fig .get_axes ()
942
+ for i in range (len (axes ))[::- 1 ]:
943
+ ax = axes [i ]
944
+ ypos = ax .get_yaxis ().get_ticks_position ().strip ().lower ()
945
+ if ypos == 'left' :
946
+ break
932
947
933
948
# is there harm in this?
934
949
if label is None :
@@ -1275,7 +1290,8 @@ def _get_layout(nplots):
1275
1290
# copied from matplotlib/pyplot.py for compatibility with matplotlib < 1.0
1276
1291
1277
1292
def _subplots (nrows = 1 , ncols = 1 , sharex = False , sharey = False , squeeze = True ,
1278
- subplot_kw = None , ax = None , secondary_y = False , ** fig_kw ):
1293
+ subplot_kw = None , ax = None , secondary_y = False , data = None ,
1294
+ ** fig_kw ):
1279
1295
"""Create a figure with a set of subplots already made.
1280
1296
1281
1297
This utility wrapper makes it convenient to create common layouts of
@@ -1317,7 +1333,7 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1317
1333
1318
1334
ax : Matplotlib axis object, default None
1319
1335
1320
- secondary_y : boolean, default False
1336
+ secondary_y : boolean or sequence of ints , default False
1321
1337
If True then y-axis will be on the right
1322
1338
1323
1339
Returns:
@@ -1348,6 +1364,7 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1348
1364
plt.subplots(2, 2, subplot_kw=dict(polar=True))
1349
1365
"""
1350
1366
import matplotlib .pyplot as plt
1367
+ from pandas .core .frame import DataFrame
1351
1368
1352
1369
if subplot_kw is None :
1353
1370
subplot_kw = {}
@@ -1363,10 +1380,18 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1363
1380
nplots = nrows * ncols
1364
1381
axarr = np .empty (nplots , dtype = object )
1365
1382
1383
+ def on_right (i ):
1384
+ if isinstance (secondary_y , bool ):
1385
+ return secondary_y
1386
+ if isinstance (data , DataFrame ):
1387
+ return data .columns [i ] in secondary_y
1388
+
1366
1389
# Create first subplot separately, so we can share it if requested
1367
1390
ax0 = fig .add_subplot (nrows , ncols , 1 , ** subplot_kw )
1368
- if secondary_y :
1391
+ if on_right (0 ):
1392
+ orig_ax = ax0
1369
1393
ax0 = ax0 .twinx ()
1394
+ orig_ax .get_yaxis ().set_visible (False )
1370
1395
1371
1396
if sharex :
1372
1397
subplot_kw ['sharex' ] = ax0
@@ -1378,8 +1403,11 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1378
1403
# convention.
1379
1404
for i in range (1 , nplots ):
1380
1405
ax = fig .add_subplot (nrows , ncols , i + 1 , ** subplot_kw )
1381
- if secondary_y :
1406
+ if on_right (i ):
1407
+ print 'on right ' , data .columns [i ]
1408
+ orig_ax = ax
1382
1409
ax = ax .twinx ()
1410
+ orig_ax .get_yaxis ().set_visible (False )
1383
1411
axarr [i ] = ax
1384
1412
1385
1413
if nplots > 1 :
0 commit comments