Skip to content

resolve issue 368 #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3054,6 +3054,7 @@ def boxplot(self, column=None, by=None, ax=None, fontsize=None,
ax = plots.boxplot(self, column=column, by=by, ax=ax, fontsize=fontsize,
grid=grid, rot=rot)
plt.draw_if_interactive()
return ax

def plot(self, subplots=False, sharex=True, sharey=False, use_index=True,
figsize=None, grid=True, legend=True, rot=30, ax=None,
Expand Down
30 changes: 23 additions & 7 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None,
Parameters
----------
data : DataFrame
column : column names or list of names, or vector
column : column name or list of names, or vector
Can be any valid input to groupby
by : string or sequence
Column in the DataFrame to group by
Expand All @@ -37,22 +37,34 @@ def plot_group(grouped, ax):
ax.boxplot(values)
ax.set_xticklabels(keys, rotation=rot, fontsize=fontsize)

if column == None:
columns = None
else:
if isinstance(column, (list, tuple)):
columns = column
else:
columns = [column]

if by is not None:
if not isinstance(by, (list, tuple)):
by = [by]

columns = None if column is None else [column]
fig, axes = _grouped_plot_by_column(plot_group, data, columns=columns,
by=by)
by=by, grid=grid)
ax = axes
else:
if ax is None:
ax = plt.gca()

data = data._get_numeric_data()
keys = [_stringify(x) for x in data.columns]
ax.boxplot(list(data.values.T))
if columns:
cols = columns
else:
cols = data.columns
keys = [_stringify(x) for x in cols]
ax.boxplot(list(data[cols].values.T))
ax.set_xticklabels(keys, rotation=rot, fontsize=fontsize)
ax.grid(grid)

plt.subplots_adjust(bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.1)
return ax
Expand Down Expand Up @@ -108,7 +120,7 @@ def _grouped_plot(plotf, data, by=None, numeric_only=True):
return fig, axes

def _grouped_plot_by_column(plotf, data, columns=None, by=None,
numeric_only=True):
numeric_only=True, grid=False):
grouped = data.groupby(by)
if columns is None:
columns = data._get_numeric_data().columns - by
Expand All @@ -123,13 +135,17 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
else:
ravel_axes = []
for row in axes:
ravel_axes.extend(row)
if isinstance(row, plt.Axes):
ravel_axes.append(row)
else:
ravel_axes.extend(row)

for i, col in enumerate(columns):
ax = ravel_axes[i]
gp_col = grouped[col]
plotf(gp_col, ax)
ax.set_title(col)
ax.grid(grid)

fig.suptitle('Boxplot grouped by %s' % by)

Expand Down