Skip to content

Commit 810cb2d

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. (cherry picked from commit 044feb5)
1 parent de94073 commit 810cb2d

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

doc/source/whatsnew/v0.20.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Plotting
6464
^^^^^^^^
6565

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

6870

6971

pandas/plotting/_core.py

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

5050

51-
def _gca():
51+
def _gca(rc=None):
5252
import matplotlib.pyplot as plt
53-
return plt.gca()
53+
with plt.rc_context(rc):
54+
return plt.gca()
5455

5556

5657
def _gcf():
@@ -1869,12 +1870,6 @@ def plot_series(data, kind='line', ax=None, # Series unique
18691870
**kwds):
18701871

18711872
import matplotlib.pyplot as plt
1872-
"""
1873-
If no axes is specified, check whether there are existing figures
1874-
If there is no existing figures, _gca() will
1875-
create a figure with the default figsize, causing the figsize=parameter to
1876-
be ignored.
1877-
"""
18781873
if ax is None and len(plt.get_fignums()) > 0:
18791874
ax = _gca()
18801875
ax = MPLPlot._get_ax_layer(ax)
@@ -2004,7 +1999,8 @@ def plot_group(keys, values, ax):
20041999
"'by' is None")
20052000

20062001
if ax is None:
2007-
ax = _gca()
2002+
rc = {'figure.figsize': figsize} if figsize is not None else {}
2003+
ax = _gca(rc)
20082004
data = data._get_numeric_data()
20092005
if columns is None:
20102006
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)