diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index 087c193d48c3..230798bfe090 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -1626,7 +1626,8 @@ def _gci(self): # If there is no image in the current Axes, search for # one in a previously created Axes. Whether this makes # sense is debatable, but it is the documented behavior. - for ax in reversed(self.axes): + axes = self.axes + for ax in reversed(axes): im = ax._gci() if im is not None: return im diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 2376c6243929..090330044c43 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -1067,8 +1067,7 @@ def gcf() -> Figure: manager = _pylab_helpers.Gcf.get_active() if manager is not None: return manager.canvas.figure - else: - return figure() + return figure() def fignum_exists(num: int | str) -> bool: