Skip to content

Figure.clear() leaves stale subplot state (subplotgrid returns dead axes) #756

Description

@cvanelteren

Pre-existing bug — reproduces identically on main, not caused by #698. Filing it because #698 is what gives this state a named owner (SubplotManager), which makes it the natural place to fix.

Repro

import ultraplot as uplt
fig, axs = uplt.subplots(nrows=1, ncols=2)
fig.clear()

print(fig.axes)                          # []          <- matplotlib cleaned up
print(list(fig._subplots.subplot_dict))  # [1, 2]      <- stale, dead axes
print(len(fig.subplotgrid))              # 2           <- hands them back to the user
print(fig.gridspec)                      # GridSpec(nrows=1, ncols=2)  <- stale
print(fig._subplots.counter)             # 2           <- never reset

Figure.clear() goes through matplotlib and empties fig.axes, but nothing resets the subplot bookkeeping. So after a clear():

  • fig.subplotgrid returns axes that are no longer in the figure,
  • fig._get_subplot(n) resolves to a dead axes,
  • fig.gridspec still points at the old gridspec,
  • the label counter keeps climbing.

(On main the same thing happens via fig._subplot_dict / fig._gridspec / fig._subplot_counter — this is a faithful description of long-standing behaviour, not something the refactor introduced.)

Suggested fix

Give SubplotManager a reset() that clears subplot_dict, zeroes counter, and drops _gridspec, and have Figure.clear() call it. Add a regression test — there is currently no test covering figure state after clear(), which is why this survived.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions