Skip to content

Commit f505097

Browse files
author
Tom Augspurger
committed
Merge pull request #8126 from onesandzeroes/axis-limits
BUG: Fix bad axis limits when boxplotting with 'by' (#7528)
2 parents 809f34d + a874c13 commit f505097

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

doc/source/v0.15.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ Bug Fixes
640640

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

644646
- Bug in ``read_csv`` where line comments were not handled correctly given
645647
a custom line terminator or ``delim_whitespace=True`` (:issue:`8122`).

pandas/tests/test_graphics.py

+28
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,34 @@ def test_boxplot_return_type_legacy(self):
18301830
result = df.boxplot(return_type='both')
18311831
self._check_box_return_type(result, 'both')
18321832

1833+
@slow
1834+
def test_boxplot_axis_limits(self):
1835+
1836+
def _check_ax_limits(col, ax):
1837+
y_min, y_max = ax.get_ylim()
1838+
self.assertLessEqual(y_min, col.min())
1839+
self.assertGreaterEqual(y_max, col.max())
1840+
1841+
df = self.hist_df.copy()
1842+
df['age'] = np.random.randint(1, 20, df.shape[0])
1843+
# One full row
1844+
height_ax, weight_ax = df.boxplot(['height', 'weight'], by='category')
1845+
_check_ax_limits(df['height'], height_ax)
1846+
_check_ax_limits(df['weight'], weight_ax)
1847+
self.assertEqual(weight_ax._sharey, height_ax)
1848+
1849+
# Two rows, one partial
1850+
p = df.boxplot(['height', 'weight', 'age'], by='category')
1851+
height_ax, weight_ax, age_ax = p[0, 0], p[0, 1], p[1, 0]
1852+
dummy_ax = p[1, 1]
1853+
_check_ax_limits(df['height'], height_ax)
1854+
_check_ax_limits(df['weight'], weight_ax)
1855+
_check_ax_limits(df['age'], age_ax)
1856+
self.assertEqual(weight_ax._sharey, height_ax)
1857+
self.assertEqual(age_ax._sharey, height_ax)
1858+
self.assertIsNone(dummy_ax._sharey)
1859+
1860+
18331861
@slow
18341862
def test_kde_df(self):
18351863
tm._skip_if_no_scipy()

pandas/tools/plotting.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3174,7 +3174,14 @@ def _subplots(naxes=None, sharex=False, sharey=False, squeeze=True,
31743174
# Note off-by-one counting because add_subplot uses the MATLAB 1-based
31753175
# convention.
31763176
for i in range(1, nplots):
3177-
ax = fig.add_subplot(nrows, ncols, i + 1, **subplot_kw)
3177+
kwds = subplot_kw.copy()
3178+
# Set sharex and sharey to None for blank/dummy axes, these can
3179+
# interfere with proper axis limits on the visible axes if
3180+
# they share axes e.g. issue #7528
3181+
if i >= naxes:
3182+
kwds['sharex'] = None
3183+
kwds['sharey'] = None
3184+
ax = fig.add_subplot(nrows, ncols, i + 1, **kwds)
31783185
axarr[i] = ax
31793186

31803187
if nplots > 1:

0 commit comments

Comments
 (0)