pydata/pandas — pull request #6991 by onesandzeroes (2014-04-28 12:52:51+00:00)
Title: BUG: df.boxplot fails to use existing axis/subplot (#3578)

Alright, I think I have a fix for issue #3578. In `plotting._grouped_plot_by_column`, there was no check being done for whether `ax` was `None`, which was the main source of the issue. When the boxplot is of multiple columns, I don't think there's anything sensible that can be done with the `ax` argument, so that
now raises a `ValueError`.

I've written some tests, but I'm not sure if they really get to the heart of the problem, so any insight on how to improve them would be appreciated.

Testing code adapted from the original bug report to demonstrate the new behaviour:

```python
import matplotlib.pyplot as plt
import pandas
from pandas import DataFrame, Series

data = {'day': Series([1, 1, 1, 2, 2, 2, 3, 3, 3]),
'group2': Series([2, 2, 2, 2, 2, 1, 1, 1, 1]),
'val': Series([3, 4, 5, 6, 7, 8, 9, 10, 11]),
'val2': Series([8, 9, 10, 11, 12, 13, 14, 15])}
df = pandas.DataFrame(data)

# Single-column using existing axis: should create a single plot
plt.figure()
plt.subplot(2, 2, 1)
plt.plot([1, 2, 3])
plt.subplot(2, 2, 4)
ax = plt.gca()
fig = ax.get_figure()
axes = df.boxplot('val', 'day', ax=ax)
print("Testing identity of returned axes: (should be True)")
print(id(axes) == id(ax.get_axes()))
plt.show()

# Multiple column, not using existing axis: should create two plots
plt.figure()
plt.subplot(2, 2, 1)
plt.plot([1, 2, 3])
plt.subplot(2, 2, 4)
ax = plt.gca()
axes = df.boxplot(['val', 'val2'], 'day')
print("Testing identity of returned axes: (should be False)")
print(id(axes) == id(ax.get_axes()))
plt.show()

# Multiple column using existing axis: should raise an exception,
# since it's hard to know what to do: we need an axis for each
# column and ax is just a single axis object
plt.figure()
plt.subplot(2, 2, 1)
plt.plot([1, 2, 3])
plt.subplot(2, 2, 4)
ax = plt.gca()
try:
df.boxplot(['val', 'val2'], 'day', ax=ax)
except ValueError:
print("Raising exception as expected")
plt.show()
```