From 5cf1fa0fac2f4d0dfdd6aed3eb9f0a636d852d78 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 22 May 2020 00:17:25 +0200 Subject: [PATCH] Regenerate background when RectangleSelector active-flag is set back on. Otherwise, if the selector is made inactive and then back to active, the "old" selector gets captured in the background image and spurious appears. No tests because good luck with that, but can be tried with examples/widgets/rectangle_selector.py. --- lib/matplotlib/widgets.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 37e56a14de59..8336d6353b62 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -1456,8 +1456,21 @@ def update_background(self, event): """Force an update of the background.""" # If you add a call to `ignore` here, you'll want to check edge case: # `release` can call a draw event even when `ignore` is True. - if self.useblit: + if not self.useblit: + return + # Make sure that widget artists don't get accidentally included in the + # background, by re-rendering the background if needed (and then + # re-re-rendering the canvas with the visible widget artists). + needs_redraw = any(artist.get_visible() for artist in self.artists) + with ExitStack() as stack: + if needs_redraw: + for artist in self.artists: + stack.callback(artist.set_visible, artist.get_visible()) + artist.set_visible(False) + self.canvas.draw() self.background = self.canvas.copy_from_bbox(self.ax.bbox) + if needs_redraw: + self.update() def connect_default_events(self): """Connect the major canvas events to methods."""