Skip to content

BUG: enabling subplots works unexpectedly #6951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ Bug Fixes
- Bug in ``DataFrame.apply`` with functions that used *args or **kwargs and returned
an empty result (:issue:`6952`)
- Bug in sum/mean on 32-bit platforms on overflows (:issue:`6915`)
- Bug in enabling ``subplots=True`` in ``DataFrame.plot`` only has single column raises ``TypeError``, and ``Series.plot`` raises ``AttributeError`` (:issue:`6951`)
- Bug in ``DataFrame.plot`` draws unnecessary axes when enabling ``subplots`` and ``kind=scatter`` (:issue:`6951`)

pandas 0.13.1
-------------
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def test_plot(self):
_check_plot_works(self.series[:10].plot, kind='barh')
_check_plot_works(Series(randn(10)).plot, kind='bar', color='black')

# GH 6951
_check_plot_works(self.ts.plot, subplots=True)

@slow
def test_plot_figsize_and_title(self):
# figsize and title
Expand Down Expand Up @@ -367,6 +370,11 @@ def test_plot(self):
index=index)
_check_plot_works(df.plot, title=u('\u03A3'))

# GH 6951
# Test with single column
df = DataFrame({'x': np.random.rand(10)})
_check_plot_works(df.plot, kind='bar', subplots=True)

def test_nonnumeric_exclude(self):
import matplotlib.pyplot as plt
df = DataFrame({'A': ["x", "y", "z"], 'B': [1, 2, 3]})
Expand Down Expand Up @@ -665,6 +673,10 @@ def test_plot_scatter(self):
with tm.assertRaises(ValueError):
df.plot(y='y', kind='scatter')

# GH 6951
axes = df.plot(x='x', y='y', kind='scatter', subplots=True)
self.assertEqual(len(axes[0].figure.axes), 1)

@slow
def test_plot_bar(self):
from matplotlib.pylab import close
Expand Down Expand Up @@ -1271,6 +1283,11 @@ def test_hexbin_basic(self):
# TODO: need better way to test. This just does existence.
self.assertEqual(len(ax.collections), 1)

# GH 6951
axes = df.plot(x='A', y='B', kind='hexbin', subplots=True)
# hexbin should have 2 axes, 1 for plotting and another is colorbar
self.assertEqual(len(axes[0].figure.axes), 2)

@slow
def test_hexbin_with_c(self):
df = DataFrame({"A": np.random.uniform(size=20),
Expand Down
30 changes: 17 additions & 13 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,18 +966,13 @@ def _maybe_right_yaxis(self, ax):
def _setup_subplots(self):
if self.subplots:
nrows, ncols = self._get_layout()
if self.ax is None:
fig, axes = _subplots(nrows=nrows, ncols=ncols,
sharex=self.sharex, sharey=self.sharey,
figsize=self.figsize,
secondary_y=self.secondary_y,
data=self.data)
else:
fig, axes = _subplots(nrows=nrows, ncols=ncols,
sharex=self.sharex, sharey=self.sharey,
figsize=self.figsize, ax=self.ax,
secondary_y=self.secondary_y,
data=self.data)
fig, axes = _subplots(nrows=nrows, ncols=ncols,
sharex=self.sharex, sharey=self.sharey,
figsize=self.figsize, ax=self.ax,
secondary_y=self.secondary_y,
data=self.data)
if not com.is_list_like(axes):
axes = np.array([axes])
else:
if self.ax is None:
fig = self.plt.figure(figsize=self.figsize)
Expand All @@ -1000,7 +995,11 @@ def _setup_subplots(self):
self.axes = axes

def _get_layout(self):
return (len(self.data.columns), 1)
from pandas.core.frame import DataFrame
if isinstance(self.data, DataFrame):
return (len(self.data.columns), 1)
else:
return (1, 1)

def _compute_plot_data(self):
numeric_data = self.data.convert_objects()._get_numeric_data()
Expand Down Expand Up @@ -1403,6 +1402,8 @@ def __init__(self, data, x, y, **kwargs):
self.x = x
self.y = y

def _get_layout(self):
return (1, 1)

def _make_plot(self):
x, y, data = self.x, self.y, self.data
Expand Down Expand Up @@ -1442,6 +1443,9 @@ def __init__(self, data, x, y, C=None, **kwargs):
self.y = y
self.C = C

def _get_layout(self):
return (1, 1)

def _make_plot(self):
import matplotlib.pyplot as plt

Expand Down