Skip to content

Commit 902b386

Browse files
author
y-p
committed
ENH: boxplot color scheme respects MPL style
1 parent 7e79dbe commit 902b386

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

pandas/tools/plotting.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,16 +1689,34 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None,
16891689
data = DataFrame({'x': data})
16901690
column = 'x'
16911691

1692+
1693+
def _get_colors():
1694+
import matplotlib.pyplot as plt
1695+
cycle = plt.rcParams.get('axes.color_cycle', list('bgrcmyk'))
1696+
if isinstance(cycle, basestring):
1697+
cycle = list(cycle)
1698+
colors = kwds.get('color', cycle)
1699+
return colors
1700+
1701+
def maybe_color_bp(bp):
1702+
if 'color' not in kwds :
1703+
from matplotlib.artist import setp
1704+
setp(bp['boxes'],color=colors[0],alpha=1)
1705+
setp(bp['whiskers'],color=colors[0],alpha=1)
1706+
setp(bp['medians'],color=colors[2],alpha=1)
1707+
16921708
def plot_group(grouped, ax):
16931709
keys, values = zip(*grouped)
16941710
keys = [com.pprint_thing(x) for x in keys]
16951711
values = [remove_na(v) for v in values]
1696-
ax.boxplot(values, **kwds)
1712+
bp = ax.boxplot(values, **kwds)
16971713
if kwds.get('vert', 1):
16981714
ax.set_xticklabels(keys, rotation=rot, fontsize=fontsize)
16991715
else:
17001716
ax.set_yticklabels(keys, rotation=rot, fontsize=fontsize)
1717+
maybe_color_bp(bp)
17011718

1719+
colors = _get_colors()
17021720
if column is None:
17031721
columns = None
17041722
else:
@@ -1731,7 +1749,10 @@ def plot_group(grouped, ax):
17311749
# Return boxplot dict in single plot case
17321750

17331751
clean_values = [remove_na(x) for x in data[cols].values.T]
1752+
17341753
bp = ax.boxplot(clean_values, **kwds)
1754+
maybe_color_bp(bp)
1755+
17351756
if kwds.get('vert', 1):
17361757
ax.set_xticklabels(keys, rotation=rot, fontsize=fontsize)
17371758
else:

0 commit comments

Comments
 (0)