Skip to content

Commit 044feb5

Browse files
Hugues ValoisTomAugspurger
Hugues Valois
authored andcommitted
BUG: Don't ignore figsize in df.boxplot (#16445)
* Propagate the figsize via the rcParams, since matplotlib doesn't allow passing it as a parameter to gca(). * Update what's new for v0.21.0 and use rc_context() to temporarily change rcParams. * Move bug fix from 0.21.0 whatsnew to 0.20.2. * Allow passing in an rc to _gca() instead of just figsize, and added a test for boxplot figsize. * Fix style violations.
1 parent 04356a8 commit 044feb5

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Plotting
6262

6363
- Bug in ``DataFrame.plot`` with a single column and a list-like ``color`` (:issue:`3486`)
6464
- Bug in ``plot`` where ``NaT`` in ``DatetimeIndex`` results in ``Timestamp.min`` (:issue: `12405`)
65+
- Bug in ``DataFrame.boxplot`` where ``figsize`` keyword was not respected for non-grouped boxplots (:issue:`11959`)
6566

6667

6768

pandas/plotting/_core.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ def _get_standard_kind(kind):
4949
return {'density': 'kde'}.get(kind, kind)
5050

5151

52-
def _gca():
52+
def _gca(rc=None):
5353
import matplotlib.pyplot as plt
54-
return plt.gca()
54+
with plt.rc_context(rc):
55+
return plt.gca()
5556

5657

5758
def _gcf():
@@ -1871,12 +1872,6 @@ def plot_series(data, kind='line', ax=None, # Series unique
18711872
**kwds):
18721873

18731874
import matplotlib.pyplot as plt
1874-
"""
1875-
If no axes is specified, check whether there are existing figures
1876-
If there is no existing figures, _gca() will
1877-
create a figure with the default figsize, causing the figsize=parameter to
1878-
be ignored.
1879-
"""
18801875
if ax is None and len(plt.get_fignums()) > 0:
18811876
ax = _gca()
18821877
ax = MPLPlot._get_ax_layer(ax)
@@ -2006,7 +2001,8 @@ def plot_group(keys, values, ax):
20062001
"'by' is None")
20072002

20082003
if ax is None:
2009-
ax = _gca()
2004+
rc = {'figure.figsize': figsize} if figsize is not None else {}
2005+
ax = _gca(rc)
20102006
data = data._get_numeric_data()
20112007
if columns is None:
20122008
columns = data.columns

pandas/tests/plotting/test_boxplot_method.py

+8
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ def test_boxplot_empty_column(self):
160160
df.loc[:, 0] = np.nan
161161
_check_plot_works(df.boxplot, return_type='axes')
162162

163+
@slow
164+
def test_figsize(self):
165+
df = DataFrame(np.random.rand(10, 5),
166+
columns=['A', 'B', 'C', 'D', 'E'])
167+
result = df.boxplot(return_type='axes', figsize=(12, 8))
168+
assert result.figure.bbox_inches.width == 12
169+
assert result.figure.bbox_inches.height == 8
170+
163171
def test_fontsize(self):
164172
df = DataFrame({"a": [1, 2, 3, 4, 5, 6]})
165173
self._check_ticks_props(df.boxplot("a", fontsize=16),

0 commit comments

Comments
 (0)