offsetbox.py 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. r"""
  2. Container classes for `.Artist`\s.
  3. `OffsetBox`
  4. The base of all container artists defined in this module.
  5. `AnchoredOffsetbox`, `AnchoredText`
  6. Anchor and align an arbitrary `.Artist` or a text relative to the parent
  7. axes or a specific anchor point.
  8. `DrawingArea`
  9. A container with fixed width and height. Children have a fixed position
  10. inside the container and may be clipped.
  11. `HPacker`, `VPacker`
  12. Containers for layouting their children vertically or horizontally.
  13. `PaddedBox`
  14. A container to add a padding around an `.Artist`.
  15. `TextArea`
  16. Contains a single `.Text` instance.
  17. """
  18. import numpy as np
  19. from matplotlib import cbook, docstring, rcParams
  20. import matplotlib.artist as martist
  21. import matplotlib.path as mpath
  22. import matplotlib.text as mtext
  23. import matplotlib.transforms as mtransforms
  24. from matplotlib.font_manager import FontProperties
  25. from matplotlib.image import BboxImage
  26. from matplotlib.patches import (
  27. FancyBboxPatch, FancyArrowPatch, bbox_artist as mbbox_artist)
  28. from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
  29. DEBUG = False
  30. # for debugging use
  31. def bbox_artist(*args, **kwargs):
  32. if DEBUG:
  33. mbbox_artist(*args, **kwargs)
  34. # _get_packed_offsets() and _get_aligned_offsets() are coded assuming
  35. # that we are packing boxes horizontally. But same function will be
  36. # used with vertical packing.
  37. def _get_packed_offsets(wd_list, total, sep, mode="fixed"):
  38. """
  39. Given a list of (width, xdescent) of each boxes, calculate the
  40. total width and the x-offset positions of each items according to
  41. *mode*. xdescent is analogous to the usual descent, but along the
  42. x-direction. xdescent values are currently ignored.
  43. For simplicity of the description, the terminology used here assumes a
  44. horizontal layout, but the function works equally for a vertical layout.
  45. There are three packing modes:
  46. - 'fixed': The elements are packed tight to the left with a spacing of
  47. *sep* in between. If *total* is *None* the returned total will be the
  48. right edge of the last box. A non-*None* total will be passed unchecked
  49. to the output. In particular this means that right edge of the last
  50. box may be further to the right than the returned total.
  51. - 'expand': Distribute the boxes with equal spacing so that the left edge
  52. of the first box is at 0, and the right edge of the last box is at
  53. *total*. The parameter *sep* is ignored in this mode. A total of *None*
  54. is accepted and considered equal to 1. The total is returned unchanged
  55. (except for the conversion *None* to 1). If the total is smaller than
  56. the sum of the widths, the laid out boxes will overlap.
  57. - 'equal': If *total* is given, the total space is divided in N equal
  58. ranges and each box is left-aligned within its subspace.
  59. Otherwise (*total* is *None*), *sep* must be provided and each box is
  60. left-aligned in its subspace of width ``(max(widths) + sep)``. The
  61. total width is then calculated to be ``N * (max(widths) + sep)``.
  62. Parameters
  63. ----------
  64. wd_list : list of (float, float)
  65. (width, xdescent) of boxes to be packed.
  66. total : float or None
  67. Intended total length. *None* if not used.
  68. sep : float
  69. Spacing between boxes.
  70. mode : {'fixed', 'expand', 'equal'}
  71. The packing mode.
  72. Returns
  73. -------
  74. total : float
  75. The total width needed to accommodate the laid out boxes.
  76. offsets : array of float
  77. The left offsets of the boxes.
  78. """
  79. w_list, d_list = zip(*wd_list) # d_list is currently not used.
  80. cbook._check_in_list(["fixed", "expand", "equal"], mode=mode)
  81. if mode == "fixed":
  82. offsets_ = np.cumsum([0] + [w + sep for w in w_list])
  83. offsets = offsets_[:-1]
  84. if total is None:
  85. total = offsets_[-1] - sep
  86. return total, offsets
  87. elif mode == "expand":
  88. # This is a bit of a hack to avoid a TypeError when *total*
  89. # is None and used in conjugation with tight layout.
  90. if total is None:
  91. total = 1
  92. if len(w_list) > 1:
  93. sep = (total - sum(w_list)) / (len(w_list) - 1)
  94. else:
  95. sep = 0
  96. offsets_ = np.cumsum([0] + [w + sep for w in w_list])
  97. offsets = offsets_[:-1]
  98. return total, offsets
  99. elif mode == "equal":
  100. maxh = max(w_list)
  101. if total is None:
  102. if sep is None:
  103. raise ValueError("total and sep cannot both be None when "
  104. "using layout mode 'equal'")
  105. total = (maxh + sep) * len(w_list)
  106. else:
  107. sep = total / len(w_list) - maxh
  108. offsets = (maxh + sep) * np.arange(len(w_list))
  109. return total, offsets
  110. def _get_aligned_offsets(hd_list, height, align="baseline"):
  111. """
  112. Given a list of (height, descent) of each boxes, align the boxes
  113. with *align* and calculate the y-offsets of each boxes.
  114. total width and the offset positions of each items according to
  115. *mode*. xdescent is analogous to the usual descent, but along the
  116. x-direction. xdescent values are currently ignored.
  117. Parameters
  118. ----------
  119. hd_list
  120. List of (height, xdescent) of boxes to be aligned.
  121. height : float or None
  122. Intended total length. If None, the maximum of the heights in *hd_list*
  123. is used.
  124. align : {'baseline', 'left', 'top', 'right', 'bottom', 'center'}
  125. Align mode.
  126. """
  127. if height is None:
  128. height = max(h for h, d in hd_list)
  129. cbook._check_in_list(
  130. ["baseline", "left", "top", "right", "bottom", "center"], align=align)
  131. if align == "baseline":
  132. height_descent = max(h - d for h, d in hd_list)
  133. descent = max(d for h, d in hd_list)
  134. height = height_descent + descent
  135. offsets = [0. for h, d in hd_list]
  136. elif align in ["left", "top"]:
  137. descent = 0.
  138. offsets = [d for h, d in hd_list]
  139. elif align in ["right", "bottom"]:
  140. descent = 0.
  141. offsets = [height - h + d for h, d in hd_list]
  142. elif align == "center":
  143. descent = 0.
  144. offsets = [(height - h) * .5 + d for h, d in hd_list]
  145. return height, descent, offsets
  146. class OffsetBox(martist.Artist):
  147. """
  148. The OffsetBox is a simple container artist.
  149. The child artists are meant to be drawn at a relative position to its
  150. parent.
  151. Being an artist itself, all parameters are passed on to `.Artist`.
  152. """
  153. def __init__(self, *args, **kwargs):
  154. super().__init__(*args, **kwargs)
  155. # Clipping has not been implemented in the OffesetBox family, so
  156. # disable the clip flag for consistency. It can always be turned back
  157. # on to zero effect.
  158. self.set_clip_on(False)
  159. self._children = []
  160. self._offset = (0, 0)
  161. def set_figure(self, fig):
  162. """
  163. Set the `.Figure` for the `.OffsetBox` and all its children.
  164. Parameters
  165. ----------
  166. fig : `~matplotlib.figure.Figure`
  167. """
  168. martist.Artist.set_figure(self, fig)
  169. for c in self.get_children():
  170. c.set_figure(fig)
  171. @martist.Artist.axes.setter
  172. def axes(self, ax):
  173. # TODO deal with this better
  174. martist.Artist.axes.fset(self, ax)
  175. for c in self.get_children():
  176. if c is not None:
  177. c.axes = ax
  178. def contains(self, mouseevent):
  179. """
  180. Delegate the mouse event contains-check to the children.
  181. As a container, the `.OffsetBox` does not respond itself to
  182. mouseevents.
  183. Parameters
  184. ----------
  185. mouseevent : `matplotlib.backend_bases.MouseEvent`
  186. Returns
  187. -------
  188. contains : bool
  189. Whether any values are within the radius.
  190. details : dict
  191. An artist-specific dictionary of details of the event context,
  192. such as which points are contained in the pick radius. See the
  193. individual Artist subclasses for details.
  194. See Also
  195. --------
  196. .Artist.contains
  197. """
  198. inside, info = self._default_contains(mouseevent)
  199. if inside is not None:
  200. return inside, info
  201. for c in self.get_children():
  202. a, b = c.contains(mouseevent)
  203. if a:
  204. return a, b
  205. return False, {}
  206. def set_offset(self, xy):
  207. """
  208. Set the offset.
  209. Parameters
  210. ----------
  211. xy : (float, float) or callable
  212. The (x, y) coordinates of the offset in display units. These can
  213. either be given explicitly as a tuple (x, y), or by providing a
  214. function that converts the extent into the offset. This function
  215. must have the signature::
  216. def offset(width, height, xdescent, ydescent, renderer) \
  217. -> (float, float)
  218. """
  219. self._offset = xy
  220. self.stale = True
  221. def get_offset(self, width, height, xdescent, ydescent, renderer):
  222. """
  223. Return the offset as a tuple (x, y).
  224. The extent parameters have to be provided to handle the case where the
  225. offset is dynamically determined by a callable (see
  226. `~.OffsetBox.set_offset`).
  227. Parameters
  228. ----------
  229. width, height, xdescent, ydescent
  230. Extent parameters.
  231. renderer : `.RendererBase` subclass
  232. """
  233. return (self._offset(width, height, xdescent, ydescent, renderer)
  234. if callable(self._offset)
  235. else self._offset)
  236. def set_width(self, width):
  237. """
  238. Set the width of the box.
  239. Parameters
  240. ----------
  241. width : float
  242. """
  243. self.width = width
  244. self.stale = True
  245. def set_height(self, height):
  246. """
  247. Set the height of the box.
  248. Parameters
  249. ----------
  250. height : float
  251. """
  252. self.height = height
  253. self.stale = True
  254. def get_visible_children(self):
  255. r"""Return a list of the visible child `.Artist`\s."""
  256. return [c for c in self._children if c.get_visible()]
  257. def get_children(self):
  258. r"""Return a list of the child `.Artist`\s."""
  259. return self._children
  260. def get_extent_offsets(self, renderer):
  261. """
  262. Update offset of the children and return the extent of the box.
  263. Parameters
  264. ----------
  265. renderer : `.RendererBase` subclass
  266. Returns
  267. -------
  268. width
  269. height
  270. xdescent
  271. ydescent
  272. list of (xoffset, yoffset) pairs
  273. """
  274. raise NotImplementedError(
  275. "get_extent_offsets must be overridden in derived classes.")
  276. def get_extent(self, renderer):
  277. """Return a tuple ``width, height, xdescent, ydescent`` of the box."""
  278. w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  279. return w, h, xd, yd
  280. def get_window_extent(self, renderer):
  281. """Return the bounding box (`.Bbox`) in display space."""
  282. w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  283. px, py = self.get_offset(w, h, xd, yd, renderer)
  284. return mtransforms.Bbox.from_bounds(px - xd, py - yd, w, h)
  285. def draw(self, renderer):
  286. """
  287. Update the location of children if necessary and draw them
  288. to the given *renderer*.
  289. """
  290. width, height, xdescent, ydescent, offsets = self.get_extent_offsets(
  291. renderer)
  292. px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
  293. for c, (ox, oy) in zip(self.get_visible_children(), offsets):
  294. c.set_offset((px + ox, py + oy))
  295. c.draw(renderer)
  296. bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
  297. self.stale = False
  298. class PackerBase(OffsetBox):
  299. def __init__(self, pad=None, sep=None, width=None, height=None,
  300. align=None, mode=None,
  301. children=None):
  302. """
  303. Parameters
  304. ----------
  305. pad : float, optional
  306. The boundary padding in points.
  307. sep : float, optional
  308. The spacing between items in points.
  309. width, height : float, optional
  310. Width and height of the container box in pixels, calculated if
  311. *None*.
  312. align : {'top', 'bottom', 'left', 'right', 'center', 'baseline'}
  313. Alignment of boxes.
  314. mode : {'fixed', 'expand', 'equal'}
  315. The packing mode.
  316. - 'fixed' packs the given `.Artists` tight with *sep* spacing.
  317. - 'expand' uses the maximal available space to distribute the
  318. artists with equal spacing in between.
  319. - 'equal': Each artist an equal fraction of the available space
  320. and is left-aligned (or top-aligned) therein.
  321. children : list of `.Artist`
  322. The artists to pack.
  323. Notes
  324. -----
  325. *pad* and *sep* are in points and will be scaled with the renderer
  326. dpi, while *width* and *height* are in in pixels.
  327. """
  328. super().__init__()
  329. self.height = height
  330. self.width = width
  331. self.sep = sep
  332. self.pad = pad
  333. self.mode = mode
  334. self.align = align
  335. self._children = children
  336. class VPacker(PackerBase):
  337. """
  338. The VPacker has its children packed vertically. It automatically
  339. adjusts the relative positions of children at drawing time.
  340. """
  341. def __init__(self, pad=None, sep=None, width=None, height=None,
  342. align="baseline", mode="fixed",
  343. children=None):
  344. """
  345. Parameters
  346. ----------
  347. pad : float, optional
  348. The boundary padding in points.
  349. sep : float, optional
  350. The spacing between items in points.
  351. width, height : float, optional
  352. Width and height of the container box in pixels, calculated if
  353. *None*.
  354. align : {'top', 'bottom', 'left', 'right', 'center', 'baseline'}
  355. Alignment of boxes.
  356. mode : {'fixed', 'expand', 'equal'}
  357. The packing mode.
  358. - 'fixed' packs the given `.Artists` tight with *sep* spacing.
  359. - 'expand' uses the maximal available space to distribute the
  360. artists with equal spacing in between.
  361. - 'equal': Each artist an equal fraction of the available space
  362. and is left-aligned (or top-aligned) therein.
  363. children : list of `.Artist`
  364. The artists to pack.
  365. Notes
  366. -----
  367. *pad* and *sep* are in points and will be scaled with the renderer
  368. dpi, while *width* and *height* are in in pixels.
  369. """
  370. super().__init__(pad, sep, width, height, align, mode, children)
  371. def get_extent_offsets(self, renderer):
  372. # docstring inherited
  373. dpicor = renderer.points_to_pixels(1.)
  374. pad = self.pad * dpicor
  375. sep = self.sep * dpicor
  376. if self.width is not None:
  377. for c in self.get_visible_children():
  378. if isinstance(c, PackerBase) and c.mode == "expand":
  379. c.set_width(self.width)
  380. whd_list = [c.get_extent(renderer)
  381. for c in self.get_visible_children()]
  382. whd_list = [(w, h, xd, (h - yd)) for w, h, xd, yd in whd_list]
  383. wd_list = [(w, xd) for w, h, xd, yd in whd_list]
  384. width, xdescent, xoffsets = _get_aligned_offsets(wd_list,
  385. self.width,
  386. self.align)
  387. pack_list = [(h, yd) for w, h, xd, yd in whd_list]
  388. height, yoffsets_ = _get_packed_offsets(pack_list, self.height,
  389. sep, self.mode)
  390. yoffsets = yoffsets_ + [yd for w, h, xd, yd in whd_list]
  391. ydescent = height - yoffsets[0]
  392. yoffsets = height - yoffsets
  393. yoffsets = yoffsets - ydescent
  394. return (width + 2 * pad, height + 2 * pad,
  395. xdescent + pad, ydescent + pad,
  396. list(zip(xoffsets, yoffsets)))
  397. class HPacker(PackerBase):
  398. """
  399. The HPacker has its children packed horizontally. It automatically
  400. adjusts the relative positions of children at draw time.
  401. """
  402. def __init__(self, pad=None, sep=None, width=None, height=None,
  403. align="baseline", mode="fixed",
  404. children=None):
  405. """
  406. Parameters
  407. ----------
  408. pad : float, optional
  409. The boundary padding in points.
  410. sep : float, optional
  411. The spacing between items in points.
  412. width, height : float, optional
  413. Width and height of the container box in pixels, calculated if
  414. *None*.
  415. align : {'top', 'bottom', 'left', 'right', 'center', 'baseline'}
  416. Alignment of boxes.
  417. mode : {'fixed', 'expand', 'equal'}
  418. The packing mode.
  419. - 'fixed' packs the given `.Artists` tight with *sep* spacing.
  420. - 'expand' uses the maximal available space to distribute the
  421. artists with equal spacing in between.
  422. - 'equal': Each artist an equal fraction of the available space
  423. and is left-aligned (or top-aligned) therein.
  424. children : list of `.Artist`
  425. The artists to pack.
  426. Notes
  427. -----
  428. *pad* and *sep* are in points and will be scaled with the renderer
  429. dpi, while *width* and *height* are in in pixels.
  430. """
  431. super().__init__(pad, sep, width, height, align, mode, children)
  432. def get_extent_offsets(self, renderer):
  433. # docstring inherited
  434. dpicor = renderer.points_to_pixels(1.)
  435. pad = self.pad * dpicor
  436. sep = self.sep * dpicor
  437. whd_list = [c.get_extent(renderer)
  438. for c in self.get_visible_children()]
  439. if not whd_list:
  440. return 2 * pad, 2 * pad, pad, pad, []
  441. if self.height is None:
  442. height_descent = max(h - yd for w, h, xd, yd in whd_list)
  443. ydescent = max(yd for w, h, xd, yd in whd_list)
  444. height = height_descent + ydescent
  445. else:
  446. height = self.height - 2 * pad # width w/o pad
  447. hd_list = [(h, yd) for w, h, xd, yd in whd_list]
  448. height, ydescent, yoffsets = _get_aligned_offsets(hd_list,
  449. self.height,
  450. self.align)
  451. pack_list = [(w, xd) for w, h, xd, yd in whd_list]
  452. width, xoffsets_ = _get_packed_offsets(pack_list, self.width,
  453. sep, self.mode)
  454. xoffsets = xoffsets_ + [xd for w, h, xd, yd in whd_list]
  455. xdescent = whd_list[0][2]
  456. xoffsets = xoffsets - xdescent
  457. return (width + 2 * pad, height + 2 * pad,
  458. xdescent + pad, ydescent + pad,
  459. list(zip(xoffsets, yoffsets)))
  460. class PaddedBox(OffsetBox):
  461. """
  462. A container to add a padding around an `.Artist`.
  463. The `.PaddedBox` contains a `.FancyBboxPatch` that is used to visualize
  464. it when rendering.
  465. """
  466. def __init__(self, child, pad=None, draw_frame=False, patch_attrs=None):
  467. """
  468. Parameters
  469. ----------
  470. child : `~matplotlib.artist.Artist`
  471. The contained `.Artist`.
  472. pad : float
  473. The padding in points. This will be scaled with the renderer dpi.
  474. In contrast *width* and *height* are in *pixels* and thus not
  475. scaled.
  476. draw_frame : bool
  477. Whether to draw the contained `.FancyBboxPatch`.
  478. patch_attrs : dict or None
  479. Additional parameters passed to the contained `.FancyBboxPatch`.
  480. """
  481. super().__init__()
  482. self.pad = pad
  483. self._children = [child]
  484. self.patch = FancyBboxPatch(
  485. xy=(0.0, 0.0), width=1., height=1.,
  486. facecolor='w', edgecolor='k',
  487. mutation_scale=1, # self.prop.get_size_in_points(),
  488. snap=True,
  489. visible=draw_frame,
  490. boxstyle="square,pad=0",
  491. )
  492. if patch_attrs is not None:
  493. self.patch.update(patch_attrs)
  494. def get_extent_offsets(self, renderer):
  495. # docstring inherited.
  496. dpicor = renderer.points_to_pixels(1.)
  497. pad = self.pad * dpicor
  498. w, h, xd, yd = self._children[0].get_extent(renderer)
  499. return (w + 2 * pad, h + 2 * pad, xd + pad, yd + pad,
  500. [(0, 0)])
  501. def draw(self, renderer):
  502. # docstring inherited
  503. width, height, xdescent, ydescent, offsets = self.get_extent_offsets(
  504. renderer)
  505. px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
  506. for c, (ox, oy) in zip(self.get_visible_children(), offsets):
  507. c.set_offset((px + ox, py + oy))
  508. self.draw_frame(renderer)
  509. for c in self.get_visible_children():
  510. c.draw(renderer)
  511. #bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
  512. self.stale = False
  513. def update_frame(self, bbox, fontsize=None):
  514. self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height)
  515. if fontsize:
  516. self.patch.set_mutation_scale(fontsize)
  517. self.stale = True
  518. def draw_frame(self, renderer):
  519. # update the location and size of the legend
  520. self.update_frame(self.get_window_extent(renderer))
  521. self.patch.draw(renderer)
  522. class DrawingArea(OffsetBox):
  523. """
  524. The DrawingArea can contain any Artist as a child. The DrawingArea
  525. has a fixed width and height. The position of children relative to
  526. the parent is fixed. The children can be clipped at the
  527. boundaries of the parent.
  528. """
  529. def __init__(self, width, height, xdescent=0.,
  530. ydescent=0., clip=False):
  531. """
  532. Parameters
  533. ----------
  534. width, height : float
  535. Width and height of the container box.
  536. xdescent, ydescent : float
  537. Descent of the box in x- and y-direction.
  538. clip : bool
  539. Whether to clip the children to the box.
  540. """
  541. super().__init__()
  542. self.width = width
  543. self.height = height
  544. self.xdescent = xdescent
  545. self.ydescent = ydescent
  546. self._clip_children = clip
  547. self.offset_transform = mtransforms.Affine2D()
  548. self.dpi_transform = mtransforms.Affine2D()
  549. @property
  550. def clip_children(self):
  551. """
  552. If the children of this DrawingArea should be clipped
  553. by DrawingArea bounding box.
  554. """
  555. return self._clip_children
  556. @clip_children.setter
  557. def clip_children(self, val):
  558. self._clip_children = bool(val)
  559. self.stale = True
  560. def get_transform(self):
  561. """
  562. Return the `~matplotlib.transforms.Transform` applied to the children.
  563. """
  564. return self.dpi_transform + self.offset_transform
  565. def set_transform(self, t):
  566. """
  567. set_transform is ignored.
  568. """
  569. def set_offset(self, xy):
  570. """
  571. Set the offset of the container.
  572. Parameters
  573. ----------
  574. xy : (float, float)
  575. The (x, y) coordinates of the offset in display units.
  576. """
  577. self._offset = xy
  578. self.offset_transform.clear()
  579. self.offset_transform.translate(xy[0], xy[1])
  580. self.stale = True
  581. def get_offset(self):
  582. """Return offset of the container."""
  583. return self._offset
  584. def get_window_extent(self, renderer):
  585. """Return the bounding box in display space."""
  586. w, h, xd, yd = self.get_extent(renderer)
  587. ox, oy = self.get_offset() # w, h, xd, yd)
  588. return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
  589. def get_extent(self, renderer):
  590. """Return width, height, xdescent, ydescent of box."""
  591. dpi_cor = renderer.points_to_pixels(1.)
  592. return (self.width * dpi_cor, self.height * dpi_cor,
  593. self.xdescent * dpi_cor, self.ydescent * dpi_cor)
  594. def add_artist(self, a):
  595. """Add an `.Artist` to the container box."""
  596. self._children.append(a)
  597. if not a.is_transform_set():
  598. a.set_transform(self.get_transform())
  599. if self.axes is not None:
  600. a.axes = self.axes
  601. fig = self.figure
  602. if fig is not None:
  603. a.set_figure(fig)
  604. def draw(self, renderer):
  605. # docstring inherited
  606. dpi_cor = renderer.points_to_pixels(1.)
  607. self.dpi_transform.clear()
  608. self.dpi_transform.scale(dpi_cor)
  609. # At this point the DrawingArea has a transform
  610. # to the display space so the path created is
  611. # good for clipping children
  612. tpath = mtransforms.TransformedPath(
  613. mpath.Path([[0, 0], [0, self.height],
  614. [self.width, self.height],
  615. [self.width, 0]]),
  616. self.get_transform())
  617. for c in self._children:
  618. if self._clip_children and not (c.clipbox or c._clippath):
  619. c.set_clip_path(tpath)
  620. c.draw(renderer)
  621. bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
  622. self.stale = False
  623. class TextArea(OffsetBox):
  624. """
  625. The TextArea is contains a single Text instance. The text is
  626. placed at (0, 0) with baseline+left alignment. The width and height
  627. of the TextArea instance is the width and height of the its child
  628. text.
  629. """
  630. def __init__(self, s,
  631. textprops=None,
  632. multilinebaseline=None,
  633. minimumdescent=True,
  634. ):
  635. """
  636. Parameters
  637. ----------
  638. s : str
  639. The text to be displayed.
  640. textprops : dict, optional
  641. Dictionary of keyword parameters to be passed to the
  642. `~matplotlib.text.Text` instance contained inside TextArea.
  643. multilinebaseline : bool, optional
  644. If `True`, baseline for multiline text is adjusted so that it is
  645. (approximately) center-aligned with singleline text.
  646. minimumdescent : bool, optional
  647. If `True`, the box has a minimum descent of "p".
  648. """
  649. if textprops is None:
  650. textprops = {}
  651. textprops.setdefault("va", "baseline")
  652. self._text = mtext.Text(0, 0, s, **textprops)
  653. OffsetBox.__init__(self)
  654. self._children = [self._text]
  655. self.offset_transform = mtransforms.Affine2D()
  656. self._baseline_transform = mtransforms.Affine2D()
  657. self._text.set_transform(self.offset_transform +
  658. self._baseline_transform)
  659. self._multilinebaseline = multilinebaseline
  660. self._minimumdescent = minimumdescent
  661. def set_text(self, s):
  662. """Set the text of this area as a string."""
  663. self._text.set_text(s)
  664. self.stale = True
  665. def get_text(self):
  666. """Return the string representation of this area's text."""
  667. return self._text.get_text()
  668. def set_multilinebaseline(self, t):
  669. """
  670. Set multilinebaseline.
  671. If True, baseline for multiline text is adjusted so that it is
  672. (approximately) center-aligned with single-line text.
  673. """
  674. self._multilinebaseline = t
  675. self.stale = True
  676. def get_multilinebaseline(self):
  677. """
  678. Get multilinebaseline.
  679. """
  680. return self._multilinebaseline
  681. def set_minimumdescent(self, t):
  682. """
  683. Set minimumdescent.
  684. If True, extent of the single line text is adjusted so that
  685. it has minimum descent of "p"
  686. """
  687. self._minimumdescent = t
  688. self.stale = True
  689. def get_minimumdescent(self):
  690. """
  691. Get minimumdescent.
  692. """
  693. return self._minimumdescent
  694. def set_transform(self, t):
  695. """
  696. set_transform is ignored.
  697. """
  698. def set_offset(self, xy):
  699. """
  700. Set the offset of the container.
  701. Parameters
  702. ----------
  703. xy : (float, float)
  704. The (x, y) coordinates of the offset in display units.
  705. """
  706. self._offset = xy
  707. self.offset_transform.clear()
  708. self.offset_transform.translate(xy[0], xy[1])
  709. self.stale = True
  710. def get_offset(self):
  711. """Return offset of the container."""
  712. return self._offset
  713. def get_window_extent(self, renderer):
  714. """Return the bounding box in display space."""
  715. w, h, xd, yd = self.get_extent(renderer)
  716. ox, oy = self.get_offset() # w, h, xd, yd)
  717. return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
  718. def get_extent(self, renderer):
  719. _, h_, d_ = renderer.get_text_width_height_descent(
  720. "lp", self._text._fontproperties, ismath=False)
  721. bbox, info, d = self._text._get_layout(renderer)
  722. w, h = bbox.width, bbox.height
  723. self._baseline_transform.clear()
  724. if len(info) > 1 and self._multilinebaseline:
  725. d_new = 0.5 * h - 0.5 * (h_ - d_)
  726. self._baseline_transform.translate(0, d - d_new)
  727. d = d_new
  728. else: # single line
  729. h_d = max(h_ - d_, h - d)
  730. if self.get_minimumdescent():
  731. ## to have a minimum descent, #i.e., "l" and "p" have same
  732. ## descents.
  733. d = max(d, d_)
  734. #else:
  735. # d = d
  736. h = h_d + d
  737. return w, h, 0., d
  738. def draw(self, renderer):
  739. # docstring inherited
  740. self._text.draw(renderer)
  741. bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
  742. self.stale = False
  743. class AuxTransformBox(OffsetBox):
  744. """
  745. Offset Box with the aux_transform. Its children will be
  746. transformed with the aux_transform first then will be
  747. offsetted. The absolute coordinate of the aux_transform is meaning
  748. as it will be automatically adjust so that the left-lower corner
  749. of the bounding box of children will be set to (0, 0) before the
  750. offset transform.
  751. It is similar to drawing area, except that the extent of the box
  752. is not predetermined but calculated from the window extent of its
  753. children. Furthermore, the extent of the children will be
  754. calculated in the transformed coordinate.
  755. """
  756. def __init__(self, aux_transform):
  757. self.aux_transform = aux_transform
  758. OffsetBox.__init__(self)
  759. self.offset_transform = mtransforms.Affine2D()
  760. # ref_offset_transform makes offset_transform always relative to the
  761. # lower-left corner of the bbox of its children.
  762. self.ref_offset_transform = mtransforms.Affine2D()
  763. def add_artist(self, a):
  764. """Add an `.Artist` to the container box."""
  765. self._children.append(a)
  766. a.set_transform(self.get_transform())
  767. self.stale = True
  768. def get_transform(self):
  769. """
  770. Return the :class:`~matplotlib.transforms.Transform` applied
  771. to the children
  772. """
  773. return (self.aux_transform
  774. + self.ref_offset_transform
  775. + self.offset_transform)
  776. def set_transform(self, t):
  777. """
  778. set_transform is ignored.
  779. """
  780. def set_offset(self, xy):
  781. """
  782. Set the offset of the container.
  783. Parameters
  784. ----------
  785. xy : (float, float)
  786. The (x, y) coordinates of the offset in display units.
  787. """
  788. self._offset = xy
  789. self.offset_transform.clear()
  790. self.offset_transform.translate(xy[0], xy[1])
  791. self.stale = True
  792. def get_offset(self):
  793. """Return offset of the container."""
  794. return self._offset
  795. def get_window_extent(self, renderer):
  796. """Return the bounding box in display space."""
  797. w, h, xd, yd = self.get_extent(renderer)
  798. ox, oy = self.get_offset() # w, h, xd, yd)
  799. return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
  800. def get_extent(self, renderer):
  801. # clear the offset transforms
  802. _off = self.offset_transform.get_matrix() # to be restored later
  803. self.ref_offset_transform.clear()
  804. self.offset_transform.clear()
  805. # calculate the extent
  806. bboxes = [c.get_window_extent(renderer) for c in self._children]
  807. ub = mtransforms.Bbox.union(bboxes)
  808. # adjust ref_offset_transform
  809. self.ref_offset_transform.translate(-ub.x0, -ub.y0)
  810. # restor offset transform
  811. self.offset_transform.set_matrix(_off)
  812. return ub.width, ub.height, 0., 0.
  813. def draw(self, renderer):
  814. # docstring inherited
  815. for c in self._children:
  816. c.draw(renderer)
  817. bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
  818. self.stale = False
  819. class AnchoredOffsetbox(OffsetBox):
  820. """
  821. An offset box placed according to location *loc*.
  822. AnchoredOffsetbox has a single child. When multiple children are needed,
  823. use an extra OffsetBox to enclose them. By default, the offset box is
  824. anchored against its parent axes. You may explicitly specify the
  825. *bbox_to_anchor*.
  826. """
  827. zorder = 5 # zorder of the legend
  828. # Location codes
  829. codes = {'upper right': 1,
  830. 'upper left': 2,
  831. 'lower left': 3,
  832. 'lower right': 4,
  833. 'right': 5,
  834. 'center left': 6,
  835. 'center right': 7,
  836. 'lower center': 8,
  837. 'upper center': 9,
  838. 'center': 10,
  839. }
  840. def __init__(self, loc,
  841. pad=0.4, borderpad=0.5,
  842. child=None, prop=None, frameon=True,
  843. bbox_to_anchor=None,
  844. bbox_transform=None,
  845. **kwargs):
  846. """
  847. Parameters
  848. ----------
  849. loc : str
  850. The box location. Supported values:
  851. - 'upper right'
  852. - 'upper left'
  853. - 'lower left'
  854. - 'lower right'
  855. - 'center left'
  856. - 'center right'
  857. - 'lower center'
  858. - 'upper center'
  859. - 'center'
  860. For backward compatibility, numeric values are accepted as well.
  861. See the parameter *loc* of `.Legend` for details.
  862. pad : float, default: 0.4
  863. Padding around the child as fraction of the fontsize.
  864. borderpad : float, default: 0.5
  865. Padding between the offsetbox frame and the *bbox_to_anchor*.
  866. child : `.OffsetBox`
  867. The box that will be anchored.
  868. prop : `.FontProperties`
  869. This is only used as a reference for paddings. If not given,
  870. :rc:`legend.fontsize` is used.
  871. frameon : bool
  872. Whether to draw a frame around the box.
  873. bbox_to_anchor : `.BboxBase`, 2-tuple, or 4-tuple of floats
  874. Box that is used to position the legend in conjunction with *loc*.
  875. bbox_transform : None or :class:`matplotlib.transforms.Transform`
  876. The transform for the bounding box (*bbox_to_anchor*).
  877. **kwargs
  878. All other parameters are passed on to `.OffsetBox`.
  879. Notes
  880. -----
  881. See `.Legend` for a detailed description of the anchoring mechanism.
  882. """
  883. super().__init__(**kwargs)
  884. self.set_bbox_to_anchor(bbox_to_anchor, bbox_transform)
  885. self.set_child(child)
  886. if isinstance(loc, str):
  887. loc = cbook._check_getitem(self.codes, loc=loc)
  888. self.loc = loc
  889. self.borderpad = borderpad
  890. self.pad = pad
  891. if prop is None:
  892. self.prop = FontProperties(size=rcParams["legend.fontsize"])
  893. else:
  894. self.prop = FontProperties._from_any(prop)
  895. if isinstance(prop, dict) and "size" not in prop:
  896. self.prop.set_size(rcParams["legend.fontsize"])
  897. self.patch = FancyBboxPatch(
  898. xy=(0.0, 0.0), width=1., height=1.,
  899. facecolor='w', edgecolor='k',
  900. mutation_scale=self.prop.get_size_in_points(),
  901. snap=True,
  902. visible=frameon,
  903. boxstyle="square,pad=0",
  904. )
  905. def set_child(self, child):
  906. """Set the child to be anchored."""
  907. self._child = child
  908. if child is not None:
  909. child.axes = self.axes
  910. self.stale = True
  911. def get_child(self):
  912. """Return the child."""
  913. return self._child
  914. def get_children(self):
  915. """Return the list of children."""
  916. return [self._child]
  917. def get_extent(self, renderer):
  918. """
  919. Return the extent of the box as (width, height, x, y).
  920. This is the extent of the child plus the padding.
  921. """
  922. w, h, xd, yd = self.get_child().get_extent(renderer)
  923. fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
  924. pad = self.pad * fontsize
  925. return w + 2 * pad, h + 2 * pad, xd + pad, yd + pad
  926. def get_bbox_to_anchor(self):
  927. """Return the bbox that the box is anchored to."""
  928. if self._bbox_to_anchor is None:
  929. return self.axes.bbox
  930. else:
  931. transform = self._bbox_to_anchor_transform
  932. if transform is None:
  933. return self._bbox_to_anchor
  934. else:
  935. return TransformedBbox(self._bbox_to_anchor,
  936. transform)
  937. def set_bbox_to_anchor(self, bbox, transform=None):
  938. """
  939. Set the bbox that the box is anchored to.
  940. *bbox* can be a Bbox instance, a list of [left, bottom, width,
  941. height], or a list of [left, bottom] where the width and
  942. height will be assumed to be zero. The bbox will be
  943. transformed to display coordinate by the given transform.
  944. """
  945. if bbox is None or isinstance(bbox, BboxBase):
  946. self._bbox_to_anchor = bbox
  947. else:
  948. try:
  949. l = len(bbox)
  950. except TypeError as err:
  951. raise ValueError("Invalid argument for bbox : %s" %
  952. str(bbox)) from err
  953. if l == 2:
  954. bbox = [bbox[0], bbox[1], 0, 0]
  955. self._bbox_to_anchor = Bbox.from_bounds(*bbox)
  956. self._bbox_to_anchor_transform = transform
  957. self.stale = True
  958. def get_window_extent(self, renderer):
  959. """Return the bounding box in display space."""
  960. self._update_offset_func(renderer)
  961. w, h, xd, yd = self.get_extent(renderer)
  962. ox, oy = self.get_offset(w, h, xd, yd, renderer)
  963. return Bbox.from_bounds(ox - xd, oy - yd, w, h)
  964. def _update_offset_func(self, renderer, fontsize=None):
  965. """
  966. Update the offset func which depends on the dpi of the
  967. renderer (because of the padding).
  968. """
  969. if fontsize is None:
  970. fontsize = renderer.points_to_pixels(
  971. self.prop.get_size_in_points())
  972. def _offset(w, h, xd, yd, renderer, fontsize=fontsize, self=self):
  973. bbox = Bbox.from_bounds(0, 0, w, h)
  974. borderpad = self.borderpad * fontsize
  975. bbox_to_anchor = self.get_bbox_to_anchor()
  976. x0, y0 = self._get_anchored_bbox(self.loc,
  977. bbox,
  978. bbox_to_anchor,
  979. borderpad)
  980. return x0 + xd, y0 + yd
  981. self.set_offset(_offset)
  982. def update_frame(self, bbox, fontsize=None):
  983. self.patch.set_bounds(bbox.x0, bbox.y0, bbox.width, bbox.height)
  984. if fontsize:
  985. self.patch.set_mutation_scale(fontsize)
  986. def draw(self, renderer):
  987. # docstring inherited
  988. if not self.get_visible():
  989. return
  990. fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
  991. self._update_offset_func(renderer, fontsize)
  992. # update the location and size of the legend
  993. bbox = self.get_window_extent(renderer)
  994. self.update_frame(bbox, fontsize)
  995. self.patch.draw(renderer)
  996. width, height, xdescent, ydescent = self.get_extent(renderer)
  997. px, py = self.get_offset(width, height, xdescent, ydescent, renderer)
  998. self.get_child().set_offset((px, py))
  999. self.get_child().draw(renderer)
  1000. self.stale = False
  1001. def _get_anchored_bbox(self, loc, bbox, parentbbox, borderpad):
  1002. """
  1003. Return the position of the bbox anchored at the parentbbox
  1004. with the loc code, with the borderpad.
  1005. """
  1006. assert loc in range(1, 11) # called only internally
  1007. BEST, UR, UL, LL, LR, R, CL, CR, LC, UC, C = range(11)
  1008. anchor_coefs = {UR: "NE",
  1009. UL: "NW",
  1010. LL: "SW",
  1011. LR: "SE",
  1012. R: "E",
  1013. CL: "W",
  1014. CR: "E",
  1015. LC: "S",
  1016. UC: "N",
  1017. C: "C"}
  1018. c = anchor_coefs[loc]
  1019. container = parentbbox.padded(-borderpad)
  1020. anchored_box = bbox.anchored(c, container=container)
  1021. return anchored_box.x0, anchored_box.y0
  1022. class AnchoredText(AnchoredOffsetbox):
  1023. """
  1024. AnchoredOffsetbox with Text.
  1025. """
  1026. def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, **kwargs):
  1027. """
  1028. Parameters
  1029. ----------
  1030. s : str
  1031. Text.
  1032. loc : str
  1033. Location code. See `AnchoredOffsetbox`.
  1034. pad : float, default: 0.4
  1035. Padding around the text as fraction of the fontsize.
  1036. borderpad : float, default: 0.5
  1037. Spacing between the offsetbox frame and the *bbox_to_anchor*.
  1038. prop : dict, optional
  1039. Dictionary of keyword parameters to be passed to the
  1040. `~matplotlib.text.Text` instance contained inside AnchoredText.
  1041. **kwargs
  1042. All other parameters are passed to `AnchoredOffsetbox`.
  1043. """
  1044. if prop is None:
  1045. prop = {}
  1046. badkwargs = {'ha', 'horizontalalignment', 'va', 'verticalalignment'}
  1047. if badkwargs & set(prop):
  1048. raise ValueError(
  1049. "Mixing horizontalalignment or verticalalignment with "
  1050. "AnchoredText is not supported.")
  1051. self.txt = TextArea(s, textprops=prop, minimumdescent=False)
  1052. fp = self.txt._text.get_fontproperties()
  1053. super().__init__(
  1054. loc, pad=pad, borderpad=borderpad, child=self.txt, prop=fp,
  1055. **kwargs)
  1056. class OffsetImage(OffsetBox):
  1057. def __init__(self, arr,
  1058. zoom=1,
  1059. cmap=None,
  1060. norm=None,
  1061. interpolation=None,
  1062. origin=None,
  1063. filternorm=True,
  1064. filterrad=4.0,
  1065. resample=False,
  1066. dpi_cor=True,
  1067. **kwargs
  1068. ):
  1069. OffsetBox.__init__(self)
  1070. self._dpi_cor = dpi_cor
  1071. self.image = BboxImage(bbox=self.get_window_extent,
  1072. cmap=cmap,
  1073. norm=norm,
  1074. interpolation=interpolation,
  1075. origin=origin,
  1076. filternorm=filternorm,
  1077. filterrad=filterrad,
  1078. resample=resample,
  1079. **kwargs
  1080. )
  1081. self._children = [self.image]
  1082. self.set_zoom(zoom)
  1083. self.set_data(arr)
  1084. def set_data(self, arr):
  1085. self._data = np.asarray(arr)
  1086. self.image.set_data(self._data)
  1087. self.stale = True
  1088. def get_data(self):
  1089. return self._data
  1090. def set_zoom(self, zoom):
  1091. self._zoom = zoom
  1092. self.stale = True
  1093. def get_zoom(self):
  1094. return self._zoom
  1095. def get_offset(self):
  1096. """Return offset of the container."""
  1097. return self._offset
  1098. def get_children(self):
  1099. return [self.image]
  1100. def get_window_extent(self, renderer):
  1101. """Return the bounding box in display space."""
  1102. w, h, xd, yd = self.get_extent(renderer)
  1103. ox, oy = self.get_offset()
  1104. return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
  1105. def get_extent(self, renderer):
  1106. if self._dpi_cor: # True, do correction
  1107. dpi_cor = renderer.points_to_pixels(1.)
  1108. else:
  1109. dpi_cor = 1.
  1110. zoom = self.get_zoom()
  1111. data = self.get_data()
  1112. ny, nx = data.shape[:2]
  1113. w, h = dpi_cor * nx * zoom, dpi_cor * ny * zoom
  1114. return w, h, 0, 0
  1115. def draw(self, renderer):
  1116. # docstring inherited
  1117. self.image.draw(renderer)
  1118. # bbox_artist(self, renderer, fill=False, props=dict(pad=0.))
  1119. self.stale = False
  1120. class AnnotationBbox(martist.Artist, mtext._AnnotationBase):
  1121. """
  1122. Container for an `OffsetBox` referring to a specific position *xy*.
  1123. Optionally an arrow pointing from the offsetbox to *xy* can be drawn.
  1124. This is like `.Annotation`, but with `OffsetBox` instead of `.Text`.
  1125. """
  1126. zorder = 3
  1127. def __str__(self):
  1128. return "AnnotationBbox(%g,%g)" % (self.xy[0], self.xy[1])
  1129. @docstring.dedent_interpd
  1130. def __init__(self, offsetbox, xy,
  1131. xybox=None,
  1132. xycoords='data',
  1133. boxcoords=None,
  1134. frameon=True, pad=0.4, # FancyBboxPatch boxstyle.
  1135. annotation_clip=None,
  1136. box_alignment=(0.5, 0.5),
  1137. bboxprops=None,
  1138. arrowprops=None,
  1139. fontsize=None,
  1140. **kwargs):
  1141. """
  1142. Parameters
  1143. ----------
  1144. offsetbox : `OffsetBox`
  1145. xy : (float, float)
  1146. The point *(x, y)* to annotate. The coordinate system is determined
  1147. by *xycoords*.
  1148. xybox : (float, float), default: *xy*
  1149. The position *(x, y)* to place the text at. The coordinate system
  1150. is determined by *boxcoords*.
  1151. xycoords : str or `.Artist` or `.Transform` or callable or \
  1152. (float, float), default: 'data'
  1153. The coordinate system that *xy* is given in. See the parameter
  1154. *xycoords* in `.Annotation` for a detailed description.
  1155. boxcoords : str or `.Artist` or `.Transform` or callable or \
  1156. (float, float), default: value of *xycoords*
  1157. The coordinate system that *xybox* is given in. See the parameter
  1158. *textcoords* in `.Annotation` for a detailed description.
  1159. frameon : bool, default: True
  1160. Whether to draw a frame around the box.
  1161. pad : float, default: 0.4
  1162. Padding around the offsetbox.
  1163. box_alignment : (float, float)
  1164. A tuple of two floats for a vertical and horizontal alignment of
  1165. the offset box w.r.t. the *boxcoords*.
  1166. The lower-left corner is (0, 0) and upper-right corner is (1, 1).
  1167. **kwargs
  1168. Other parameters are identical to `.Annotation`.
  1169. """
  1170. martist.Artist.__init__(self, **kwargs)
  1171. mtext._AnnotationBase.__init__(self,
  1172. xy,
  1173. xycoords=xycoords,
  1174. annotation_clip=annotation_clip)
  1175. self.offsetbox = offsetbox
  1176. self.arrowprops = arrowprops
  1177. self.set_fontsize(fontsize)
  1178. if xybox is None:
  1179. self.xybox = xy
  1180. else:
  1181. self.xybox = xybox
  1182. if boxcoords is None:
  1183. self.boxcoords = xycoords
  1184. else:
  1185. self.boxcoords = boxcoords
  1186. if arrowprops is not None:
  1187. self._arrow_relpos = self.arrowprops.pop("relpos", (0.5, 0.5))
  1188. self.arrow_patch = FancyArrowPatch((0, 0), (1, 1),
  1189. **self.arrowprops)
  1190. else:
  1191. self._arrow_relpos = None
  1192. self.arrow_patch = None
  1193. self._box_alignment = box_alignment
  1194. # frame
  1195. self.patch = FancyBboxPatch(
  1196. xy=(0.0, 0.0), width=1., height=1.,
  1197. facecolor='w', edgecolor='k',
  1198. mutation_scale=self.prop.get_size_in_points(),
  1199. snap=True,
  1200. visible=frameon,
  1201. )
  1202. self.patch.set_boxstyle("square", pad=pad)
  1203. if bboxprops:
  1204. self.patch.set(**bboxprops)
  1205. @property
  1206. def xyann(self):
  1207. return self.xybox
  1208. @xyann.setter
  1209. def xyann(self, xyann):
  1210. self.xybox = xyann
  1211. self.stale = True
  1212. @property
  1213. def anncoords(self):
  1214. return self.boxcoords
  1215. @anncoords.setter
  1216. def anncoords(self, coords):
  1217. self.boxcoords = coords
  1218. self.stale = True
  1219. def contains(self, mouseevent):
  1220. inside, info = self._default_contains(mouseevent)
  1221. if inside is not None:
  1222. return inside, info
  1223. if not self._check_xy(None):
  1224. return False, {}
  1225. return self.offsetbox.contains(mouseevent)
  1226. #if self.arrow_patch is not None:
  1227. # a, ainfo=self.arrow_patch.contains(event)
  1228. # t = t or a
  1229. # self.arrow_patch is currently not checked as this can be a line - JJ
  1230. def get_children(self):
  1231. children = [self.offsetbox, self.patch]
  1232. if self.arrow_patch:
  1233. children.append(self.arrow_patch)
  1234. return children
  1235. def set_figure(self, fig):
  1236. if self.arrow_patch is not None:
  1237. self.arrow_patch.set_figure(fig)
  1238. self.offsetbox.set_figure(fig)
  1239. martist.Artist.set_figure(self, fig)
  1240. def set_fontsize(self, s=None):
  1241. """
  1242. Set the fontsize in points.
  1243. If *s* is not given, reset to :rc:`legend.fontsize`.
  1244. """
  1245. if s is None:
  1246. s = rcParams["legend.fontsize"]
  1247. self.prop = FontProperties(size=s)
  1248. self.stale = True
  1249. @cbook._delete_parameter("3.3", "s")
  1250. def get_fontsize(self, s=None):
  1251. """Return the fontsize in points."""
  1252. return self.prop.get_size_in_points()
  1253. def get_window_extent(self, renderer):
  1254. """
  1255. get the bounding box in display space.
  1256. """
  1257. bboxes = [child.get_window_extent(renderer)
  1258. for child in self.get_children()]
  1259. return Bbox.union(bboxes)
  1260. def get_tightbbox(self, renderer):
  1261. """
  1262. get tight bounding box in display space.
  1263. """
  1264. bboxes = [child.get_tightbbox(renderer)
  1265. for child in self.get_children()]
  1266. return Bbox.union(bboxes)
  1267. def update_positions(self, renderer):
  1268. """
  1269. Update the pixel positions of the annotated point and the text.
  1270. """
  1271. xy_pixel = self._get_position_xy(renderer)
  1272. self._update_position_xybox(renderer, xy_pixel)
  1273. mutation_scale = renderer.points_to_pixels(self.get_fontsize())
  1274. self.patch.set_mutation_scale(mutation_scale)
  1275. if self.arrow_patch:
  1276. self.arrow_patch.set_mutation_scale(mutation_scale)
  1277. def _update_position_xybox(self, renderer, xy_pixel):
  1278. """
  1279. Update the pixel positions of the annotation text and the arrow patch.
  1280. """
  1281. x, y = self.xybox
  1282. if isinstance(self.boxcoords, tuple):
  1283. xcoord, ycoord = self.boxcoords
  1284. x1, y1 = self._get_xy(renderer, x, y, xcoord)
  1285. x2, y2 = self._get_xy(renderer, x, y, ycoord)
  1286. ox0, oy0 = x1, y2
  1287. else:
  1288. ox0, oy0 = self._get_xy(renderer, x, y, self.boxcoords)
  1289. w, h, xd, yd = self.offsetbox.get_extent(renderer)
  1290. _fw, _fh = self._box_alignment
  1291. self.offsetbox.set_offset((ox0 - _fw * w + xd, oy0 - _fh * h + yd))
  1292. # update patch position
  1293. bbox = self.offsetbox.get_window_extent(renderer)
  1294. #self.offsetbox.set_offset((ox0-_fw*w, oy0-_fh*h))
  1295. self.patch.set_bounds(bbox.x0, bbox.y0,
  1296. bbox.width, bbox.height)
  1297. x, y = xy_pixel
  1298. ox1, oy1 = x, y
  1299. if self.arrowprops:
  1300. d = self.arrowprops.copy()
  1301. # Use FancyArrowPatch if self.arrowprops has "arrowstyle" key.
  1302. # adjust the starting point of the arrow relative to
  1303. # the textbox.
  1304. # TODO : Rotation needs to be accounted.
  1305. relpos = self._arrow_relpos
  1306. ox0 = bbox.x0 + bbox.width * relpos[0]
  1307. oy0 = bbox.y0 + bbox.height * relpos[1]
  1308. # The arrow will be drawn from (ox0, oy0) to (ox1,
  1309. # oy1). It will be first clipped by patchA and patchB.
  1310. # Then it will be shrunk by shrinkA and shrinkB
  1311. # (in points). If patch A is not set, self.bbox_patch
  1312. # is used.
  1313. self.arrow_patch.set_positions((ox0, oy0), (ox1, oy1))
  1314. fs = self.prop.get_size_in_points()
  1315. mutation_scale = d.pop("mutation_scale", fs)
  1316. mutation_scale = renderer.points_to_pixels(mutation_scale)
  1317. self.arrow_patch.set_mutation_scale(mutation_scale)
  1318. patchA = d.pop("patchA", self.patch)
  1319. self.arrow_patch.set_patchA(patchA)
  1320. def draw(self, renderer):
  1321. # docstring inherited
  1322. if renderer is not None:
  1323. self._renderer = renderer
  1324. if not self.get_visible() or not self._check_xy(renderer):
  1325. return
  1326. self.update_positions(renderer)
  1327. if self.arrow_patch is not None:
  1328. if self.arrow_patch.figure is None and self.figure is not None:
  1329. self.arrow_patch.figure = self.figure
  1330. self.arrow_patch.draw(renderer)
  1331. self.patch.draw(renderer)
  1332. self.offsetbox.draw(renderer)
  1333. self.stale = False
  1334. class DraggableBase:
  1335. """
  1336. Helper base class for a draggable artist (legend, offsetbox).
  1337. Derived classes must override the following methods::
  1338. def save_offset(self):
  1339. '''
  1340. Called when the object is picked for dragging; should save the
  1341. reference position of the artist.
  1342. '''
  1343. def update_offset(self, dx, dy):
  1344. '''
  1345. Called during the dragging; (*dx*, *dy*) is the pixel offset from
  1346. the point where the mouse drag started.
  1347. '''
  1348. Optionally, you may override the following method::
  1349. def finalize_offset(self):
  1350. '''Called when the mouse is released.'''
  1351. In the current implementation of `.DraggableLegend` and
  1352. `DraggableAnnotation`, `update_offset` places the artists in display
  1353. coordinates, and `finalize_offset` recalculates their position in axes
  1354. coordinate and set a relevant attribute.
  1355. """
  1356. def __init__(self, ref_artist, use_blit=False):
  1357. self.ref_artist = ref_artist
  1358. self.got_artist = False
  1359. self.canvas = self.ref_artist.figure.canvas
  1360. self._use_blit = use_blit and self.canvas.supports_blit
  1361. c2 = self.canvas.mpl_connect('pick_event', self.on_pick)
  1362. c3 = self.canvas.mpl_connect('button_release_event', self.on_release)
  1363. if not ref_artist.pickable():
  1364. ref_artist.set_picker(True)
  1365. overridden_picker = cbook._deprecate_method_override(
  1366. __class__.artist_picker, self, since="3.3",
  1367. addendum="Directly set the artist's picker if desired.")
  1368. if overridden_picker is not None:
  1369. ref_artist.set_picker(overridden_picker)
  1370. self.cids = [c2, c3]
  1371. def on_motion(self, evt):
  1372. if self._check_still_parented() and self.got_artist:
  1373. dx = evt.x - self.mouse_x
  1374. dy = evt.y - self.mouse_y
  1375. self.update_offset(dx, dy)
  1376. if self._use_blit:
  1377. self.canvas.restore_region(self.background)
  1378. self.ref_artist.draw(self.ref_artist.figure._cachedRenderer)
  1379. self.canvas.blit()
  1380. else:
  1381. self.canvas.draw()
  1382. @cbook.deprecated("3.3", alternative="self.on_motion")
  1383. def on_motion_blit(self, evt):
  1384. if self._check_still_parented() and self.got_artist:
  1385. dx = evt.x - self.mouse_x
  1386. dy = evt.y - self.mouse_y
  1387. self.update_offset(dx, dy)
  1388. self.canvas.restore_region(self.background)
  1389. self.ref_artist.draw(self.ref_artist.figure._cachedRenderer)
  1390. self.canvas.blit()
  1391. def on_pick(self, evt):
  1392. if self._check_still_parented() and evt.artist == self.ref_artist:
  1393. self.mouse_x = evt.mouseevent.x
  1394. self.mouse_y = evt.mouseevent.y
  1395. self.got_artist = True
  1396. if self._use_blit:
  1397. self.ref_artist.set_animated(True)
  1398. self.canvas.draw()
  1399. self.background = \
  1400. self.canvas.copy_from_bbox(self.ref_artist.figure.bbox)
  1401. self.ref_artist.draw(self.ref_artist.figure._cachedRenderer)
  1402. self.canvas.blit()
  1403. self._c1 = self.canvas.mpl_connect(
  1404. "motion_notify_event", self.on_motion)
  1405. self.save_offset()
  1406. def on_release(self, event):
  1407. if self._check_still_parented() and self.got_artist:
  1408. self.finalize_offset()
  1409. self.got_artist = False
  1410. self.canvas.mpl_disconnect(self._c1)
  1411. if self._use_blit:
  1412. self.ref_artist.set_animated(False)
  1413. def _check_still_parented(self):
  1414. if self.ref_artist.figure is None:
  1415. self.disconnect()
  1416. return False
  1417. else:
  1418. return True
  1419. def disconnect(self):
  1420. """Disconnect the callbacks."""
  1421. for cid in self.cids:
  1422. self.canvas.mpl_disconnect(cid)
  1423. try:
  1424. c1 = self._c1
  1425. except AttributeError:
  1426. pass
  1427. else:
  1428. self.canvas.mpl_disconnect(c1)
  1429. @cbook.deprecated("3.3", alternative="self.ref_artist.contains")
  1430. def artist_picker(self, artist, evt):
  1431. return self.ref_artist.contains(evt)
  1432. def save_offset(self):
  1433. pass
  1434. def update_offset(self, dx, dy):
  1435. pass
  1436. def finalize_offset(self):
  1437. pass
  1438. class DraggableOffsetBox(DraggableBase):
  1439. def __init__(self, ref_artist, offsetbox, use_blit=False):
  1440. DraggableBase.__init__(self, ref_artist, use_blit=use_blit)
  1441. self.offsetbox = offsetbox
  1442. def save_offset(self):
  1443. offsetbox = self.offsetbox
  1444. renderer = offsetbox.figure._cachedRenderer
  1445. w, h, xd, yd = offsetbox.get_extent(renderer)
  1446. offset = offsetbox.get_offset(w, h, xd, yd, renderer)
  1447. self.offsetbox_x, self.offsetbox_y = offset
  1448. self.offsetbox.set_offset(offset)
  1449. def update_offset(self, dx, dy):
  1450. loc_in_canvas = self.offsetbox_x + dx, self.offsetbox_y + dy
  1451. self.offsetbox.set_offset(loc_in_canvas)
  1452. def get_loc_in_canvas(self):
  1453. offsetbox = self.offsetbox
  1454. renderer = offsetbox.figure._cachedRenderer
  1455. w, h, xd, yd = offsetbox.get_extent(renderer)
  1456. ox, oy = offsetbox._offset
  1457. loc_in_canvas = (ox - xd, oy - yd)
  1458. return loc_in_canvas
  1459. class DraggableAnnotation(DraggableBase):
  1460. def __init__(self, annotation, use_blit=False):
  1461. DraggableBase.__init__(self, annotation, use_blit=use_blit)
  1462. self.annotation = annotation
  1463. def save_offset(self):
  1464. ann = self.annotation
  1465. self.ox, self.oy = ann.get_transform().transform(ann.xyann)
  1466. def update_offset(self, dx, dy):
  1467. ann = self.annotation
  1468. ann.xyann = ann.get_transform().inverted().transform(
  1469. (self.ox + dx, self.oy + dy))