Don't ignore figsize in df.boxplot
State: closed

Work in progress from PyCon sprints

Fix for https://github.com/pandas-dev/pandas/issues/11959

It really doesn't feel like a proper fix, but I'm not sure what a proper fix would be (without changing matplotlib).  The matplotlib `gca()` ends up calling `figure()`, but doesn't let us pass in a `figsize` to `gca()`.

Any advice? FYI this works, ie it fixes the behavior for the code snippet in the bug report. It would obviously break in multithreaded scenarios, but I'm not sure if that's a consideration or not.

Labels:
name: Visualization
description: plotting

Comments:
Thanks @huguesv. I think the issue is somewhere around https://github.com/pandas-dev/pandas/blob/49ec31bbaeca81a6f58fc1be26fe80f3ac188cdd/pandas/plotting/_core.py#L2009

This simplest fix would be to wrap that call in a `plt.rc_context` like

```python

In [8]: with plt.rc_context({'figure.figsize': figsize}):
   ...:     ax = plt.gca()
 ```

Alternatively you could modify `_gca` to take an `rc` (a dictionary) like `plt.rc_context`. And just pass that through. Make sense?
Sorry I got confused by the diff. I see that you found the right line :)
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=h1) Report
> Merging [#16445](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/49ec31bbaeca81a6f58fc1be26fe80f3ac188cdd?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `70%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16445/graphs/tree.svg?src=pr&width=650&token=eZ4WkYLtcO&height=150)](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16445      +/-   ##
==========================================
- Coverage   90.42%   90.41%   -0.01%     
==========================================
  Files         161      161              
  Lines       51023    51029       +6     
==========================================
+ Hits        46138    46140       +2     
- Misses       4885     4889       +4
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.25% <70%> (-0.01%)` | :arrow_down: |
| #single | `40.16% <10%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/plotting/\_core.py](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb3JlLnB5) | `81.75% <70%> (-0.15%)` | :arrow_down: |
| [pandas/core/common.py](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvY29tbW9uLnB5) | `91.05% <0%> (-0.34%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=footer). Last update [49ec31b...3df3a2e](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=h1) Report
> Merging [#16445](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/a8a497f0d8c1acd472d57dfb48832292fb3f8c2e?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/16445/graphs/tree.svg?width=650&height=150&src=pr&token=eZ4WkYLtcO)](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #16445      +/-   ##
==========================================
+ Coverage   90.42%   90.42%   +<.01%     
==========================================
  Files         161      161              
  Lines       51023    51027       +4     
==========================================
+ Hits        46138    46142       +4     
  Misses       4885     4885
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `88.26% <100%> (ø)` | :arrow_up: |
| #single | `40.17% <20%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/plotting/\_core.py](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree#diff-cGFuZGFzL3Bsb3R0aW5nL19jb3JlLnB5) | `81.92% <100%> (+0.02%)` | :arrow_up: |
| [pandas/io/formats/style.py](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvc3R5bGUucHk=) | `95.92% <0%> (+0.01%)` | :arrow_up: |
| [pandas/io/excel.py](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=tree#diff-cGFuZGFzL2lvL2V4Y2VsLnB5) | `62.31% <0%> (+0.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=footer). Last update [a8a497f...bab6ea2](https://codecov.io/gh/pandas-dev/pandas/pull/16445?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

You can add a release note in `doc/source/whatsnew/v0.20.2.txt` under the "Other Enhancements" section, about how `DataFrame.boxplot` now respects the `figsize` keyword for non-grouped boxplots.
Oops, changed the wrong whatsnew file, let me fix that.
Did you have a test earlier? I thought you did, but maybe not. Anyway, could you add one in `pandas/tests/plotting/test_boxplot_method.py`  Just need to verify that `ax = df.boxplot(figsize=(4, 4))` or whatver actually has the correct figsize.
Added test.
The one test failure seemed unrealted. I've restarted it to be safe though: https://travis-ci.org/pandas-dev/pandas/jobs/235323266
All tests passed! :)
@huguesv thanks. Nice job!