@@ -314,6 +314,8 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=True,
314
314
self .fig = fig
315
315
self .axes = None
316
316
317
+ self .secondary_y = kwds .pop ('secondary_y' , False )
318
+
317
319
self .kwds = kwds
318
320
319
321
def _iter_data (self ):
@@ -363,18 +365,26 @@ def _setup_subplots(self):
363
365
if self .ax is None :
364
366
fig , axes = _subplots (nrows = nrows , ncols = ncols ,
365
367
sharex = self .sharex , sharey = self .sharey ,
366
- figsize = self .figsize )
368
+ figsize = self .figsize ,
369
+ secondary_y = self .secondary_y )
367
370
else :
368
371
fig , axes = _subplots (nrows = nrows , ncols = ncols ,
369
372
sharex = self .sharex , sharey = self .sharey ,
370
- figsize = self .figsize , ax = self .ax )
371
-
373
+ figsize = self .figsize , ax = self .ax ,
374
+ secondary_y = self . secondary_y )
372
375
else :
373
376
if self .ax is None :
374
377
fig = self .plt .figure (figsize = self .figsize )
375
- self .ax = fig .add_subplot (111 )
378
+ ax = fig .add_subplot (111 )
379
+ if self .secondary_y :
380
+ ax = ax .twinx ()
381
+ self .ax = ax
376
382
else :
383
+ ax = self .ax
377
384
fig = self .ax .get_figure ()
385
+ if self .secondary_y :
386
+ ax = ax .twinx ()
387
+ self .ax = ax
378
388
379
389
axes = [self .ax ]
380
390
@@ -500,6 +510,16 @@ def _get_index_name(self):
500
510
501
511
return name
502
512
513
+ def _get_ax_and_style (self , i ):
514
+ if self .subplots :
515
+ ax = self .axes [i ]
516
+ style = 'k'
517
+ else :
518
+ style = '' # empty string ignored
519
+ ax = self .ax
520
+
521
+ return ax , style
522
+
503
523
class KdePlot (MPLPlot ):
504
524
def __init__ (self , data , ** kwargs ):
505
525
MPLPlot .__init__ (self , data , ** kwargs )
@@ -508,12 +528,9 @@ def _make_plot(self):
508
528
from scipy .stats import gaussian_kde
509
529
plotf = self ._get_plot_function ()
510
530
for i , (label , y ) in enumerate (self ._iter_data ()):
511
- if self .subplots :
512
- ax = self .axes [i ]
513
- style = 'k'
514
- else :
515
- style = '' # empty string ignored
516
- ax = self .ax
531
+
532
+ ax , style = self ._get_ax_and_style (i )
533
+
517
534
if self .style :
518
535
style = self .style
519
536
gkde = gaussian_kde (y )
@@ -577,12 +594,9 @@ def _make_plot(self):
577
594
plotf = self ._get_plot_function ()
578
595
579
596
for i , (label , y ) in enumerate (self ._iter_data ()):
580
- if self .subplots :
581
- ax = self .axes [i ]
582
- style = 'k'
583
- else :
584
- style = '' # empty string ignored
585
- ax = self .ax
597
+
598
+ ax , style = self ._get_ax_and_style (i )
599
+
586
600
if self .style :
587
601
style = self .style
588
602
@@ -1253,7 +1267,7 @@ def _get_layout(nplots):
1253
1267
# copied from matplotlib/pyplot.py for compatibility with matplotlib < 1.0
1254
1268
1255
1269
def _subplots (nrows = 1 , ncols = 1 , sharex = False , sharey = False , squeeze = True ,
1256
- subplot_kw = None , ax = None , ** fig_kw ):
1270
+ subplot_kw = None , ax = None , secondary_y = False , ** fig_kw ):
1257
1271
"""Create a figure with a set of subplots already made.
1258
1272
1259
1273
This utility wrapper makes it convenient to create common layouts of
@@ -1295,6 +1309,9 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1295
1309
1296
1310
ax : Matplotlib axis object, default None
1297
1311
1312
+ secondary_y : boolean, default False
1313
+ If True then y-axis will be on the right
1314
+
1298
1315
Returns:
1299
1316
1300
1317
fig, ax : tuple
@@ -1340,6 +1357,9 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1340
1357
1341
1358
# Create first subplot separately, so we can share it if requested
1342
1359
ax0 = fig .add_subplot (nrows , ncols , 1 , ** subplot_kw )
1360
+ if secondary_y :
1361
+ ax0 = ax0 .twinx ()
1362
+
1343
1363
if sharex :
1344
1364
subplot_kw ['sharex' ] = ax0
1345
1365
if sharey :
@@ -1349,7 +1369,10 @@ def _subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1349
1369
# Note off-by-one counting because add_subplot uses the MATLAB 1-based
1350
1370
# convention.
1351
1371
for i in range (1 , nplots ):
1352
- axarr [i ] = fig .add_subplot (nrows , ncols , i + 1 , ** subplot_kw )
1372
+ ax = fig .add_subplot (nrows , ncols , i + 1 , ** subplot_kw )
1373
+ if secondary_y :
1374
+ ax = ax .twinx ()
1375
+ axarr [i ] = ax
1353
1376
1354
1377
if nplots > 1 :
1355
1378
if sharex and nrows > 1 :
0 commit comments