Skip to content

Commit 5860895

Browse files
MarcoGorelliJulianWgs
authored andcommitted
REF, TYP: factor out _mark_right from _add_legend_handle (pandas-dev#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
1 parent a9a3482 commit 5860895

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

pandas/plotting/_matplotlib/core.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,24 @@ def legend_title(self) -> Optional[str]:
579579
stringified = map(pprint_thing, self.data.columns.names)
580580
return ",".join(stringified)
581581

582-
def _add_legend_handle(self, handle, label, index=None):
583-
if label is not None:
584-
if self.mark_right and index is not None:
585-
if self.on_right(index):
586-
label = label + " (right)"
587-
self.legend_handles.append(handle)
588-
self.legend_labels.append(label)
582+
def _mark_right_label(self, label: str, index: int) -> str:
583+
"""
584+
Append ``(right)`` to the label of a line if it's plotted on the right axis.
585+
586+
Note that ``(right)`` is only appended when ``subplots=False``.
587+
"""
588+
if not self.subplots and self.mark_right and self.on_right(index):
589+
label += " (right)"
590+
return label
591+
592+
def _append_legend_handles_labels(self, handle: Artist, label: str) -> None:
593+
"""
594+
Append current handle and label to ``legend_handles`` and ``legend_labels``.
595+
596+
These will be used to make the legend.
597+
"""
598+
self.legend_handles.append(handle)
599+
self.legend_labels.append(label)
589600

590601
def _make_legend(self):
591602
ax, leg, handle = self._get_ax_legend_handle(self.axes[0])
@@ -1078,7 +1089,7 @@ def _make_plot(self):
10781089
cbar.ax.set_yticklabels(self.data[c].cat.categories)
10791090

10801091
if label is not None:
1081-
self._add_legend_handle(scatter, label)
1092+
self._append_legend_handles_labels(scatter, label)
10821093
else:
10831094
self.legend = False
10841095

@@ -1170,6 +1181,7 @@ def _make_plot(self):
11701181
kwds = dict(kwds, **errors)
11711182

11721183
label = pprint_thing(label) # .encode('utf-8')
1184+
label = self._mark_right_label(label, index=i)
11731185
kwds["label"] = label
11741186

11751187
newlines = plotf(
@@ -1182,7 +1194,7 @@ def _make_plot(self):
11821194
is_errorbar=is_errorbar,
11831195
**kwds,
11841196
)
1185-
self._add_legend_handle(newlines[0], label, index=i)
1197+
self._append_legend_handles_labels(newlines[0], label)
11861198

11871199
if self._is_ts_plot():
11881200

@@ -1458,6 +1470,7 @@ def _make_plot(self):
14581470
kwds = dict(kwds, **errors)
14591471

14601472
label = pprint_thing(label)
1473+
label = self._mark_right_label(label, index=i)
14611474

14621475
if (("yerr" in kwds) or ("xerr" in kwds)) and (kwds.get("ecolor") is None):
14631476
kwds["ecolor"] = mpl.rcParams["xtick.color"]
@@ -1508,7 +1521,7 @@ def _make_plot(self):
15081521
log=self.log,
15091522
**kwds,
15101523
)
1511-
self._add_legend_handle(rect, label, index=i)
1524+
self._append_legend_handles_labels(rect, label)
15121525

15131526
def _post_plot_logic(self, ax: Axes, data):
15141527
if self.use_index:
@@ -1620,4 +1633,4 @@ def blank_labeler(label, value):
16201633
# leglabels is used for legend labels
16211634
leglabels = labels if labels is not None else idx
16221635
for p, l in zip(patches, leglabels):
1623-
self._add_legend_handle(p, l)
1636+
self._append_legend_handles_labels(p, l)

pandas/plotting/_matplotlib/hist.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def _make_plot(self):
8989
kwds = self.kwds.copy()
9090

9191
label = pprint_thing(label)
92+
label = self._mark_right_label(label, index=i)
9293
kwds["label"] = label
9394

9495
style, kwds = self._apply_style_colors(colors, kwds, i, label)
@@ -105,7 +106,7 @@ def _make_plot(self):
105106
kwds["weights"] = weights[:, i]
106107

107108
artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds)
108-
self._add_legend_handle(artists[0], label, index=i)
109+
self._append_legend_handles_labels(artists[0], label)
109110

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

0 commit comments

Comments
 (0)