Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit578efde

Browse files
committed
Cleanup _pylab_helpers.
_activeQue + figs can be combined into a single OrderedDict.
1 parent684a1ea commit578efde

File tree

1 file changed

+51
-64
lines changed

1 file changed

+51
-64
lines changed

‎lib/matplotlib/_pylab_helpers.py

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
"""
2-
Manage figures for pyplot interface.
2+
Manage figures forthepyplot interface.
33
"""
44

55
importatexit
6+
fromcollectionsimportOrderedDict
67
importgc
78

89

9-
classGcf(object):
10+
classGcf:
1011
"""
11-
Singleton to manage a set of integer-numbered figures.
12-
13-
This class is never instantiated; it consists of two class
14-
attributes (a list and a dictionary), and a set of static
15-
methods that operate on those attributes, accessing them
16-
directly as class attributes.
17-
18-
Attributes:
19-
20-
*figs*:
21-
dictionary of the form {*num*: *manager*, ...}
22-
23-
*_activeQue*:
24-
list of *managers*, with active one at the end
25-
12+
Singleton to maintain the relation between figures and their managers, and
13+
keep track of and "active" figure and manager.
14+
15+
The canvas of a figure created through pyplot is associated with a figure
16+
manager, which handles the interaction between the figure and the backend.
17+
pyplot keeps track of figure managers using an identifier, the "figure
18+
number" or "manager number" (which can actually be any hashable value);
19+
this number is available as the :attr:`number` attribute of the manager.
20+
21+
This class is never instantiated; it consists of an `OrderedDict` mapping
22+
figure/manager numbers to managers, and a set of class methods that
23+
manipulate this `OrderedDict`.
24+
25+
Attributes
26+
----------
27+
figs : OrderedDict
28+
`OrderedDict` mapping numbers to managers; the active manager is at the
29+
end.
2630
"""
27-
_activeQue= []
28-
figs={}
31+
32+
figs=OrderedDict()
2933

3034
@classmethod
3135
defget_fig_manager(cls,num):
3236
"""
33-
Iffiguremanager *num* exists, make it the active
34-
figure and return the manager;otherwise return *None*.
37+
If managernumber*num* exists, make it the active one and return it;
38+
otherwise return *None*.
3539
"""
3640
manager=cls.figs.get(num,None)
3741
ifmanagerisnotNone:
@@ -41,90 +45,73 @@ def get_fig_manager(cls, num):
4145
@classmethod
4246
defdestroy(cls,num):
4347
"""
44-
Try to remove all traces offigure *num*.
48+
Destroyfigure number *num*.
4549
46-
In the interactive backends, this is bound to the
47-
window "destroy" and"delete" events.
50+
In the interactive backends, this is bound to the window "destroy" and
51+
"delete" events.
4852
"""
4953
ifnotcls.has_fignum(num):
5054
return
51-
manager=cls.figs[num]
55+
manager=cls.figs.pop(num)
5256
manager.canvas.mpl_disconnect(manager._cidgcf)
53-
cls._activeQue.remove(manager)
54-
delcls.figs[num]
5557
manager.destroy()
5658
gc.collect(1)
5759

5860
@classmethod
5961
defdestroy_fig(cls,fig):
60-
"*fig* is a Figure instance"
61-
num=next((manager.numformanagerincls.figs.values()
62-
ifmanager.canvas.figure==fig),None)
63-
ifnumisnotNone:
64-
cls.destroy(num)
62+
"""Destroy figure*fig*."""
63+
canvas=getattr(fig,"canvas",None)
64+
manager=getattr(canvas,"manager",None)
65+
num=getattr(manager,"num",None)
66+
cls.destroy(num)
6567

6668
@classmethod
6769
defdestroy_all(cls):
68-
# this is need to ensure that gc is available in corner cases
69-
# where modules are being torn down after install with easy_install
70-
importgc# noqa
70+
"""Destroy all figures."""
71+
# Reimport gc in case the module globals have already been removed
72+
# during interpreter shutdown.
73+
importgc
7174
formanagerinlist(cls.figs.values()):
7275
manager.canvas.mpl_disconnect(manager._cidgcf)
7376
manager.destroy()
74-
75-
cls._activeQue= []
7677
cls.figs.clear()
7778
gc.collect(1)
7879

7980
@classmethod
8081
defhas_fignum(cls,num):
81-
"""
82-
Return *True* if figure *num* exists.
83-
"""
82+
"""Return whether figure number *num* exists."""
8483
returnnumincls.figs
8584

8685
@classmethod
8786
defget_all_fig_managers(cls):
88-
"""
89-
Return a list of figure managers.
90-
"""
87+
"""Return a list of figure managers."""
9188
returnlist(cls.figs.values())
9289

9390
@classmethod
9491
defget_num_fig_managers(cls):
95-
"""
96-
Return the number of figures being managed.
97-
"""
92+
"""Return the number of figures being managed."""
9893
returnlen(cls.figs)
9994

10095
@classmethod
10196
defget_active(cls):
102-
"""
103-
Return the manager of the active figure, or *None*.
104-
"""
105-
iflen(cls._activeQue)==0:
106-
returnNone
107-
else:
108-
returncls._activeQue[-1]
97+
"""Return the active manager, or *None* if there is no manager."""
98+
returnnext(reversed(cls.figs.values()))ifcls.figselseNone
10999

110100
@classmethod
111101
defset_active(cls,manager):
112-
"""
113-
Make the figure corresponding to *manager* the active one.
114-
"""
115-
oldQue=cls._activeQue[:]
116-
cls._activeQue= [mforminoldQueifm!=manager]
117-
cls._activeQue.append(manager)
102+
"""Make *manager* the active manager."""
118103
cls.figs[manager.num]=manager
104+
cls.figs.move_to_end(manager.num)
119105

120106
@classmethod
121107
defdraw_all(cls,force=False):
122108
"""
123-
Redraw all figures registered with the pyplot
124-
state machine.
109+
Redraw allstale managedfigures, or, if *force* is True, all managed
110+
figures.
125111
"""
126-
forf_mgrincls.get_all_fig_managers():
127-
ifforceorf_mgr.canvas.figure.stale:
128-
f_mgr.canvas.draw_idle()
112+
formanagerincls.get_all_fig_managers():
113+
ifforceormanager.canvas.figure.stale:
114+
manager.canvas.draw_idle()
115+
129116

130117
atexit.register(Gcf.destroy_all)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp