Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Cleanup _pylab_helpers.#13581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Cleanup _pylab_helpers.#13581
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,41 @@ | ||
""" | ||
Manage figures forthepyplot interface. | ||
""" | ||
import atexit | ||
from collections import OrderedDict | ||
import gc | ||
class Gcf: | ||
""" | ||
Singleton to maintain the relation between figures and their managers, and | ||
keep track of and "active" figure and manager. | ||
The canvas of a figure created through pyplot is associated with a figure | ||
manager, which handles the interaction between the figure and the backend. | ||
pyplot keeps track of figure managers using an identifier, the "figure | ||
number" or "manager number" (which can actually be any hashable value); | ||
this number is available as the :attr:`number` attribute of the manager. | ||
This class is never instantiated; it consists of an `OrderedDict` mapping | ||
figure/manager numbers to managers, and a set of class methods that | ||
manipulate this `OrderedDict`. | ||
Attributes | ||
---------- | ||
figs : OrderedDict | ||
`OrderedDict` mapping numbers to managers; the active manager is at the | ||
end. | ||
""" | ||
figs =OrderedDict() | ||
@classmethod | ||
def get_fig_manager(cls, num): | ||
""" | ||
If managernumber*num* exists, make it the active one and return it; | ||
otherwise return *None*. | ||
""" | ||
manager = cls.figs.get(num, None) | ||
if manager is not None: | ||
@@ -40,90 +45,73 @@ def get_fig_manager(cls, num): | ||
@classmethod | ||
def destroy(cls, num): | ||
""" | ||
Destroyfigure number *num*. | ||
In the interactive backends, this is bound to the window "destroy" and | ||
"delete" events. | ||
""" | ||
if not cls.has_fignum(num): | ||
return | ||
manager = cls.figs.pop(num) | ||
manager.canvas.mpl_disconnect(manager._cidgcf) | ||
manager.destroy() | ||
gc.collect(1) | ||
@classmethod | ||
def destroy_fig(cls, fig): | ||
"""Destroy figure*fig*.""" | ||
canvas =getattr(fig, "canvas", None) | ||
manager = getattr(canvas, "manager", None) | ||
num= getattr(manager, "num",None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is this always equivalent, since it seems to be working in reverse? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This basically gets | ||
cls.destroy(num) | ||
@classmethod | ||
def destroy_all(cls): | ||
"""Destroy all figures.""" | ||
# Reimport gc in case the module globals have already been removed | ||
# during interpreter shutdown. | ||
import gc | ||
for manager in list(cls.figs.values()): | ||
manager.canvas.mpl_disconnect(manager._cidgcf) | ||
manager.destroy() | ||
cls.figs.clear() | ||
gc.collect(1) | ||
@classmethod | ||
def has_fignum(cls, num): | ||
"""Return whether figure number *num* exists.""" | ||
return num in cls.figs | ||
@classmethod | ||
def get_all_fig_managers(cls): | ||
"""Return a list of figure managers.""" | ||
return list(cls.figs.values()) | ||
@classmethod | ||
def get_num_fig_managers(cls): | ||
"""Return the number of figures being managed.""" | ||
return len(cls.figs) | ||
@classmethod | ||
def get_active(cls): | ||
"""Return the active manager, or *None* if there is no manager.""" | ||
return next(reversed(cls.figs.values())) if cls.figs else None | ||
@classmethod | ||
def set_active(cls, manager): | ||
"""Make *manager* the active manager.""" | ||
cls.figs[manager.num] = manager | ||
cls.figs.move_to_end(manager.num) | ||
@classmethod | ||
def draw_all(cls, force=False): | ||
""" | ||
Redraw allstale managedfigures, or, if *force* is True, all managed | ||
figures. | ||
""" | ||
for manager in cls.get_all_fig_managers(): | ||
if force or manager.canvas.figure.stale: | ||
manager.canvas.draw_idle() | ||
atexit.register(Gcf.destroy_all) |