Skip to content

Commit f851b57

Browse files
onesandzeroesTom Augspurger
authored and
Tom Augspurger
committed
BUG: df.boxplot fails to use existing axis/subplot (#3578)
axes needs to be a plt.Axes object use ax get_axes() to set axes Add tests for use of existing axis Add test for when by is None Add fix to the release notes
1 parent 668f0f7 commit f851b57

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ Bug Fixes
477477
were being passed to plotting method (:issue:`6956`)
478478
- Bug in ``Float64Index.isin()`` where containing ``nan`` s would make indices
479479
claim that they contained all the things (:issue:`7066`).
480+
- Bug in ``DataFrame.boxplot`` where it failed to use the axis passed as the ``ax`` argument (:issue:`3578`)
480481

481482
pandas 0.13.1
482483
-------------

pandas/tests/test_graphics.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,25 @@ def test_boxplot(self):
10831083
df['X'] = Series(['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'])
10841084
_check_plot_works(df.boxplot, by='X')
10851085

1086+
# When ax is supplied, existing axes should be used:
1087+
import matplotlib.pyplot as plt
1088+
fig, ax = plt.subplots()
1089+
axes = df.boxplot('Col1', by='X', ax=ax)
1090+
self.assertIs(ax.get_axes(), axes)
1091+
1092+
# Multiple columns with an ax argument is not supported
1093+
fig, ax = plt.subplots()
1094+
self.assertRaisesRegexp(
1095+
ValueError, 'existing axis', df.boxplot,
1096+
column=['Col1', 'Col2'], by='X', ax=ax
1097+
)
1098+
1099+
# When by is None, check that all relevant lines are present in the dict
1100+
fig, ax = plt.subplots()
1101+
d = df.boxplot(ax=ax)
1102+
lines = list(itertools.chain.from_iterable(d.values()))
1103+
self.assertEqual(len(ax.get_lines()), len(lines))
1104+
10861105
@slow
10871106
def test_kde(self):
10881107
_skip_if_no_scipy()

pandas/tools/plotting.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,10 +2760,16 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
27602760
columns = data._get_numeric_data().columns - by
27612761
ngroups = len(columns)
27622762

2763-
nrows, ncols = _get_layout(ngroups)
2764-
fig, axes = _subplots(nrows=nrows, ncols=ncols,
2765-
sharex=True, sharey=True,
2766-
figsize=figsize, ax=ax)
2763+
if ax is None:
2764+
nrows, ncols = _get_layout(ngroups)
2765+
fig, axes = _subplots(nrows=nrows, ncols=ncols,
2766+
sharex=True, sharey=True,
2767+
figsize=figsize, ax=ax)
2768+
else:
2769+
if ngroups > 1:
2770+
raise ValueError("Using an existing axis is not supported when plotting multiple columns.")
2771+
fig = ax.get_figure()
2772+
axes = ax.get_axes()
27672773

27682774
if isinstance(axes, plt.Axes):
27692775
ravel_axes = [axes]

0 commit comments

Comments
 (0)