Skip to content

Commit bc54a3f

Browse files
mslapekdongjoon-hyun
authored andcommitted
[SPARK-37730][PYTHON] Replace use of MPLPlot._add_legend_handle with MPLPlot._append_legend_handles_labels
### What changes were proposed in this pull request? Replace use of MPLPlot._add_legend_handle (removed in pandas) with MPLPlot._append_legend_handles_labels in histogram and KDE plots. Based on: pandas-dev/pandas@029907c ### Why are the changes needed? Fix of SPARK-37730. plot.hist and plot.kde don't throw AttributeError for pandas=1.3.5. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? ~~Tested with existing plot test on CI (for older pandas only).~~ (it seems that CI doesn't run matplotlib tests, see #35000 (comment)) I've run tests on a local computer, see #35000 (comment) : ``` $ python python/pyspark/pandas/tests/plot/test_series_plot_matplotlib.py ``` :question: **QUESTION:** Maybe add plot testing for pandas 1.3.5 on CI? (I've noticed that CI uses `pandas=1.3.4`, maybe update it to `1.3.5`?) Closes #35000 from mslapek/fixpythonplot. Authored-by: Michał Słapek <[email protected]> Signed-off-by: Hyukjin Kwon <[email protected]> (cherry picked from commit 371e307) Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent c5983c1 commit bc54a3f

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

python/pyspark/pandas/plot/matplotlib.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ def _make_plot(self):
392392
kwds = self.kwds.copy()
393393

394394
label = pprint_thing(label if len(label) > 1 else label[0])
395+
# `if hasattr(...)` makes plotting compatible with pandas < 1.3, see pandas-dev/pandas#40078.
396+
label = (
397+
self._mark_right_label(label, index=i)
398+
if hasattr(self, "_mark_right_label")
399+
else label
400+
)
395401
kwds["label"] = label
396402

397403
style, kwds = self._apply_style_colors(colors, kwds, i, label)
@@ -400,7 +406,10 @@ def _make_plot(self):
400406

401407
kwds = self._make_plot_keywords(kwds, y)
402408
artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds)
403-
self._add_legend_handle(artists[0], label, index=i)
409+
# `if hasattr(...)` makes plotting compatible with pandas < 1.3, see pandas-dev/pandas#40078.
410+
self._append_legend_handles_labels(artists[0], label) if hasattr(
411+
self, "_append_legend_handles_labels"
412+
) else self._add_legend_handle(artists[0], label, index=i)
404413

405414
@classmethod
406415
def _plot(cls, ax, y, style=None, bins=None, bottom=0, column_num=0, stacking_id=None, **kwds):
@@ -483,6 +492,12 @@ def _make_plot(self):
483492
kwds = self.kwds.copy()
484493

485494
label = pprint_thing(label if len(label) > 1 else label[0])
495+
# `if hasattr(...)` makes plotting compatible with pandas < 1.3, see pandas-dev/pandas#40078.
496+
label = (
497+
self._mark_right_label(label, index=i)
498+
if hasattr(self, "_mark_right_label")
499+
else label
500+
)
486501
kwds["label"] = label
487502

488503
style, kwds = self._apply_style_colors(colors, kwds, i, label)
@@ -491,7 +506,10 @@ def _make_plot(self):
491506

492507
kwds = self._make_plot_keywords(kwds, y)
493508
artists = self._plot(ax, y, column_num=i, stacking_id=stacking_id, **kwds)
494-
self._add_legend_handle(artists[0], label, index=i)
509+
# `if hasattr(...)` makes plotting compatible with pandas < 1.3, see pandas-dev/pandas#40078.
510+
self._append_legend_handles_labels(artists[0], label) if hasattr(
511+
self, "_append_legend_handles_labels"
512+
) else self._add_legend_handle(artists[0], label, index=i)
495513

496514
def _get_ind(self, y):
497515
return KdePlotBase.get_ind(y, self.ind)

0 commit comments

Comments
 (0)