Skip to content

Commit 4b961b2

Browse files
committed
factor out mark_right
1 parent 3c13004 commit 4b961b2

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

pandas/plotting/_matplotlib/core.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,17 @@ 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):
582+
def _mark_right_label(
583+
self, label: Optional[str], index: Optional[int]
584+
) -> Optional[str]:
583585
if label is not None:
584586
if self.mark_right and index is not None:
585587
if self.on_right(index):
586588
label = label + " (right)"
589+
return label
590+
591+
def _add_legend_handle(self, handle: Artist, label: str) -> None:
592+
if label is not None:
587593
self.legend_handles.append(handle)
588594
self.legend_labels.append(label)
589595

@@ -1065,7 +1071,6 @@ def _make_plot(self):
10651071
label = self.label
10661072
else:
10671073
label = None
1068-
10691074
scatter = ax.scatter(
10701075
data[x].values,
10711076
data[y].values,
@@ -1175,6 +1180,7 @@ def _make_plot(self):
11751180
kwds = dict(kwds, **errors)
11761181

11771182
label = pprint_thing(label) # .encode('utf-8')
1183+
label = self._mark_right_label(label, i)
11781184
kwds["label"] = label
11791185

11801186
newlines = plotf(
@@ -1187,8 +1193,7 @@ def _make_plot(self):
11871193
is_errorbar=is_errorbar,
11881194
**kwds,
11891195
)
1190-
if label is not None:
1191-
self._add_legend_handle(newlines[0], label, index=i)
1196+
self._add_legend_handle(newlines[0], label)
11921197

11931198
if self._is_ts_plot():
11941199

@@ -1464,11 +1469,7 @@ def _make_plot(self):
14641469
kwds = dict(kwds, **errors)
14651470

14661471
label = pprint_thing(label)
1467-
1468-
if label is not None:
1469-
if self.mark_right and i is not None:
1470-
if self.on_right(i):
1471-
label = label + " (right)"
1472+
label = self._mark_right_label(label, i)
14721473

14731474
if (("yerr" in kwds) or ("xerr" in kwds)) and (kwds.get("ecolor") is None):
14741475
kwds["ecolor"] = mpl.rcParams["xtick.color"]
@@ -1519,8 +1520,7 @@ def _make_plot(self):
15191520
log=self.log,
15201521
**kwds,
15211522
)
1522-
if label is not None:
1523-
self._add_legend_handle(rect, label, index=i)
1523+
self._add_legend_handle(rect, label)
15241524

15251525
def _post_plot_logic(self, ax: Axes, data):
15261526
if self.use_index:

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, 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._add_legend_handle(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)