Skip to content

BUG: Fix bad axis limits when boxplotting with 'by' (#7528) #8126

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

Merged
merged 1 commit into from
Aug 30, 2014
Merged
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/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,8 @@ Bug Fixes

- Bug in ``Float64Index`` where ``iat`` and ``at`` were not testing and were
failing (:issue:`8092`).
- Bug in ``DataFrame.boxplot()`` where y-limits were not set correctly when
producing multiple axes (:issue:`7528`, :issue:`5517`).

- Bug in ``read_csv`` where line comments were not handled correctly given
a custom line terminator or ``delim_whitespace=True`` (:issue:`8122`).
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,34 @@ def test_boxplot_return_type_legacy(self):
result = df.boxplot(return_type='both')
self._check_box_return_type(result, 'both')

@slow
def test_boxplot_axis_limits(self):

def _check_ax_limits(col, ax):
y_min, y_max = ax.get_ylim()
self.assertLessEqual(y_min, col.min())
self.assertGreaterEqual(y_max, col.max())

df = self.hist_df.copy()
df['age'] = np.random.randint(1, 20, df.shape[0])
# One full row
height_ax, weight_ax = df.boxplot(['height', 'weight'], by='category')
_check_ax_limits(df['height'], height_ax)
_check_ax_limits(df['weight'], weight_ax)
self.assertEqual(weight_ax._sharey, height_ax)

# Two rows, one partial
p = df.boxplot(['height', 'weight', 'age'], by='category')
height_ax, weight_ax, age_ax = p[0, 0], p[0, 1], p[1, 0]
dummy_ax = p[1, 1]
_check_ax_limits(df['height'], height_ax)
_check_ax_limits(df['weight'], weight_ax)
_check_ax_limits(df['age'], age_ax)
self.assertEqual(weight_ax._sharey, height_ax)
self.assertEqual(age_ax._sharey, height_ax)
self.assertIsNone(dummy_ax._sharey)


@slow
def test_kde_df(self):
tm._skip_if_no_scipy()
Expand Down
9 changes: 8 additions & 1 deletion pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3174,7 +3174,14 @@ def _subplots(naxes=None, sharex=False, sharey=False, squeeze=True,
# Note off-by-one counting because add_subplot uses the MATLAB 1-based
# convention.
for i in range(1, nplots):
ax = fig.add_subplot(nrows, ncols, i + 1, **subplot_kw)
kwds = subplot_kw.copy()
# Set sharex and sharey to None for blank/dummy axes, these can
# interfere with proper axis limits on the visible axes if
# they share axes e.g. issue #7528
if i >= naxes:
kwds['sharex'] = None
kwds['sharey'] = None
ax = fig.add_subplot(nrows, ncols, i + 1, **kwds)
axarr[i] = ax

if nplots > 1:
Expand Down