Skip to content

Commit

Permalink
REF, TYP: factor out _mark_right from _add_legend_handle (pandas-dev#…
Browse files Browse the repository at this point in the history
…40078)

* wip

* add docstring, simplify

* remove redundant null check in _add_legend_handles

* shorten dosctrings (these are only internal)

* 🚚 _add_legend_handle -> _append_legend_handles_labels
  • Loading branch information
MarcoGorelli authored and JulianWgs committed Jul 3, 2021
1 parent a9a3482 commit 5860895
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
35 changes: 24 additions & 11 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,24 @@ 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):
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)
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``.
"""
if not self.subplots and self.mark_right and self.on_right(index):
label += " (right)"
return label

def _append_legend_handles_labels(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)

def _make_legend(self):
ax, leg, handle = self._get_ax_legend_handle(self.axes[0])
Expand Down Expand Up @@ -1078,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

Expand Down Expand Up @@ -1170,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(
Expand All @@ -1182,7 +1194,7 @@ def _make_plot(self):
is_errorbar=is_errorbar,
**kwds,
)
self._add_legend_handle(newlines[0], label, index=i)
self._append_legend_handles_labels(newlines[0], label)

if self._is_ts_plot():

Expand Down Expand Up @@ -1458,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"]
Expand Down Expand Up @@ -1508,7 +1521,7 @@ def _make_plot(self):
log=self.log,
**kwds,
)
self._add_legend_handle(rect, label, index=i)
self._append_legend_handles_labels(rect, label)

def _post_plot_logic(self, ax: Axes, data):
if self.use_index:
Expand Down Expand Up @@ -1620,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)
3 changes: 2 additions & 1 deletion pandas/plotting/_matplotlib/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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._append_legend_handles_labels(artists[0], label)

def _make_plot_keywords(self, kwds, y):
"""merge BoxPlot/KdePlot properties to passed kwds"""
Expand Down

0 comments on commit 5860895

Please sign in to comment.