Skip to content

Commit 2ad68ef

Browse files
Chang Shewesm
Chang She
authored andcommitted
ENH: explicit secondary_y keyword in plot_series and plot_frame. Call twinx depending on the yaxis ticks position
1 parent c45c4de commit 2ad68ef

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

pandas/tools/plotting.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class MPLPlot(object):
276276

277277
def __init__(self, data, kind=None, by=None, subplots=False, sharex=True,
278278
sharey=False, use_index=True,
279-
figsize=None, grid=True, legend=True, rot=None,
279+
figsize=None, grid=None, legend=True, rot=None,
280280
ax=None, fig=None, title=None, xlim=None, ylim=None,
281281
xticks=None, yticks=None,
282282
sort_columns=False, fontsize=None,
@@ -304,6 +304,9 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=True,
304304
self.fontsize = fontsize
305305
self.rot = rot
306306

307+
if grid is None:
308+
grid = False if secondary_y else True
309+
307310
self.grid = grid
308311
self.legend = legend
309312

@@ -377,13 +380,15 @@ def _setup_subplots(self):
377380
if self.ax is None:
378381
fig = self.plt.figure(figsize=self.figsize)
379382
ax = fig.add_subplot(111)
380-
if self.secondary_y:
383+
ypos = ax.get_yaxis().get_ticks_position().strip().lower()
384+
if self.secondary_y and ypos != 'right':
381385
ax = ax.twinx()
382386
self.ax = ax
383387
else:
384388
ax = self.ax
385389
fig = self.ax.get_figure()
386-
if self.secondary_y:
390+
ypos = ax.get_yaxis().get_ticks_position().strip().lower()
391+
if self.secondary_y and ypos != 'right':
387392
ax = ax.twinx()
388393
self.ax = ax
389394

@@ -796,12 +801,12 @@ class HistPlot(MPLPlot):
796801

797802
def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
798803
use_index=True,
799-
figsize=None, grid=True, legend=True, rot=None,
804+
figsize=None, grid=False, legend=True, rot=None,
800805
ax=None, title=None,
801806
xlim=None, ylim=None, logy=False,
802807
xticks=None, yticks=None,
803808
kind='line',
804-
sort_columns=False, fontsize=None, **kwds):
809+
sort_columns=False, fontsize=None, secondary_y=False, **kwds):
805810
"""
806811
Make line or bar plot of DataFrame's series with the index on the x-axis
807812
using matplotlib / pylab.
@@ -874,7 +879,8 @@ def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
874879

875880
def plot_series(series, label=None, kind='line', use_index=True, rot=None,
876881
xticks=None, yticks=None, xlim=None, ylim=None,
877-
ax=None, style=None, grid=True, logy=False, **kwds):
882+
ax=None, style=None, grid=None, logy=False, secondary_y=False,
883+
**kwds):
878884
"""
879885
Plot the input series with the index on the x-axis using matplotlib
880886
@@ -931,7 +937,8 @@ def plot_series(series, label=None, kind='line', use_index=True, rot=None,
931937
plot_obj = klass(series, kind=kind, rot=rot, logy=logy,
932938
ax=ax, use_index=use_index, style=style,
933939
xticks=xticks, yticks=yticks, xlim=xlim, ylim=ylim,
934-
legend=False, grid=grid, label=label, **kwds)
940+
legend=False, grid=grid, label=label,
941+
secondary_y=secondary_y, **kwds)
935942

936943
plot_obj.generate()
937944
plot_obj.draw()

pandas/tseries/tests/test_plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def test_finder_monthly(self):
192192
rs = xaxis.get_majorticklocs()[0]
193193
self.assert_(rs == xp)
194194

195+
@slow
195196
def test_secondary_y(self):
196197
import matplotlib.pyplot as plt
197198
plt.close('all')
@@ -204,7 +205,6 @@ def test_secondary_y(self):
204205
assert_series_equal(ser, xp)
205206
self.assert_(axes[1].get_yaxis().get_ticks_position() == 'right')
206207

207-
208208
PNG_PATH = 'tmp.png'
209209
def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
210210
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)