Skip to content

Commit 97c4a2e

Browse files
sinhrksTomAugspurger
authored and
TomAugspurger
committed
BUG: enabling subplots works unexpectedly
1 parent 4a67608 commit 97c4a2e

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ Bug Fixes
436436
an empty result (:issue:`6952`)
437437
- Bug in sum/mean on 32-bit platforms on overflows (:issue:`6915`)
438438
- Moved ``Panel.shift`` to ``NDFrame.slice_shift`` and fixed to respect multiple dtypes. (:issue:`6959`)
439+
- Bug in enabling ``subplots=True`` in ``DataFrame.plot`` only has single column raises ``TypeError``, and ``Series.plot`` raises ``AttributeError`` (:issue:`6951`)
440+
- Bug in ``DataFrame.plot`` draws unnecessary axes when enabling ``subplots`` and ``kind=scatter`` (:issue:`6951`)
439441

440442
pandas 0.13.1
441443
-------------

pandas/tests/test_graphics.py

+17
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def test_plot(self):
6262
_check_plot_works(self.series[:10].plot, kind='barh')
6363
_check_plot_works(Series(randn(10)).plot, kind='bar', color='black')
6464

65+
# GH 6951
66+
_check_plot_works(self.ts.plot, subplots=True)
67+
6568
@slow
6669
def test_plot_figsize_and_title(self):
6770
# figsize and title
@@ -367,6 +370,11 @@ def test_plot(self):
367370
index=index)
368371
_check_plot_works(df.plot, title=u('\u03A3'))
369372

373+
# GH 6951
374+
# Test with single column
375+
df = DataFrame({'x': np.random.rand(10)})
376+
_check_plot_works(df.plot, kind='bar', subplots=True)
377+
370378
def test_nonnumeric_exclude(self):
371379
import matplotlib.pyplot as plt
372380
df = DataFrame({'A': ["x", "y", "z"], 'B': [1, 2, 3]})
@@ -665,6 +673,10 @@ def test_plot_scatter(self):
665673
with tm.assertRaises(ValueError):
666674
df.plot(y='y', kind='scatter')
667675

676+
# GH 6951
677+
axes = df.plot(x='x', y='y', kind='scatter', subplots=True)
678+
self.assertEqual(len(axes[0].figure.axes), 1)
679+
668680
@slow
669681
def test_plot_bar(self):
670682
from matplotlib.pylab import close
@@ -1271,6 +1283,11 @@ def test_hexbin_basic(self):
12711283
# TODO: need better way to test. This just does existence.
12721284
self.assertEqual(len(ax.collections), 1)
12731285

1286+
# GH 6951
1287+
axes = df.plot(x='A', y='B', kind='hexbin', subplots=True)
1288+
# hexbin should have 2 axes, 1 for plotting and another is colorbar
1289+
self.assertEqual(len(axes[0].figure.axes), 2)
1290+
12741291
@slow
12751292
def test_hexbin_with_c(self):
12761293
df = DataFrame({"A": np.random.uniform(size=20),

pandas/tools/plotting.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -966,18 +966,13 @@ def _maybe_right_yaxis(self, ax):
966966
def _setup_subplots(self):
967967
if self.subplots:
968968
nrows, ncols = self._get_layout()
969-
if self.ax is None:
970-
fig, axes = _subplots(nrows=nrows, ncols=ncols,
971-
sharex=self.sharex, sharey=self.sharey,
972-
figsize=self.figsize,
973-
secondary_y=self.secondary_y,
974-
data=self.data)
975-
else:
976-
fig, axes = _subplots(nrows=nrows, ncols=ncols,
977-
sharex=self.sharex, sharey=self.sharey,
978-
figsize=self.figsize, ax=self.ax,
979-
secondary_y=self.secondary_y,
980-
data=self.data)
969+
fig, axes = _subplots(nrows=nrows, ncols=ncols,
970+
sharex=self.sharex, sharey=self.sharey,
971+
figsize=self.figsize, ax=self.ax,
972+
secondary_y=self.secondary_y,
973+
data=self.data)
974+
if not com.is_list_like(axes):
975+
axes = np.array([axes])
981976
else:
982977
if self.ax is None:
983978
fig = self.plt.figure(figsize=self.figsize)
@@ -1000,7 +995,11 @@ def _setup_subplots(self):
1000995
self.axes = axes
1001996

1002997
def _get_layout(self):
1003-
return (len(self.data.columns), 1)
998+
from pandas.core.frame import DataFrame
999+
if isinstance(self.data, DataFrame):
1000+
return (len(self.data.columns), 1)
1001+
else:
1002+
return (1, 1)
10041003

10051004
def _compute_plot_data(self):
10061005
numeric_data = self.data.convert_objects()._get_numeric_data()
@@ -1403,6 +1402,8 @@ def __init__(self, data, x, y, **kwargs):
14031402
self.x = x
14041403
self.y = y
14051404

1405+
def _get_layout(self):
1406+
return (1, 1)
14061407

14071408
def _make_plot(self):
14081409
x, y, data = self.x, self.y, self.data
@@ -1442,6 +1443,9 @@ def __init__(self, data, x, y, C=None, **kwargs):
14421443
self.y = y
14431444
self.C = C
14441445

1446+
def _get_layout(self):
1447+
return (1, 1)
1448+
14451449
def _make_plot(self):
14461450
import matplotlib.pyplot as plt
14471451

0 commit comments

Comments
 (0)