Skip to content

Commit 968aecc

Browse files
committed
COMPAT: use mpl area legend if available
1 parent 142c796 commit 968aecc

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@ Bug Fixes
849849
- Bug in ``pd.read_csv()``, which caused BOM files to be incorrectly parsed by not ignoring the BOM (:issue:`4793`)
850850
- Bug in ``io.json.json_normalize()``, where non-ascii keys raised an exception (:issue:`13213`)
851851
- Bug when passing a not-default-indexed ``Series`` as ``xerr`` or ``yerr`` in ``.plot()`` (:issue:`11858`)
852+
- Bug in area plot draws legend incorrectly if subplot is enabled or legend is moved after plot (matplotlib 1.5.0 is required to draw area plot legend properly) (issue:`9161`, :issue:`13544`)
852853
- Bug in matplotlib ``AutoDataFormatter``; this restores the second scaled formatting and re-adds micro-second scaled formatting (:issue:`13131`)
853854
- Bug in selection from a ``HDFStore`` with a fixed format and ``start`` and/or ``stop`` specified will now return the selected range (:issue:`8287`)
854855
- Bug in ``Series`` construction from a tuple of integers on windows not returning default dtype (int64) (:issue:`13646`)

pandas/tools/plotting.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1839,10 +1839,16 @@ def __init__(self, data, **kwargs):
18391839
@classmethod
18401840
def _plot(cls, ax, x, y, style=None, column_num=None,
18411841
stacking_id=None, is_errorbar=False, **kwds):
1842+
18421843
if column_num == 0:
18431844
cls._initialize_stacker(ax, stacking_id, len(y))
18441845
y_values = cls._get_stacked_values(ax, stacking_id, y, kwds['label'])
1845-
lines = MPLPlot._plot(ax, x, y_values, style=style, **kwds)
1846+
1847+
# need to remove label, because subplots uses mpl legend as it is
1848+
line_kwds = kwds.copy()
1849+
if cls.mpl_ge_1_5_0():
1850+
line_kwds.pop('label')
1851+
lines = MPLPlot._plot(ax, x, y_values, style=style, **line_kwds)
18461852

18471853
# get data from the line to get coordinates for fill_between
18481854
xdata, y_values = lines[0].get_data(orig=False)
@@ -1860,18 +1866,21 @@ def _plot(cls, ax, x, y, style=None, column_num=None,
18601866
if 'color' not in kwds:
18611867
kwds['color'] = lines[0].get_color()
18621868

1863-
if cls.mpl_ge_1_5_0(): # mpl 1.5 added real support for poly legends
1864-
kwds.pop('label')
1865-
ax.fill_between(xdata, start, y_values, **kwds)
1869+
rect = ax.fill_between(xdata, start, y_values, **kwds)
18661870
cls._update_stacker(ax, stacking_id, y)
1867-
return lines
1871+
1872+
# LinePlot expects list of artists
1873+
res = [rect] if cls.mpl_ge_1_5_0() else lines
1874+
return res
18681875

18691876
def _add_legend_handle(self, handle, label, index=None):
1870-
from matplotlib.patches import Rectangle
1871-
# Because fill_between isn't supported in legend,
1872-
# specifically add Rectangle handle here
1873-
alpha = self.kwds.get('alpha', None)
1874-
handle = Rectangle((0, 0), 1, 1, fc=handle.get_color(), alpha=alpha)
1877+
if not self.mpl_ge_1_5_0():
1878+
from matplotlib.patches import Rectangle
1879+
# Because fill_between isn't supported in legend,
1880+
# specifically add Rectangle handle here
1881+
alpha = self.kwds.get('alpha', None)
1882+
handle = Rectangle((0, 0), 1, 1, fc=handle.get_color(),
1883+
alpha=alpha)
18751884
LinePlot._add_legend_handle(self, handle, label, index=index)
18761885

18771886
def _post_plot_logic(self, ax, data):

0 commit comments

Comments
 (0)