container.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. from matplotlib.artist import Artist
  2. import matplotlib.cbook as cbook
  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 ("<{} object of {} artists>"
  11. .format(type(self).__name__, len(self)))
  12. def __new__(cls, *args, **kwargs):
  13. return tuple.__new__(cls, args[0])
  14. def __init__(self, kl, label=None):
  15. self.eventson = False # fire events only if eventson
  16. self._oid = 0 # an observer id
  17. self._propobservers = {} # a dict from oids to funcs
  18. self._remove_method = None
  19. self.set_label(label)
  20. def remove(self):
  21. for c in cbook.flatten(
  22. self, scalarp=lambda x: isinstance(x, Artist)):
  23. if c is not None:
  24. c.remove()
  25. if self._remove_method:
  26. self._remove_method(self)
  27. def get_children(self):
  28. return [child for child in cbook.flatten(self) if child is not None]
  29. get_label = Artist.get_label
  30. set_label = Artist.set_label
  31. add_callback = Artist.add_callback
  32. remove_callback = Artist.remove_callback
  33. pchanged = Artist.pchanged
  34. class BarContainer(Container):
  35. """
  36. Container for the artists of bar plots (e.g. created by `.Axes.bar`).
  37. The container can be treated as a tuple of the *patches* themselves.
  38. Additionally, you can access these and further parameters by the
  39. attributes.
  40. Attributes
  41. ----------
  42. patches : list of :class:`~matplotlib.patches.Rectangle`
  43. The artists of the bars.
  44. errorbar : None or :class:`~matplotlib.container.ErrorbarContainer`
  45. A container for the error bar artists if error bars are present.
  46. *None* otherwise.
  47. """
  48. def __init__(self, patches, errorbar=None, **kwargs):
  49. self.patches = patches
  50. self.errorbar = errorbar
  51. Container.__init__(self, patches, **kwargs)
  52. class ErrorbarContainer(Container):
  53. """
  54. Container for the artists of error bars (e.g. created by `.Axes.errorbar`).
  55. The container can be treated as the *lines* tuple itself.
  56. Additionally, you can access these and further parameters by the
  57. attributes.
  58. Attributes
  59. ----------
  60. lines : tuple
  61. Tuple of ``(data_line, caplines, barlinecols)``.
  62. - data_line : :class:`~matplotlib.lines.Line2D` instance of
  63. x, y plot markers and/or line.
  64. - caplines : tuple of :class:`~matplotlib.lines.Line2D` instances of
  65. the error bar caps.
  66. - barlinecols : list of :class:`~matplotlib.collections.LineCollection`
  67. with the horizontal and vertical error ranges.
  68. has_xerr, has_yerr : bool
  69. ``True`` if the errorbar has x/y errors.
  70. """
  71. def __init__(self, lines, has_xerr=False, has_yerr=False, **kwargs):
  72. self.lines = lines
  73. self.has_xerr = has_xerr
  74. self.has_yerr = has_yerr
  75. Container.__init__(self, lines, **kwargs)
  76. class StemContainer(Container):
  77. """
  78. Container for the artists created in a :meth:`.Axes.stem` plot.
  79. The container can be treated like a namedtuple ``(markerline, stemlines,
  80. baseline)``.
  81. Attributes
  82. ----------
  83. markerline : :class:`~matplotlib.lines.Line2D`
  84. The artist of the markers at the stem heads.
  85. stemlines : list of :class:`~matplotlib.lines.Line2D`
  86. The artists of the vertical lines for all stems.
  87. baseline : :class:`~matplotlib.lines.Line2D`
  88. The artist of the horizontal baseline.
  89. """
  90. def __init__(self, markerline_stemlines_baseline, **kwargs):
  91. """
  92. Parameters
  93. ----------
  94. markerline_stemlines_baseline : tuple
  95. Tuple of ``(markerline, stemlines, baseline)``.
  96. ``markerline`` contains the `LineCollection` of the markers,
  97. ``stemlines`` is a `LineCollection` of the main lines,
  98. ``baseline`` is the `Line2D` of the baseline.
  99. """
  100. markerline, stemlines, baseline = markerline_stemlines_baseline
  101. self.markerline = markerline
  102. self.stemlines = stemlines
  103. self.baseline = baseline
  104. Container.__init__(self, markerline_stemlines_baseline, **kwargs)