Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Manage figures for pyplot interface. 

3""" 

4 

5import atexit 

6import gc 

7 

8 

9class Gcf: 

10 """ 

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 _activeQue 

23 list of *managers*, with active one at the end 

24 

25 """ 

26 _activeQue = [] 

27 figs = {} 

28 

29 @classmethod 

30 def get_fig_manager(cls, num): 

31 """ 

32 If figure manager *num* exists, make it the active 

33 figure and return the manager; otherwise return *None*. 

34 """ 

35 manager = cls.figs.get(num, None) 

36 if manager is not None: 

37 cls.set_active(manager) 

38 return manager 

39 

40 @classmethod 

41 def destroy(cls, num): 

42 """ 

43 Try to remove all traces of figure *num*. 

44 

45 In the interactive backends, this is bound to the 

46 window "destroy" and "delete" events. 

47 """ 

48 if not cls.has_fignum(num): 

49 return 

50 manager = cls.figs[num] 

51 manager.canvas.mpl_disconnect(manager._cidgcf) 

52 cls._activeQue.remove(manager) 

53 del cls.figs[num] 

54 manager.destroy() 

55 gc.collect(1) 

56 

57 @classmethod 

58 def destroy_fig(cls, fig): 

59 "*fig* is a Figure instance" 

60 num = next((manager.num for manager in cls.figs.values() 

61 if manager.canvas.figure == fig), None) 

62 if num is not None: 

63 cls.destroy(num) 

64 

65 @classmethod 

66 def destroy_all(cls): 

67 # this is need to ensure that gc is available in corner cases 

68 # where modules are being torn down after install with easy_install 

69 import gc # noqa 

70 for manager in list(cls.figs.values()): 

71 manager.canvas.mpl_disconnect(manager._cidgcf) 

72 manager.destroy() 

73 

74 cls._activeQue = [] 

75 cls.figs.clear() 

76 gc.collect(1) 

77 

78 @classmethod 

79 def has_fignum(cls, num): 

80 """ 

81 Return *True* if figure *num* exists. 

82 """ 

83 return num in cls.figs 

84 

85 @classmethod 

86 def get_all_fig_managers(cls): 

87 """ 

88 Return a list of figure managers. 

89 """ 

90 return list(cls.figs.values()) 

91 

92 @classmethod 

93 def get_num_fig_managers(cls): 

94 """ 

95 Return the number of figures being managed. 

96 """ 

97 return len(cls.figs) 

98 

99 @classmethod 

100 def get_active(cls): 

101 """ 

102 Return the manager of the active figure, or *None*. 

103 """ 

104 if len(cls._activeQue) == 0: 

105 return None 

106 else: 

107 return cls._activeQue[-1] 

108 

109 @classmethod 

110 def set_active(cls, manager): 

111 """ 

112 Make the figure corresponding to *manager* the active one. 

113 """ 

114 oldQue = cls._activeQue[:] 

115 cls._activeQue = [m for m in oldQue if m != manager] 

116 cls._activeQue.append(manager) 

117 cls.figs[manager.num] = manager 

118 

119 @classmethod 

120 def draw_all(cls, force=False): 

121 """ 

122 Redraw all figures registered with the pyplot 

123 state machine. 

124 """ 

125 for f_mgr in cls.get_all_fig_managers(): 

126 if force or f_mgr.canvas.figure.stale: 

127 f_mgr.canvas.draw_idle() 

128 

129atexit.register(Gcf.destroy_all)