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 f"<{type(self).__name__} object of {len(self)} artists>"
15
16 def __new__(cls, *args, **kwargs):
17 return tuple.__new__(cls, args[0])
18
19 def __init__(self, kl, label=None):
20 self._callbacks = cbook.CallbackRegistry(signals=["pchanged"])
21 self._remove_method = None
22 self._label = str(label) if label is not None else None
23
24 def remove(self):
25 for c in cbook.flatten(
26 self, scalarp=lambda x: isinstance(x, Artist)):
27 if c is not None:
28 c.remove()
29 if self._remove_method:
30 self._remove_method(self)
31
32 def get_children(self):
33 return [child for child in cbook.flatten(self) if child is not None]
34
35 get_label = Artist.get_label
36 set_label = Artist.set_label
37 add_callback = Artist.add_callback
38 remove_callback = Artist.remove_callback
39 pchanged = Artist.pchanged
40
41
42class BarContainer(Container):
43 """
44 Container for the artists of bar plots (e.g. created by `.Axes.bar`).
45
46 The container can be treated as a tuple of the *patches* themselves.
47 Additionally, you can access these and further parameters by the
48 attributes.
49
50 Attributes
51 ----------
52 patches : list of :class:`~matplotlib.patches.Rectangle`
53 The artists of the bars.
54
55 errorbar : None or :class:`~matplotlib.container.ErrorbarContainer`
56 A container for the error bar artists if error bars are present.
57 *None* otherwise.
58
59 datavalues : None or array-like
60 The underlying data values corresponding to the bars.
61
62 orientation : {'vertical', 'horizontal'}, default: None
63 If 'vertical', the bars are assumed to be vertical.
64 If 'horizontal', the bars are assumed to be horizontal.
65
66 """
67
68 def __init__(self, patches, errorbar=None, *, datavalues=None,
69 orientation=None, **kwargs):
70 self.patches = patches
71 self.errorbar = errorbar
72 self.datavalues = datavalues
73 self.orientation = orientation
74 super().__init__(patches, **kwargs)
75
76
77class ErrorbarContainer(Container):
78 """
79 Container for the artists of error bars (e.g. created by `.Axes.errorbar`).
80
81 The container can be treated as the *lines* tuple itself.
82 Additionally, you can access these and further parameters by the
83 attributes.
84
85 Attributes
86 ----------
87 lines : tuple
88 Tuple of ``(data_line, caplines, barlinecols)``.
89
90 - data_line : A `~matplotlib.lines.Line2D` instance of x, y plot markers
91 and/or line.
92 - caplines : A tuple of `~matplotlib.lines.Line2D` instances of the error
93 bar caps.
94 - barlinecols : A tuple of `~matplotlib.collections.LineCollection` with the
95 horizontal and vertical error ranges.
96
97 has_xerr, has_yerr : bool
98 ``True`` if the errorbar has x/y errors.
99
100 """
101
102 def __init__(self, lines, has_xerr=False, has_yerr=False, **kwargs):
103 self.lines = lines
104 self.has_xerr = has_xerr
105 self.has_yerr = has_yerr
106 super().__init__(lines, **kwargs)
107
108
109class StemContainer(Container):
110 """
111 Container for the artists created in a :meth:`.Axes.stem` plot.
112
113 The container can be treated like a namedtuple ``(markerline, stemlines,
114 baseline)``.
115
116 Attributes
117 ----------
118 markerline : `~matplotlib.lines.Line2D`
119 The artist of the markers at the stem heads.
120
121 stemlines : `~matplotlib.collections.LineCollection`
122 The artists of the vertical lines for all stems.
123
124 baseline : `~matplotlib.lines.Line2D`
125 The artist of the horizontal baseline.
126 """
127 def __init__(self, markerline_stemlines_baseline, **kwargs):
128 """
129 Parameters
130 ----------
131 markerline_stemlines_baseline : tuple
132 Tuple of ``(markerline, stemlines, baseline)``.
133 ``markerline`` contains the `.Line2D` of the markers,
134 ``stemlines`` is a `.LineCollection` of the main lines,
135 ``baseline`` is the `.Line2D` of the baseline.
136 """
137 markerline, stemlines, baseline = markerline_stemlines_baseline
138 self.markerline = markerline
139 self.stemlines = stemlines
140 self.baseline = baseline
141 super().__init__(markerline_stemlines_baseline, **kwargs)