Skip to content

PLT: Color attributes of medianprops etc are lost in df.boxplot and df.plot.boxplot #31262

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 15 commits into from
Feb 11, 2020
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ Plotting

- :func:`.plot` for line/bar now accepts color by dictonary (:issue:`8193`).
-
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)


Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
32 changes: 22 additions & 10 deletions pandas/plotting/_matplotlib/boxplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,16 @@ def maybe_color_bp(self, bp):
medians = self.color or self._medians_c
caps = self.color or self._caps_c

setp(bp["boxes"], color=boxes, alpha=1)
setp(bp["whiskers"], color=whiskers, alpha=1)
setp(bp["medians"], color=medians, alpha=1)
setp(bp["caps"], color=caps, alpha=1)
# GH 30346, when users specifying those arguments explicitly, our defaults
# for these four kwargs should be overridden; if not, use Pandas settings
if not self.kwds.get("boxprops"):
setp(bp["boxes"], color=boxes, alpha=1)
if not self.kwds.get("whiskerprops"):
setp(bp["whiskers"], color=whiskers, alpha=1)
if not self.kwds.get("medianprops"):
setp(bp["medians"], color=medians, alpha=1)
if not self.kwds.get("capprops"):
setp(bp["caps"], color=caps, alpha=1)

def _make_plot(self):
if self.subplots:
Expand Down Expand Up @@ -275,11 +281,17 @@ def _get_colors():

return result

def maybe_color_bp(bp):
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 maybe_color_bp(bp, **kwds):
# GH 30346, when users specifying those arguments explicitly, our defaults
# for these four kwargs should be overridden; if not, use Pandas settings
if not kwds.get("boxprops"):
setp(bp["boxes"], color=colors[0], alpha=1)
if not kwds.get("whiskerprops"):
setp(bp["whiskers"], color=colors[1], alpha=1)
if not kwds.get("medianprops"):
setp(bp["medians"], color=colors[2], alpha=1)
if not kwds.get("capprops"):
setp(bp["caps"], color=colors[3], alpha=1)

def plot_group(keys, values, ax):
keys = [pprint_thing(x) for x in keys]
Expand All @@ -291,7 +303,7 @@ def plot_group(keys, values, ax):
ax.set_xticklabels(keys, rotation=rot)
else:
ax.set_yticklabels(keys, rotation=rot)
maybe_color_bp(bp)
maybe_color_bp(bp, **kwds)

# Return axes in multiplot case, maybe revisit later # 985
if return_type == "dict":
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ def test_color_kwd_errors(self, dict_colors, msg):
with pytest.raises(ValueError, match=msg):
df.boxplot(color=dict_colors, return_type="dict")

@pytest.mark.parametrize(
"props, expected",
[
("boxprops", "boxes"),
("whiskerprops", "whiskers"),
("capprops", "caps"),
("medianprops", "medians"),
],
)
def test_specified_props_kwd(self, props, expected):
# GH 30346
df = DataFrame({k: np.random.random(100) for k in "ABC"})
kwd = {props: dict(color="C1")}
result = df.boxplot(return_type="dict", **kwd)

assert result[expected][0].get_color() == "C1"


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,23 @@ def _check_colors(bp, box_c, whiskers_c, medians_c, caps_c="k", fliers_c=None):
# Color contains invalid key results in ValueError
df.plot.box(color=dict(boxes="red", xxxx="blue"))

@pytest.mark.parametrize(
"props, expected",
[
("boxprops", "boxes"),
("whiskerprops", "whiskers"),
("capprops", "caps"),
("medianprops", "medians"),
],
)
def test_specified_props_kwd_plot_box(self, props, expected):
# GH 30346
df = DataFrame({k: np.random.random(100) for k in "ABC"})
kwd = {props: dict(color="C1")}
result = df.plot.box(return_type="dict", **kwd)

assert result[expected][0].get_color() == "C1"

def test_default_color_cycle(self):
import matplotlib.pyplot as plt
import cycler
Expand Down