From 7e461a18d9f6928132afec6f48ce968b3e989ba6 Mon Sep 17 00:00:00 2001 From: Kaiqi Dong Date: Mon, 3 Dec 2018 17:43:52 +0100 Subject: [PATCH 01/16] remove \n from docstring --- pandas/core/arrays/datetimes.py | 26 +++++++++++++------------- pandas/core/arrays/timedeltas.py | 16 ++++++++-------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index cfe3afcf3730a..b3df505d56d78 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -82,7 +82,7 @@ def f(self): return result f.__name__ = name - f.__doc__ = docstring + f.__doc__ = "\n{}\n".format(docstring) return property(f) @@ -1072,19 +1072,19 @@ def date(self): return tslib.ints_to_pydatetime(timestamps, box="date") - year = _field_accessor('year', 'Y', "\n The year of the datetime\n") + year = _field_accessor('year', 'Y', "The year of the datetime") month = _field_accessor('month', 'M', - "\n The month as January=1, December=12 \n") - day = _field_accessor('day', 'D', "\nThe days of the datetime\n") - hour = _field_accessor('hour', 'h', "\nThe hours of the datetime\n") - minute = _field_accessor('minute', 'm', "\nThe minutes of the datetime\n") - second = _field_accessor('second', 's', "\nThe seconds of the datetime\n") + "The month as January=1, December=12") + day = _field_accessor('day', 'D', "The days of the datetime") + hour = _field_accessor('hour', 'h', "The hours of the datetime") + minute = _field_accessor('minute', 'm', "The minutes of the datetime") + second = _field_accessor('second', 's', "The seconds of the datetime") microsecond = _field_accessor('microsecond', 'us', - "\nThe microseconds of the datetime\n") + "The microseconds of the datetime") nanosecond = _field_accessor('nanosecond', 'ns', - "\nThe nanoseconds of the datetime\n") + "The nanoseconds of the datetime") weekofyear = _field_accessor('weekofyear', 'woy', - "\nThe week ordinal of the year\n") + "The week ordinal of the year") week = weekofyear _dayofweek_doc = """ The day of the week with Monday=0, Sunday=6. @@ -1129,12 +1129,12 @@ def date(self): "The name of day in a week (ex: Friday)\n\n.. deprecated:: 0.23.0") dayofyear = _field_accessor('dayofyear', 'doy', - "\nThe ordinal day of the year\n") - quarter = _field_accessor('quarter', 'q', "\nThe quarter of the date\n") + "The ordinal day of the year") + quarter = _field_accessor('quarter', 'q', "The quarter of the date") days_in_month = _field_accessor( 'days_in_month', 'dim', - "\nThe number of days in the month\n") + "The number of days in the month") daysinmonth = days_in_month _is_month_doc = """ Indicates whether the date is the {first_or_last} day of the month. diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index 830283d31a929..4afc9f5483c2a 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -59,7 +59,7 @@ def f(self): return result f.__name__ = name - f.__doc__ = docstring + f.__doc__ = "\n{}\n".format(docstring) return property(f) @@ -684,16 +684,16 @@ def to_pytimedelta(self): return tslibs.ints_to_pytimedelta(self.asi8) days = _field_accessor("days", "days", - "\nNumber of days for each element.\n") + "Number of days for each element.") seconds = _field_accessor("seconds", "seconds", - "\nNumber of seconds (>= 0 and less than 1 day) " - "for each element.\n") + "Number of seconds (>= 0 and less than 1 day) " + "for each element.") microseconds = _field_accessor("microseconds", "microseconds", - "\nNumber of microseconds (>= 0 and less " - "than 1 second) for each element.\n") + "Number of microseconds (>= 0 and less " + "than 1 second) for each element.") nanoseconds = _field_accessor("nanoseconds", "nanoseconds", - "\nNumber of nanoseconds (>= 0 and less " - "than 1 microsecond) for each element.\n") + "Number of nanoseconds (>= 0 and less " + "than 1 microsecond) for each element.") @property def components(self): From cf10d4b87f5d6a083ae69309e538a9341d91dd14 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 7 Aug 2019 22:05:05 +0200 Subject: [PATCH 02/16] Fix issue 18222 --- pandas/plotting/_matplotlib/core.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 519465802085b..9ac258bac5058 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -166,6 +166,15 @@ def __init__( setattr(self, attr, value) self.ax = ax + + # set subplots to True if plt.subplots() uses and assign to ax + from matplotlib.axes import SubplotBase + try: + ax_type = isinstance(self.ax, SubplotBase) + except TypeError: + ax_type = isinstance(self.ax[0], SubplotBase) + if ax_type: + self.subplots = True self.fig = fig self.axes = None From ed2dddc0f0ef72ed91105c266554bf586040900b Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 7 Aug 2019 22:26:53 +0200 Subject: [PATCH 03/16] Add whatsnew note --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index f5ca843e1a6f7..bbd68058d1c9d 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -164,7 +164,7 @@ Plotting ^^^^^^^^ - -- +- Bug in :meth:`DataFrame.plot` producing incorrect legend labels when plotting multiple series on the same axis (:issue:`18222`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ From bf45c4abf4bdae744970fb29dc12dd3f9bba2a4b Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 7 Aug 2019 23:06:26 +0200 Subject: [PATCH 04/16] change code --- pandas/plotting/_matplotlib/core.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 5a50396ee5cf7..0ab35107afee2 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -167,14 +167,6 @@ def __init__( self.ax = ax - # set subplots to True if plt.subplots() uses and assign to ax - from matplotlib.axes import SubplotBase - try: - ax_type = isinstance(self.ax, SubplotBase) - except TypeError: - ax_type = isinstance(self.ax[0], SubplotBase) - if ax_type: - self.subplots = True self.fig = fig self.axes = None @@ -564,6 +556,11 @@ def _make_legend(self): labels = [] title = "" + # set subplots to True if plt.subplots() uses and assign to ax + from matplotlib.axes import SubplotBase + if isinstance(self.ax, SubplotBase): + self.subplots = True + if not self.subplots: if leg is not None: title = leg.get_title().get_text() From 636a345d559403eb90f01a47d6a84485fc87a9f0 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 7 Aug 2019 23:08:48 +0200 Subject: [PATCH 05/16] remove blank line --- pandas/plotting/_matplotlib/core.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 0ab35107afee2..ec7a6e3ea2f0e 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -166,7 +166,6 @@ def __init__( setattr(self, attr, value) self.ax = ax - self.fig = fig self.axes = None From 48654ec852ff4a24bf9041f47a8c3ae3a27c3976 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 7 Aug 2019 23:09:49 +0200 Subject: [PATCH 06/16] Reformat --- pandas/plotting/_matplotlib/core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index ec7a6e3ea2f0e..71900e1bfa009 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -557,6 +557,7 @@ def _make_legend(self): # set subplots to True if plt.subplots() uses and assign to ax from matplotlib.axes import SubplotBase + if isinstance(self.ax, SubplotBase): self.subplots = True From de8faa4579bdfca112917e467832f20adb0e325c Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Wed, 7 Aug 2019 23:11:07 +0200 Subject: [PATCH 07/16] Move import to top of function --- pandas/plotting/_matplotlib/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 71900e1bfa009..7873a2a4114da 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -549,6 +549,8 @@ def _add_legend_handle(self, handle, label, index=None): self.legend_labels.append(label) def _make_legend(self): + from matplotlib.axes import SubplotBase + ax, leg = self._get_ax_legend(self.axes[0]) handles = [] @@ -556,8 +558,6 @@ def _make_legend(self): title = "" # set subplots to True if plt.subplots() uses and assign to ax - from matplotlib.axes import SubplotBase - if isinstance(self.ax, SubplotBase): self.subplots = True From af64b076819fd149d6b971e6b3a75a6495d6a70e Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 8 Aug 2019 11:48:19 +0200 Subject: [PATCH 08/16] try tests --- pandas/plotting/_matplotlib/core.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 7873a2a4114da..eba275459c5a9 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -549,18 +549,12 @@ def _add_legend_handle(self, handle, label, index=None): self.legend_labels.append(label) def _make_legend(self): - from matplotlib.axes import SubplotBase - ax, leg = self._get_ax_legend(self.axes[0]) handles = [] labels = [] title = "" - # set subplots to True if plt.subplots() uses and assign to ax - if isinstance(self.ax, SubplotBase): - self.subplots = True - if not self.subplots: if leg is not None: title = leg.get_title().get_text() @@ -578,7 +572,8 @@ def _make_legend(self): title = self.legend_title if len(handles) > 0: - ax.legend(handles, labels, loc="best", title=title) + ax.legend(loc='best', title=title) + # ax.legend(handles, labels, loc="best", title=title) elif self.subplots and self.legend: for ax in self.axes: From 6e7f5ddc5f808ea4025141b2dc04bc099cc88d74 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 8 Aug 2019 16:32:45 +0200 Subject: [PATCH 09/16] test --- pandas/plotting/_matplotlib/core.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index eba275459c5a9..99404f44e124b 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -551,29 +551,28 @@ def _add_legend_handle(self, handle, label, index=None): def _make_legend(self): ax, leg = self._get_ax_legend(self.axes[0]) - handles = [] - labels = [] title = "" if not self.subplots: + handles, labels = ax.get_legend_handles_labels() + + if self.secondary_y: + handles = self.legend_handles + labels = self.legend_labels + if leg is not None: title = leg.get_title().get_text() - handles = leg.legendHandles - labels = [x.get_text() for x in leg.get_texts()] if self.legend: if self.legend == "reverse": self.legend_handles = reversed(self.legend_handles) self.legend_labels = reversed(self.legend_labels) - handles += self.legend_handles - labels += self.legend_labels if self.legend_title is not None: title = self.legend_title if len(handles) > 0: - ax.legend(loc='best', title=title) - # ax.legend(handles, labels, loc="best", title=title) + ax.legend(handles, labels, loc='best', title=title) elif self.subplots and self.legend: for ax in self.axes: From 5a8481fdad6ddc0859782624d791ed9ae11499b0 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 8 Aug 2019 16:33:13 +0200 Subject: [PATCH 10/16] reformat --- pandas/plotting/_matplotlib/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 99404f44e124b..573e1a81c6aa9 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -572,7 +572,7 @@ def _make_legend(self): title = self.legend_title if len(handles) > 0: - ax.legend(handles, labels, loc='best', title=title) + ax.legend(handles, labels, loc="best", title=title) elif self.subplots and self.legend: for ax in self.axes: From 9c6e7d75b8f18f5c3f6648c99a2225f961988e7c Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 8 Aug 2019 17:01:24 +0200 Subject: [PATCH 11/16] Fix issue --- pandas/plotting/_matplotlib/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 573e1a81c6aa9..052a934b7d4a1 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -556,14 +556,14 @@ def _make_legend(self): if not self.subplots: handles, labels = ax.get_legend_handles_labels() - if self.secondary_y: - handles = self.legend_handles - labels = self.legend_labels - if leg is not None: title = leg.get_title().get_text() if self.legend: + if self.secondary_y: + handles = self.legend_handles + labels = self.legend_labels + if self.legend == "reverse": self.legend_handles = reversed(self.legend_handles) self.legend_labels = reversed(self.legend_labels) @@ -571,8 +571,8 @@ def _make_legend(self): if self.legend_title is not None: title = self.legend_title - if len(handles) > 0: - ax.legend(handles, labels, loc="best", title=title) + if len(handles) > 0: + ax.legend(handles, labels, loc="best", title=title) elif self.subplots and self.legend: for ax in self.axes: From 9734db10eddd54288e8ed72151efdc3ae6906fbb Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Thu, 8 Aug 2019 23:26:55 +0200 Subject: [PATCH 12/16] Fix issue 18222 --- pandas/plotting/_matplotlib/core.py | 24 ++++++++++++++---------- pandas/tests/plotting/common.py | 22 ++++++++++++++++++++++ pandas/tests/plotting/test_frame.py | 25 +++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 052a934b7d4a1..1cecfdf7204bc 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -549,30 +549,31 @@ def _add_legend_handle(self, handle, label, index=None): self.legend_labels.append(label) def _make_legend(self): - ax, leg = self._get_ax_legend(self.axes[0]) + ax, leg, handle = self._get_ax_legend(self.axes[0]) + handles = [] + labels = [] title = "" if not self.subplots: - handles, labels = ax.get_legend_handles_labels() - if leg is not None: title = leg.get_title().get_text() + handles.extend(handle) + labels = [x.get_text() for x in leg.get_texts()] if self.legend: - if self.secondary_y: - handles = self.legend_handles - labels = self.legend_labels - if self.legend == "reverse": self.legend_handles = reversed(self.legend_handles) self.legend_labels = reversed(self.legend_labels) + handles += self.legend_handles + labels += self.legend_labels + if self.legend_title is not None: title = self.legend_title - if len(handles) > 0: - ax.legend(handles, labels, loc="best", title=title) + if len(handles) > 0: + ax.legend(handles, labels, loc="best", title=title) elif self.subplots and self.legend: for ax in self.axes: @@ -581,6 +582,9 @@ def _make_legend(self): def _get_ax_legend(self, ax): leg = ax.get_legend() + + # add handles is because leg.LegendHandler misses marker information + handles, labels = ax.get_legend_handles_labels() other_ax = getattr(ax, "left_ax", None) or getattr(ax, "right_ax", None) other_leg = None if other_ax is not None: @@ -588,7 +592,7 @@ def _get_ax_legend(self, ax): if leg is None and other_leg is not None: leg = other_leg ax = other_ax - return ax, leg + return ax, leg, handles @cache_readonly def plt(self): diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index 4929422d20e8a..5a591f72d7361 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -103,6 +103,28 @@ def _check_legend_labels(self, axes, labels=None, visible=True): else: assert ax.get_legend() is None + def _check_legend_marker(self, ax, expected_markers=None, visible=True): + """ + Check ax has expected legend markers + + Parameters + ---------- + ax : matplotlib Axes object + expected_markers : list-like + expected legend markers + visible : bool + expected legend visibility. labels are checked only when visible is + True + """ + if visible and (expected_markers is None): + raise ValueError("Markers must be specified when visible is True") + if visible: + handles, _ = ax.get_legend_handles_labels() + markers = [handle.get_marker() for handle in handles] + assert markers == expected_markers + else: + assert ax.get_legend() is None + def _check_data(self, xp, rs): """ Check each axes has identical lines diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 65815bcedebfc..7fdc0252b71e3 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -1881,6 +1881,31 @@ def test_df_legend_labels(self): self._check_legend_labels(ax, labels=["LABEL_b", "LABEL_c"]) assert df5.columns.tolist() == ["b", "c"] + def test_missing_marker_multi_plots_on_same_ax(self): + # GH 18222 + df = pd.DataFrame( + data=[[1, 1, 1, 1], [2, 2, 4, 8]], columns=["x", "r", "g", "b"] + ) + fig, ax = self.plt.subplots(nrows=1, ncols=3) + # Left plot + df.plot(x="x", y="r", linewidth=0, marker="o", color="r", ax=ax[0]) + df.plot(x="x", y="g", linewidth=1, marker="x", color="g", ax=ax[0]) + df.plot(x="x", y="b", linewidth=1, marker="o", color="b", ax=ax[0]) + self._check_legend_labels(ax[0], labels=["r", "g", "b"]) + self._check_legend_marker(ax[0], expected_markers=["o", "x", "o"]) + # Center plot + df.plot(x="x", y="b", linewidth=1, marker="o", color="b", ax=ax[1]) + df.plot(x="x", y="r", linewidth=0, marker="o", color="r", ax=ax[1]) + df.plot(x="x", y="g", linewidth=1, marker="x", color="g", ax=ax[1]) + self._check_legend_labels(ax[1], labels=["b", "r", "g"]) + self._check_legend_marker(ax[1], expected_markers=["o", "o", "x"]) + # Right plot + df.plot(x="x", y="g", linewidth=1, marker="x", color="g", ax=ax[2]) + df.plot(x="x", y="b", linewidth=1, marker="o", color="b", ax=ax[2]) + df.plot(x="x", y="r", linewidth=0, marker="o", color="r", ax=ax[2]) + self._check_legend_labels(ax[2], labels=["g", "b", "r"]) + self._check_legend_marker(ax[2], expected_markers=["x", "o", "o"]) + def test_legend_name(self): multi = DataFrame( randn(4, 4), From ec76abe9d6708b9a3b416432f487299d9a9a53b0 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 9 Aug 2019 09:09:39 +0200 Subject: [PATCH 13/16] Make code prettier --- pandas/plotting/_matplotlib/core.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 1cecfdf7204bc..2d70b108bb96e 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -549,7 +549,7 @@ def _add_legend_handle(self, handle, label, index=None): self.legend_labels.append(label) def _make_legend(self): - ax, leg, handle = self._get_ax_legend(self.axes[0]) + ax, leg = self._get_ax_legend(self.axes[0]) handles = [] labels = [] @@ -558,7 +558,9 @@ def _make_legend(self): if not self.subplots: if leg is not None: title = leg.get_title().get_text() - handles.extend(handle) + + # replace handles is because leg.LegendHandler misses marker information + handles, _ = ax.get_legend_handles_labels() labels = [x.get_text() for x in leg.get_texts()] if self.legend: @@ -582,9 +584,6 @@ def _make_legend(self): def _get_ax_legend(self, ax): leg = ax.get_legend() - - # add handles is because leg.LegendHandler misses marker information - handles, labels = ax.get_legend_handles_labels() other_ax = getattr(ax, "left_ax", None) or getattr(ax, "right_ax", None) other_leg = None if other_ax is not None: @@ -592,7 +591,7 @@ def _get_ax_legend(self, ax): if leg is None and other_leg is not None: leg = other_leg ax = other_ax - return ax, leg, handles + return ax, leg @cache_readonly def plt(self): From d437f9455e9a5c84c015e78fbf5574f71937d434 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 9 Aug 2019 09:38:33 +0200 Subject: [PATCH 14/16] Fix test --- pandas/plotting/_matplotlib/core.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 2d70b108bb96e..5b4d6f33de772 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -549,7 +549,7 @@ def _add_legend_handle(self, handle, label, index=None): self.legend_labels.append(label) def _make_legend(self): - ax, leg = self._get_ax_legend(self.axes[0]) + ax, leg, handle = self._get_ax_legend(self.axes[0]) handles = [] labels = [] @@ -558,9 +558,8 @@ def _make_legend(self): if not self.subplots: if leg is not None: title = leg.get_title().get_text() - - # replace handles is because leg.LegendHandler misses marker information - handles, _ = ax.get_legend_handles_labels() + # Replace leg.LegendHandles because it misses marker info + handles.extend(handle) labels = [x.get_text() for x in leg.get_texts()] if self.legend: @@ -584,6 +583,9 @@ def _make_legend(self): def _get_ax_legend(self, ax): leg = ax.get_legend() + + # Get handle from axes + handle, _ = ax.get_legend_handles_labels() other_ax = getattr(ax, "left_ax", None) or getattr(ax, "right_ax", None) other_leg = None if other_ax is not None: @@ -591,7 +593,7 @@ def _get_ax_legend(self, ax): if leg is None and other_leg is not None: leg = other_leg ax = other_ax - return ax, leg + return ax, leg, handle @cache_readonly def plt(self): From fc7b9c4951a001f4b03c79db591c131656373926 Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Fri, 9 Aug 2019 09:41:23 +0200 Subject: [PATCH 15/16] Add docstring --- pandas/plotting/_matplotlib/core.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 5b4d6f33de772..354b35ce2e7be 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -549,7 +549,7 @@ def _add_legend_handle(self, handle, label, index=None): self.legend_labels.append(label) def _make_legend(self): - ax, leg, handle = self._get_ax_legend(self.axes[0]) + ax, leg, handle = self._get_ax_legend_handle(self.axes[0]) handles = [] labels = [] @@ -581,7 +581,10 @@ def _make_legend(self): if ax.get_visible(): ax.legend(loc="best") - def _get_ax_legend(self, ax): + def _get_ax_legend_handle(self, ax): + """ + Take in axes and return ax, legend and handle under different scenarios + """ leg = ax.get_legend() # Get handle from axes From 7a4a804ff6b12d20c8516d1f3661b038677850ee Mon Sep 17 00:00:00 2001 From: Kaiqi Date: Tue, 13 Aug 2019 20:27:22 +0200 Subject: [PATCH 16/16] Walk around flaky test --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 6ed19493af775..82bba1ae22bf2 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -165,7 +165,7 @@ Plotting - Bug in :meth:`Series.plot` not able to plot boolean values (:issue:`23719`) - -- Bug in :meth:`DataFrame.plot` producing incorrect legend labels when plotting multiple series on the same axis (:issue:`18222`) +- Bug in :meth:`DataFrame.plot` producing incorrect legend markers when plotting multiple series on the same axis (:issue:`18222`) - Bug in :meth:`DataFrame.plot` when ``kind='box'`` and data contains datetime or timedelta data. These types are now automatically dropped (:issue:`22799`) Groupby/resample/rolling