Coverage for /usr/lib/python3/dist-packages/matplotlib/container.py: 43%

44 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1from matplotlib import cbook 

2from matplotlib.artist import Artist 

3 

4 

5class Container(tuple): 

6 """ 

7 Base class for containers. 

8 

9 Containers are classes that collect semantically related Artists such as 

10 the bars of a bar plot. 

11 """ 

12 

13 def __repr__(self): 

14 return ("<{} object of {} artists>" 

15 .format(type(self).__name__, len(self))) 

16 

17 def __new__(cls, *args, **kwargs): 

18 return tuple.__new__(cls, args[0]) 

19 

20 def __init__(self, kl, label=None): 

21 self._callbacks = cbook.CallbackRegistry(signals=["pchanged"]) 

22 self._remove_method = None 

23 self.set_label(label) 

24 

25 def remove(self): 

26 for c in cbook.flatten( 

27 self, scalarp=lambda x: isinstance(x, Artist)): 

28 if c is not None: 

29 c.remove() 

30 if self._remove_method: 

31 self._remove_method(self) 

32 

33 def get_children(self): 

34 return [child for child in cbook.flatten(self) if child is not None] 

35 

36 get_label = Artist.get_label 

37 set_label = Artist.set_label 

38 add_callback = Artist.add_callback 

39 remove_callback = Artist.remove_callback 

40 pchanged = Artist.pchanged 

41 

42 

43class BarContainer(Container): 

44 """ 

45 Container for the artists of bar plots (e.g. created by `.Axes.bar`). 

46 

47 The container can be treated as a tuple of the *patches* themselves. 

48 Additionally, you can access these and further parameters by the 

49 attributes. 

50 

51 Attributes 

52 ---------- 

53 patches : list of :class:`~matplotlib.patches.Rectangle` 

54 The artists of the bars. 

55 

56 errorbar : None or :class:`~matplotlib.container.ErrorbarContainer` 

57 A container for the error bar artists if error bars are present. 

58 *None* otherwise. 

59 

60 datavalues : None or array-like 

61 The underlying data values corresponding to the bars. 

62 

63 orientation : {'vertical', 'horizontal'}, default: None 

64 If 'vertical', the bars are assumed to be vertical. 

65 If 'horizontal', the bars are assumed to be horizontal. 

66 

67 """ 

68 

69 def __init__(self, patches, errorbar=None, *, datavalues=None, 

70 orientation=None, **kwargs): 

71 self.patches = patches 

72 self.errorbar = errorbar 

73 self.datavalues = datavalues 

74 self.orientation = orientation 

75 super().__init__(patches, **kwargs) 

76 

77 

78class ErrorbarContainer(Container): 

79 """ 

80 Container for the artists of error bars (e.g. created by `.Axes.errorbar`). 

81 

82 The container can be treated as the *lines* tuple itself. 

83 Additionally, you can access these and further parameters by the 

84 attributes. 

85 

86 Attributes 

87 ---------- 

88 lines : tuple 

89 Tuple of ``(data_line, caplines, barlinecols)``. 

90 

91 - data_line : :class:`~matplotlib.lines.Line2D` instance of 

92 x, y plot markers and/or line. 

93 - caplines : tuple of :class:`~matplotlib.lines.Line2D` instances of 

94 the error bar caps. 

95 - barlinecols : list of :class:`~matplotlib.collections.LineCollection` 

96 with the horizontal and vertical error ranges. 

97 

98 has_xerr, has_yerr : bool 

99 ``True`` if the errorbar has x/y errors. 

100 

101 """ 

102 

103 def __init__(self, lines, has_xerr=False, has_yerr=False, **kwargs): 

104 self.lines = lines 

105 self.has_xerr = has_xerr 

106 self.has_yerr = has_yerr 

107 super().__init__(lines, **kwargs) 

108 

109 

110class StemContainer(Container): 

111 """ 

112 Container for the artists created in a :meth:`.Axes.stem` plot. 

113 

114 The container can be treated like a namedtuple ``(markerline, stemlines, 

115 baseline)``. 

116 

117 Attributes 

118 ---------- 

119 markerline : :class:`~matplotlib.lines.Line2D` 

120 The artist of the markers at the stem heads. 

121 

122 stemlines : list of :class:`~matplotlib.lines.Line2D` 

123 The artists of the vertical lines for all stems. 

124 

125 baseline : :class:`~matplotlib.lines.Line2D` 

126 The artist of the horizontal baseline. 

127 """ 

128 def __init__(self, markerline_stemlines_baseline, **kwargs): 

129 """ 

130 Parameters 

131 ---------- 

132 markerline_stemlines_baseline : tuple 

133 Tuple of ``(markerline, stemlines, baseline)``. 

134 ``markerline`` contains the `.LineCollection` of the markers, 

135 ``stemlines`` is a `.LineCollection` of the main lines, 

136 ``baseline`` is the `.Line2D` of the baseline. 

137 """ 

138 markerline, stemlines, baseline = markerline_stemlines_baseline 

139 self.markerline = markerline 

140 self.stemlines = stemlines 

141 self.baseline = baseline 

142 super().__init__(markerline_stemlines_baseline, **kwargs)