Skip to content

BUG-26214 fix colors parameter in DataFrame.boxplot #26456

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 7 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ Plotting
- Fixed bug where :class:`api.extensions.ExtensionArray` could not be used in matplotlib plotting (:issue:`25587`)
- Bug in an error message in :meth:`DataFrame.plot`. Improved the error message if non-numerics are passed to :meth:`DataFrame.plot` (:issue:`25481`)
- Bug in incorrect ticklabel positions when plotting an index that are non-numeric / non-datetime (:issue:`7612` :issue:`15912` :issue:`22334`)
-
- Bug where :meth:`DataFrame.boxplot` would not accept a `color` parameter like `DataFrame.plot.box` (:issue:`26214`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the behavior here match exactly what you'd get with DataFrame.plot.box? I assume the latter just passes kwargs on through but this looks to do special handling, so not entirely equivalent right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataFrame.plot.box does special handling too. Neither color nor any of its valid keys are listed as a valid kwarg in matplotlib.pyplot.boxplot

-

Groupby/Resample/Rolling
Expand Down
23 changes: 17 additions & 6 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2202,14 +2202,25 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None,
def _get_colors():
# num_colors=3 is required as method maybe_color_bp takes the colors
# in positions 0 and 2.
return _get_standard_colors(color=kwds.get('color'), num_colors=3)
# if colors not provided, use color 2 for medians and 0 for all else
result = _get_standard_colors(num_colors=3)
result = np.take(result, [0, 0, 2, 0])

colors = kwds.pop('color', None)
if colors and isinstance(colors, dict):
valid_keys = ['boxes', 'whiskers', 'medians', 'caps']
for i in range(4):
if valid_keys[i] in colors:
result[i] = colors[valid_keys[i]]

return result

def maybe_color_bp(bp):
if 'color' not in kwds:
from matplotlib.artist import setp
setp(bp['boxes'], color=colors[0], alpha=1)
setp(bp['whiskers'], color=colors[0], alpha=1)
setp(bp['medians'], color=colors[2], alpha=1)
from matplotlib.artist import setp
setp(bp['boxes'], color=colors[0], alpha=1)
setp(bp['whiskers'], color=colors[1], alpha=1)
setp(bp['medians'], color=colors[2], alpha=1)
setp(bp['caps'], color=colors[3], alpha=1)

def plot_group(keys, values, ax):
keys = [pprint_thing(x) for x in keys]
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ def test_fontsize(self):
self._check_ticks_props(df.boxplot("a", fontsize=16),
xlabelsize=16, ylabelsize=16)

@pytest.mark.parametrize('dict_colors, expected',
[(dict(boxes='r',
whiskers='b',
medians='g',
caps='c'),
dict(boxes='r',
whiskers='b',
medians='g',
caps='c')),
(dict(boxes='r', invalid='b'), dict(boxes='r')),
(102, dict())])
def test_color_kwd(self, dict_colors, expected):
# GH: 26214
df = DataFrame(random.rand(10, 2))
result = df.boxplot(color=dict_colors, return_type='dict')
for k, v in expected.items():
assert result[k][0].get_color() == v


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):
Expand Down