From bfacfd8cc472d26227fe131556dda49e4ba589a9 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 26 Feb 2021 11:45:26 +0000 Subject: [PATCH 1/5] wip --- pandas/plotting/_matplotlib/core.py | 21 +++++++++++++++------ pandas/plotting/_matplotlib/hist.py | 3 ++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 3b0d59501ba05..6008301cb4d12 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -579,11 +579,18 @@ def legend_title(self) -> Optional[str]: stringified = map(pprint_thing, self.data.columns.names) return ",".join(stringified) - def _add_legend_handle(self, handle, label, index=None): + def _mark_right_label( + self, label: Optional[str], index: Optional[int] + ) -> Optional[str]: + if not self.subplots: # (right) is only attached when subplots=False + if label is not None: + if self.mark_right and index is not None: + if self.on_right(index): + label = label + " (right)" + return label + + def _add_legend_handle(self, handle: Artist, label: str) -> None: if label is not None: - if self.mark_right and index is not None: - if self.on_right(index): - label = label + " (right)" self.legend_handles.append(handle) self.legend_labels.append(label) @@ -1174,6 +1181,7 @@ def _make_plot(self): kwds = dict(kwds, **errors) label = pprint_thing(label) # .encode('utf-8') + label = self._mark_right_label(label, index=i) kwds["label"] = label newlines = plotf( @@ -1186,7 +1194,7 @@ def _make_plot(self): is_errorbar=is_errorbar, **kwds, ) - self._add_legend_handle(newlines[0], label, index=i) + self._add_legend_handle(newlines[0], label) if self._is_ts_plot(): @@ -1462,6 +1470,7 @@ def _make_plot(self): kwds = dict(kwds, **errors) label = pprint_thing(label) + label = self._mark_right_label(label, index=i) if (("yerr" in kwds) or ("xerr" in kwds)) and (kwds.get("ecolor") is None): kwds["ecolor"] = mpl.rcParams["xtick.color"] @@ -1512,7 +1521,7 @@ def _make_plot(self): log=self.log, **kwds, ) - self._add_legend_handle(rect, label, index=i) + self._add_legend_handle(rect, label) def _post_plot_logic(self, ax: Axes, data): if self.use_index: diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py index 3de467c77d289..9934f8f04aa53 100644 --- a/pandas/plotting/_matplotlib/hist.py +++ b/pandas/plotting/_matplotlib/hist.py @@ -89,6 +89,7 @@ def _make_plot(self): kwds = self.kwds.copy() label = pprint_thing(label) + label = self._mark_right_label(label, index=i) kwds["label"] = label style, kwds = self._apply_style_colors(colors, kwds, i, label) @@ -105,7 +106,7 @@ def _make_plot(self): kwds["weights"] = weights[:, i] artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds) - self._add_legend_handle(artists[0], label, index=i) + self._add_legend_handle(artists[0], label) def _make_plot_keywords(self, kwds, y): """merge BoxPlot/KdePlot properties to passed kwds""" From 1c66910c339d88553eaf11f3edbae52c94f128d2 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 10 Mar 2021 11:30:02 +0000 Subject: [PATCH 2/5] add docstring, simplify --- pandas/plotting/_matplotlib/core.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index ee3fea32b7fc9..d3207d07d20b5 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -579,14 +579,26 @@ def legend_title(self) -> Optional[str]: stringified = map(pprint_thing, self.data.columns.names) return ",".join(stringified) - def _mark_right_label( - self, label: Optional[str], index: Optional[int] - ) -> Optional[str]: - if not self.subplots: # (right) is only attached when subplots=False - if label is not None: - if self.mark_right and index is not None: - if self.on_right(index): - label = label + " (right)" + def _mark_right_label(self, label: str, index: int) -> str: + """ + Append ``(right)`` to the label of a line if it's plotted on the right axis. + + Note that ``(right)`` is only appended when ``subplots=False``. + + Parameters + ---------- + label: str + Label of line being plotted. + index: int + Index of line within plotted data. + + Returns + ------- + str + Updated label. + """ + if not self.subplots and self.mark_right and self.on_right(index): + label += " (right)" return label def _add_legend_handle(self, handle: Artist, label: str) -> None: From 981f3854e9188f9c5ac9d51516b8f8457ead0b32 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 10 Mar 2021 11:38:21 +0000 Subject: [PATCH 3/5] remove redundant null check in _add_legend_handles --- pandas/plotting/_matplotlib/core.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index d3207d07d20b5..8b00c1ef36bd2 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -602,9 +602,8 @@ def _mark_right_label(self, label: str, index: int) -> str: return label def _add_legend_handle(self, handle: Artist, label: str) -> None: - if label is not None: - self.legend_handles.append(handle) - self.legend_labels.append(label) + self.legend_handles.append(handle) + self.legend_labels.append(label) def _make_legend(self): ax, leg, handle = self._get_ax_legend_handle(self.axes[0]) From ef1c733fd4e1f5fb11996ed3290822b837c54076 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 10 Mar 2021 11:40:43 +0000 Subject: [PATCH 4/5] shorten dosctrings (these are only internal) --- pandas/plotting/_matplotlib/core.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 8b00c1ef36bd2..d210db18eb129 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -584,24 +584,17 @@ def _mark_right_label(self, label: str, index: int) -> str: Append ``(right)`` to the label of a line if it's plotted on the right axis. Note that ``(right)`` is only appended when ``subplots=False``. - - Parameters - ---------- - label: str - Label of line being plotted. - index: int - Index of line within plotted data. - - Returns - ------- - str - Updated label. """ if not self.subplots and self.mark_right and self.on_right(index): label += " (right)" return label def _add_legend_handle(self, handle: Artist, label: str) -> None: + """ + Append current handle and label to ``legend_handles`` and ``legend_labels``. + + These will be used to make the legend. + """ self.legend_handles.append(handle) self.legend_labels.append(label) From f6589df780694f10a072448db8eb0ccbf97ebdd3 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Tue, 23 Mar 2021 20:40:33 +0000 Subject: [PATCH 5/5] :truck: _add_legend_handle -> _append_legend_handles_labels --- pandas/plotting/_matplotlib/core.py | 10 +++++----- pandas/plotting/_matplotlib/hist.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index d210db18eb129..37d9ed45d4050 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -589,7 +589,7 @@ def _mark_right_label(self, label: str, index: int) -> str: label += " (right)" return label - def _add_legend_handle(self, handle: Artist, label: str) -> None: + def _append_legend_handles_labels(self, handle: Artist, label: str) -> None: """ Append current handle and label to ``legend_handles`` and ``legend_labels``. @@ -1089,7 +1089,7 @@ def _make_plot(self): cbar.ax.set_yticklabels(self.data[c].cat.categories) if label is not None: - self._add_legend_handle(scatter, label) + self._append_legend_handles_labels(scatter, label) else: self.legend = False @@ -1194,7 +1194,7 @@ def _make_plot(self): is_errorbar=is_errorbar, **kwds, ) - self._add_legend_handle(newlines[0], label) + self._append_legend_handles_labels(newlines[0], label) if self._is_ts_plot(): @@ -1521,7 +1521,7 @@ def _make_plot(self): log=self.log, **kwds, ) - self._add_legend_handle(rect, label) + self._append_legend_handles_labels(rect, label) def _post_plot_logic(self, ax: Axes, data): if self.use_index: @@ -1633,4 +1633,4 @@ def blank_labeler(label, value): # leglabels is used for legend labels leglabels = labels if labels is not None else idx for p, l in zip(patches, leglabels): - self._add_legend_handle(p, l) + self._append_legend_handles_labels(p, l) diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py index 9934f8f04aa53..a02d9a2b9dc8d 100644 --- a/pandas/plotting/_matplotlib/hist.py +++ b/pandas/plotting/_matplotlib/hist.py @@ -106,7 +106,7 @@ def _make_plot(self): kwds["weights"] = weights[:, i] artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds) - self._add_legend_handle(artists[0], label) + self._append_legend_handles_labels(artists[0], label) def _make_plot_keywords(self, kwds, y): """merge BoxPlot/KdePlot properties to passed kwds"""