Skip to content
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

BUG: df.boxplot fails to use existing axis/subplot (#3578) #6991

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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ Bug Fixes
caused possible color/class mismatch (:issue:`6956`)
- Bug in ``radviz`` and ``andrews_curves`` where multiple values of 'color'
were being passed to plotting method (:issue:`6956`)
- Bug in ``DataFrame.boxplot`` where it failed to use the axis passed as the ``ax`` argument (:issue:`3578`)

pandas 0.13.1
-------------
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,25 @@ def test_boxplot(self):
df['X'] = Series(['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'])
_check_plot_works(df.boxplot, by='X')

# When ax is supplied, existing axes should be used:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
axes = df.boxplot('Col1', by='X', ax=ax)
self.assertIs(ax.get_axes(), axes)

# Multiple columns with an ax argument is not supported
fig, ax = plt.subplots()
self.assertRaisesRegexp(
ValueError, 'existing axis', df.boxplot,
column=['Col1', 'Col2'], by='X', ax=ax
)

# When by is None, check that all relevant lines are present in the dict
fig, ax = plt.subplots()
d = df.boxplot(ax=ax)
lines = list(itertools.chain.from_iterable(d.values()))
self.assertEqual(len(ax.get_lines()), len(lines))

@slow
def test_kde(self):
_skip_if_no_scipy()
Expand Down
14 changes: 10 additions & 4 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,10 +2760,16 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
columns = data._get_numeric_data().columns - by
ngroups = len(columns)

nrows, ncols = _get_layout(ngroups)
fig, axes = _subplots(nrows=nrows, ncols=ncols,
sharex=True, sharey=True,
figsize=figsize, ax=ax)
if ax is None:
nrows, ncols = _get_layout(ngroups)
fig, axes = _subplots(nrows=nrows, ncols=ncols,
sharex=True, sharey=True,
figsize=figsize, ax=ax)
else:
if ngroups > 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could handle this case, assuming that ax is an array with size equal to ngroups. Want to open a separate issue for that?

raise ValueError("Using an existing axis is not supported when plotting multiple columns.")
fig = ax.get_figure()
axes = ax.get_axes()

if isinstance(axes, plt.Axes):
ravel_axes = [axes]
Expand Down