container.py 4.5 KB

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