Skip to content

Commit 3395eed

Browse files
committed
BUG: Area plot legend has incorrect color
1 parent 534784b commit 3395eed

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ Bug Fixes
472472
times were returned when crossing DST boundaries (:issue:`7835`, :issue:`7901`).
473473

474474

475-
475+
- Bug in area plot draws legend with incorrect ``alpha`` when ``stacked=True`` (:issue:`8027`)
476476

477477
- ``Period`` and ``PeriodIndex`` addition/subtraction with ``np.timedelta64`` results in incorrect internal representations (:issue:`7740`)
478478

pandas/tests/test_graphics.py

+27-9
Original file line numberDiff line numberDiff line change
@@ -1718,15 +1718,15 @@ def test_hist_df_coord(self):
17181718
normal_df = DataFrame({'A': np.repeat(np.array([1, 2, 3, 4, 5]),
17191719
np.array([10, 9, 8, 7, 6])),
17201720
'B': np.repeat(np.array([1, 2, 3, 4, 5]),
1721-
np.array([8, 8, 8, 8, 8])),
1721+
np.array([8, 8, 8, 8, 8])),
17221722
'C': np.repeat(np.array([1, 2, 3, 4, 5]),
17231723
np.array([6, 7, 8, 9, 10]))},
17241724
columns=['A', 'B', 'C'])
17251725

17261726
nan_df = DataFrame({'A': np.repeat(np.array([np.nan, 1, 2, 3, 4, 5]),
17271727
np.array([3, 10, 9, 8, 7, 6])),
17281728
'B': np.repeat(np.array([1, np.nan, 2, 3, 4, 5]),
1729-
np.array([8, 3, 8, 8, 8, 8])),
1729+
np.array([8, 3, 8, 8, 8, 8])),
17301730
'C': np.repeat(np.array([1, 2, 3, np.nan, 4, 5]),
17311731
np.array([6, 7, 8, 3, 9, 10]))},
17321732
columns=['A', 'B', 'C'])
@@ -2157,20 +2157,38 @@ def test_area_colors(self):
21572157
self._check_colors(ax.get_lines(), linecolors=custom_colors)
21582158
poly = [o for o in ax.get_children() if isinstance(o, PolyCollection)]
21592159
self._check_colors(poly, facecolors=custom_colors)
2160+
2161+
handles, labels = ax.get_legend_handles_labels()
2162+
# legend is stored as Line2D, thus check linecolors
2163+
self._check_colors(handles, linecolors=custom_colors)
2164+
for h in handles:
2165+
self.assertTrue(h.get_alpha() is None)
21602166
tm.close()
21612167

21622168
ax = df.plot(kind='area', colormap='jet')
2163-
rgba_colors = lmap(cm.jet, np.linspace(0, 1, len(df)))
2164-
self._check_colors(ax.get_lines(), linecolors=rgba_colors)
2169+
jet_colors = lmap(cm.jet, np.linspace(0, 1, len(df)))
2170+
self._check_colors(ax.get_lines(), linecolors=jet_colors)
21652171
poly = [o for o in ax.get_children() if isinstance(o, PolyCollection)]
2166-
self._check_colors(poly, facecolors=rgba_colors)
2172+
self._check_colors(poly, facecolors=jet_colors)
2173+
2174+
handles, labels = ax.get_legend_handles_labels()
2175+
self._check_colors(handles, linecolors=jet_colors)
2176+
for h in handles:
2177+
self.assertTrue(h.get_alpha() is None)
21672178
tm.close()
21682179

2169-
ax = df.plot(kind='area', colormap=cm.jet)
2170-
rgba_colors = lmap(cm.jet, np.linspace(0, 1, len(df)))
2171-
self._check_colors(ax.get_lines(), linecolors=rgba_colors)
2180+
# When stacked=True, alpha is set to 0.5
2181+
ax = df.plot(kind='area', colormap=cm.jet, stacked=False)
2182+
self._check_colors(ax.get_lines(), linecolors=jet_colors)
21722183
poly = [o for o in ax.get_children() if isinstance(o, PolyCollection)]
2173-
self._check_colors(poly, facecolors=rgba_colors)
2184+
jet_with_alpha = [(c[0], c[1], c[2], 0.5) for c in jet_colors]
2185+
self._check_colors(poly, facecolors=jet_with_alpha)
2186+
2187+
handles, labels = ax.get_legend_handles_labels()
2188+
# Line2D can't have alpha in its linecolor
2189+
self._check_colors(handles, linecolors=jet_colors)
2190+
for h in handles:
2191+
self.assertEqual(h.get_alpha(), 0.5)
21742192

21752193
@slow
21762194
def test_hist_colors(self):

pandas/tools/plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ def _add_legend_handle(self, handle, label, index=None):
16861686
from matplotlib.patches import Rectangle
16871687
# Because fill_between isn't supported in legend,
16881688
# specifically add Rectangle handle here
1689-
alpha = self.kwds.get('alpha', 0.5)
1689+
alpha = self.kwds.get('alpha', None)
16901690
handle = Rectangle((0, 0), 1, 1, fc=handle.get_color(), alpha=alpha)
16911691
LinePlot._add_legend_handle(self, handle, label, index=index)
16921692

0 commit comments

Comments
 (0)