Skip to content

Commit ee4a25d

Browse files
author
Chang She
committed
ENH: plot one column vs another #857 #1527
1 parent 5dfb2ee commit ee4a25d

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

pandas/tests/test_graphics.py

+29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import numpy as np
1212

13+
from numpy.testing import assert_array_equal
1314
from numpy.testing.decorators import slow
1415
import pandas.tools.plotting as plotting
1516

@@ -163,6 +164,34 @@ def test_plot(self):
163164
index=MultiIndex.from_tuples(tuples))
164165
_check_plot_works(df.plot, use_index=True)
165166

167+
@slow
168+
def test_plot_xy(self):
169+
df = tm.makeTimeDataFrame()
170+
self._check_data(df.plot(x=0, y=1),
171+
df.set_index('A').sort_index()['B'].plot())
172+
173+
self._check_data(df.plot(x=0), df.set_index('A').sort_index().plot())
174+
175+
self._check_data(df.plot(y=0), df.B.plot())
176+
177+
self._check_data(df.plot(x='A', y='B'),
178+
df.set_index('A').sort_index().B.plot())
179+
180+
self._check_data(df.plot(x='A'), df.set_index('A').sort_index().plot())
181+
182+
self._check_data(df.plot(y='B'), df.B.plot())
183+
184+
def _check_data(self, xp, rs):
185+
xp_lines = xp.get_lines()
186+
rs_lines = rs.get_lines()
187+
188+
def check_line(xpl, rsl):
189+
xpdata = xpl.get_xydata()
190+
rsdata = rsl.get_xydata()
191+
assert_array_equal(xpdata, rsdata)
192+
193+
[check_line(xpl, rsl) for xpl, rsl in zip(xp_lines, rs_lines)]
194+
166195
@slow
167196
def test_subplots(self):
168197
df = DataFrame(np.random.rand(10, 3),

pandas/tools/plotting.py

+23-6
Original file line numberDiff line numberDiff line change
@@ -1038,17 +1038,21 @@ class HistPlot(MPLPlot):
10381038
pass
10391039

10401040

1041-
def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
1042-
use_index=True, figsize=None, grid=False, legend=True, rot=None,
1043-
ax=None, style=None, title=None, xlim=None, ylim=None, logy=False,
1044-
xticks=None, yticks=None, kind='line', sort_columns=False,
1045-
fontsize=None, secondary_y=False, **kwds):
1041+
def plot_frame(frame=None, x=None, y=None, subplots=False, sharex=True,
1042+
sharey=False, use_index=True, figsize=None, grid=False,
1043+
legend=True, rot=None, ax=None, style=None, title=None, xlim=None,
1044+
ylim=None, logy=False, xticks=None, yticks=None, kind='line',
1045+
sort_columns=False, fontsize=None, secondary_y=False, **kwds):
1046+
10461047
"""
10471048
Make line or bar plot of DataFrame's series with the index on the x-axis
10481049
using matplotlib / pylab.
10491050
10501051
Parameters
10511052
----------
1053+
x : int or str, default None
1054+
y : int or str, default None
1055+
Allows plotting of one column versus another
10521056
subplots : boolean, default False
10531057
Make separate subplots for each time series
10541058
sharex : boolean, default True
@@ -1104,6 +1108,20 @@ def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
11041108
else:
11051109
raise ValueError('Invalid chart type given %s' % kind)
11061110

1111+
if isinstance(x, int):
1112+
x = frame.columns[x]
1113+
if x is not None:
1114+
frame = frame.set_index(x).sort_index()
1115+
1116+
if isinstance(y, int):
1117+
y = frame.columns[y]
1118+
if y is not None:
1119+
return plot_series(frame[y], label=y, kind=kind, use_index=True,
1120+
rot=rot, xticks=xticks, yticks=yticks,
1121+
xlim=xlim, ylim=ylim, ax=ax, style=style,
1122+
grid=grid, logy=logy, secondary_y=secondary_y,
1123+
**kwds)
1124+
11071125
plot_obj = klass(frame, kind=kind, subplots=subplots, rot=rot,
11081126
legend=legend, ax=ax, style=style, fontsize=fontsize,
11091127
use_index=use_index, sharex=sharex, sharey=sharey,
@@ -1118,7 +1136,6 @@ def plot_frame(frame=None, subplots=False, sharex=True, sharey=False,
11181136
else:
11191137
return plot_obj.axes[0]
11201138

1121-
11221139
def plot_series(series, label=None, kind='line', use_index=True, rot=None,
11231140
xticks=None, yticks=None, xlim=None, ylim=None,
11241141
ax=None, style=None, grid=None, logy=False, secondary_y=False,

0 commit comments

Comments
 (0)