From 4e8e786d852aca8b2c1dd4afe9d9526e5cc735f1 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 17 Jun 2021 17:52:53 +0100 Subject: [PATCH] Set the axes of legend text --- lib/matplotlib/legend.py | 1 + lib/matplotlib/tests/test_legend.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 7d4b4771ddb8..94cc1d806657 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -828,6 +828,7 @@ def _init_legend_box(self, handles, labels, markerfirst=True): children=[self._legend_title_box, self._legend_handle_box]) self._legend_box.set_figure(self.figure) + self._legend_box.axes = self.axes self.texts = text_list self.legendHandles = handle_list diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index bae031f83750..48172cbd9b24 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -852,3 +852,12 @@ def test_legend_face_edgecolor(): ax.fill_between([0, 1, 2], [1, 2, 3], [2, 3, 4], facecolor='r', edgecolor='face', label='Fill') ax.legend() + + +def test_legend_text_axes(): + fig, ax = plt.subplots() + ax.plot([1, 2], [3, 4], label='line') + leg = ax.legend() + + assert leg.axes is ax + assert leg.get_texts()[0].axes is ax