You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1539 lines
48 KiB

4 years ago
  1. from collections import OrderedDict, namedtuple
  2. from functools import wraps
  3. import inspect
  4. import re
  5. import warnings
  6. import numpy as np
  7. import matplotlib
  8. from . import cbook, docstring, rcParams
  9. from .path import Path
  10. from .transforms import (Bbox, IdentityTransform, Transform, TransformedBbox,
  11. TransformedPatchPath, TransformedPath)
  12. # Note, matplotlib artists use the doc strings for set and get
  13. # methods to enable the introspection methods of setp and getp. Every
  14. # set_* method should have a docstring containing the line
  15. #
  16. # ACCEPTS: [ legal | values ]
  17. #
  18. # and aliases for setters and getters should have a docstring that
  19. # starts with 'alias for ', as in 'alias for set_somemethod'
  20. #
  21. # You may wonder why we use so much boiler-plate manually defining the
  22. # set_alias and get_alias functions, rather than using some clever
  23. # python trick. The answer is that I need to be able to manipulate
  24. # the docstring, and there is no clever way to do that in python 2.2,
  25. # as far as I can see - see
  26. #
  27. # https://mail.python.org/pipermail/python-list/2004-October/242925.html
  28. def allow_rasterization(draw):
  29. """
  30. Decorator for Artist.draw method. Provides routines
  31. that run before and after the draw call. The before and after functions
  32. are useful for changing artist-dependent renderer attributes or making
  33. other setup function calls, such as starting and flushing a mixed-mode
  34. renderer.
  35. """
  36. # the axes class has a second argument inframe for its draw method.
  37. @wraps(draw)
  38. def draw_wrapper(artist, renderer, *args, **kwargs):
  39. try:
  40. if artist.get_rasterized():
  41. renderer.start_rasterizing()
  42. if artist.get_agg_filter() is not None:
  43. renderer.start_filter()
  44. return draw(artist, renderer, *args, **kwargs)
  45. finally:
  46. if artist.get_agg_filter() is not None:
  47. renderer.stop_filter(artist.get_agg_filter())
  48. if artist.get_rasterized():
  49. renderer.stop_rasterizing()
  50. draw_wrapper._supports_rasterization = True
  51. return draw_wrapper
  52. def _stale_axes_callback(self, val):
  53. if self.axes:
  54. self.axes.stale = val
  55. _XYPair = namedtuple("_XYPair", "x y")
  56. class Artist(object):
  57. """
  58. Abstract base class for someone who renders into a
  59. :class:`FigureCanvas`.
  60. """
  61. aname = 'Artist'
  62. zorder = 0
  63. # order of precedence when bulk setting/updating properties
  64. # via update. The keys should be property names and the values
  65. # integers
  66. _prop_order = dict(color=-1)
  67. def __init__(self):
  68. self._stale = True
  69. self.stale_callback = None
  70. self._axes = None
  71. self.figure = None
  72. self._transform = None
  73. self._transformSet = False
  74. self._visible = True
  75. self._animated = False
  76. self._alpha = None
  77. self.clipbox = None
  78. self._clippath = None
  79. self._clipon = True
  80. self._label = ''
  81. self._picker = None
  82. self._contains = None
  83. self._rasterized = None
  84. self._agg_filter = None
  85. self._mouseover = False
  86. self.eventson = False # fire events only if eventson
  87. self._oid = 0 # an observer id
  88. self._propobservers = {} # a dict from oids to funcs
  89. try:
  90. self.axes = None
  91. except AttributeError:
  92. # Handle self.axes as a read-only property, as in Figure.
  93. pass
  94. self._remove_method = None
  95. self._url = None
  96. self._gid = None
  97. self._snap = None
  98. self._sketch = rcParams['path.sketch']
  99. self._path_effects = rcParams['path.effects']
  100. self._sticky_edges = _XYPair([], [])
  101. self._in_layout = True
  102. def __getstate__(self):
  103. d = self.__dict__.copy()
  104. # remove the unpicklable remove method, this will get re-added on load
  105. # (by the axes) if the artist lives on an axes.
  106. d['stale_callback'] = None
  107. return d
  108. def remove(self):
  109. """
  110. Remove the artist from the figure if possible. The effect
  111. will not be visible until the figure is redrawn, e.g., with
  112. :meth:`matplotlib.axes.Axes.draw_idle`. Call
  113. :meth:`matplotlib.axes.Axes.relim` to update the axes limits
  114. if desired.
  115. Note: :meth:`~matplotlib.axes.Axes.relim` will not see
  116. collections even if the collection was added to axes with
  117. *autolim* = True.
  118. Note: there is no support for removing the artist's legend entry.
  119. """
  120. # There is no method to set the callback. Instead the parent should
  121. # set the _remove_method attribute directly. This would be a
  122. # protected attribute if Python supported that sort of thing. The
  123. # callback has one parameter, which is the child to be removed.
  124. if self._remove_method is not None:
  125. self._remove_method(self)
  126. # clear stale callback
  127. self.stale_callback = None
  128. _ax_flag = False
  129. if hasattr(self, 'axes') and self.axes:
  130. # remove from the mouse hit list
  131. self.axes._mouseover_set.discard(self)
  132. # mark the axes as stale
  133. self.axes.stale = True
  134. # decouple the artist from the axes
  135. self.axes = None
  136. _ax_flag = True
  137. if self.figure:
  138. self.figure = None
  139. if not _ax_flag:
  140. self.figure = True
  141. else:
  142. raise NotImplementedError('cannot remove artist')
  143. # TODO: the fix for the collections relim problem is to move the
  144. # limits calculation into the artist itself, including the property of
  145. # whether or not the artist should affect the limits. Then there will
  146. # be no distinction between axes.add_line, axes.add_patch, etc.
  147. # TODO: add legend support
  148. def have_units(self):
  149. 'Return *True* if units are set on the *x* or *y* axes'
  150. ax = self.axes
  151. if ax is None or ax.xaxis is None:
  152. return False
  153. return ax.xaxis.have_units() or ax.yaxis.have_units()
  154. def convert_xunits(self, x):
  155. """For artists in an axes, if the xaxis has units support,
  156. convert *x* using xaxis unit type
  157. """
  158. ax = getattr(self, 'axes', None)
  159. if ax is None or ax.xaxis is None:
  160. return x
  161. return ax.xaxis.convert_units(x)
  162. def convert_yunits(self, y):
  163. """For artists in an axes, if the yaxis has units support,
  164. convert *y* using yaxis unit type
  165. """
  166. ax = getattr(self, 'axes', None)
  167. if ax is None or ax.yaxis is None:
  168. return y
  169. return ax.yaxis.convert_units(y)
  170. @property
  171. def axes(self):
  172. """
  173. The :class:`~matplotlib.axes.Axes` instance the artist
  174. resides in, or *None*.
  175. """
  176. return self._axes
  177. @axes.setter
  178. def axes(self, new_axes):
  179. if (new_axes is not None and self._axes is not None
  180. and new_axes != self._axes):
  181. raise ValueError("Can not reset the axes. You are probably "
  182. "trying to re-use an artist in more than one "
  183. "Axes which is not supported")
  184. self._axes = new_axes
  185. if new_axes is not None and new_axes is not self:
  186. self.stale_callback = _stale_axes_callback
  187. return new_axes
  188. @property
  189. def stale(self):
  190. """
  191. If the artist is 'stale' and needs to be re-drawn for the output to
  192. match the internal state of the artist.
  193. """
  194. return self._stale
  195. @stale.setter
  196. def stale(self, val):
  197. self._stale = val
  198. # if the artist is animated it does not take normal part in the
  199. # draw stack and is not expected to be drawn as part of the normal
  200. # draw loop (when not saving) so do not propagate this change
  201. if self.get_animated():
  202. return
  203. if val and self.stale_callback is not None:
  204. self.stale_callback(self, val)
  205. def get_window_extent(self, renderer):
  206. """
  207. Get the axes bounding box in display space.
  208. Subclasses should override for inclusion in the bounding box
  209. "tight" calculation. Default is to return an empty bounding
  210. box at 0, 0.
  211. Be careful when using this function, the results will not update
  212. if the artist window extent of the artist changes. The extent
  213. can change due to any changes in the transform stack, such as
  214. changing the axes limits, the figure size, or the canvas used
  215. (as is done when saving a figure). This can lead to unexpected
  216. behavior where interactive figures will look fine on the screen,
  217. but will save incorrectly.
  218. """
  219. return Bbox([[0, 0], [0, 0]])
  220. def get_tightbbox(self, renderer):
  221. """
  222. Like `Artist.get_window_extent`, but includes any clipping.
  223. Parameters
  224. ----------
  225. renderer : `.RendererBase` instance
  226. renderer that will be used to draw the figures (i.e.
  227. ``fig.canvas.get_renderer()``)
  228. Returns
  229. -------
  230. bbox : `.BboxBase`
  231. containing the bounding box (in figure pixel co-ordinates).
  232. """
  233. bbox = self.get_window_extent(renderer)
  234. if self.get_clip_on():
  235. clip_box = self.get_clip_box()
  236. if clip_box is not None:
  237. bbox = Bbox.intersection(bbox, clip_box)
  238. clip_path = self.get_clip_path()
  239. if clip_path is not None and bbox is not None:
  240. clip_path = clip_path.get_fully_transformed_path()
  241. bbox = Bbox.intersection(bbox, clip_path.get_extents())
  242. return bbox
  243. def add_callback(self, func):
  244. """
  245. Adds a callback function that will be called whenever one of
  246. the :class:`Artist`'s properties changes.
  247. Returns an *id* that is useful for removing the callback with
  248. :meth:`remove_callback` later.
  249. """
  250. oid = self._oid
  251. self._propobservers[oid] = func
  252. self._oid += 1
  253. return oid
  254. def remove_callback(self, oid):
  255. """
  256. Remove a callback based on its *id*.
  257. .. seealso::
  258. :meth:`add_callback`
  259. For adding callbacks
  260. """
  261. try:
  262. del self._propobservers[oid]
  263. except KeyError:
  264. pass
  265. def pchanged(self):
  266. """
  267. Fire an event when property changed, calling all of the
  268. registered callbacks.
  269. """
  270. for oid, func in self._propobservers.items():
  271. func(self)
  272. def is_transform_set(self):
  273. """
  274. Returns *True* if :class:`Artist` has a transform explicitly
  275. set.
  276. """
  277. return self._transformSet
  278. def set_transform(self, t):
  279. """
  280. Set the artist transform.
  281. Parameters
  282. ----------
  283. t : `.Transform`
  284. """
  285. self._transform = t
  286. self._transformSet = True
  287. self.pchanged()
  288. self.stale = True
  289. def get_transform(self):
  290. """
  291. Return the :class:`~matplotlib.transforms.Transform`
  292. instance used by this artist.
  293. """
  294. if self._transform is None:
  295. self._transform = IdentityTransform()
  296. elif (not isinstance(self._transform, Transform)
  297. and hasattr(self._transform, '_as_mpl_transform')):
  298. self._transform = self._transform._as_mpl_transform(self.axes)
  299. return self._transform
  300. @cbook.deprecated("2.2")
  301. def hitlist(self, event):
  302. """
  303. List the children of the artist which contain the mouse event *event*.
  304. """
  305. L = []
  306. try:
  307. hascursor, info = self.contains(event)
  308. if hascursor:
  309. L.append(self)
  310. except:
  311. import traceback
  312. traceback.print_exc()
  313. print("while checking", self.__class__)
  314. for a in self.get_children():
  315. L.extend(a.hitlist(event))
  316. return L
  317. def get_children(self):
  318. """
  319. Return a list of the child :class:`Artist`s this
  320. :class:`Artist` contains.
  321. """
  322. return []
  323. def contains(self, mouseevent):
  324. """Test whether the artist contains the mouse event.
  325. Returns the truth value and a dictionary of artist specific details of
  326. selection, such as which points are contained in the pick radius. See
  327. individual artists for details.
  328. """
  329. if callable(self._contains):
  330. return self._contains(self, mouseevent)
  331. warnings.warn("'%s' needs 'contains' method" % self.__class__.__name__)
  332. return False, {}
  333. def set_contains(self, picker):
  334. """
  335. Replace the contains test used by this artist. The new picker
  336. should be a callable function which determines whether the
  337. artist is hit by the mouse event::
  338. hit, props = picker(artist, mouseevent)
  339. If the mouse event is over the artist, return *hit* = *True*
  340. and *props* is a dictionary of properties you want returned
  341. with the contains test.
  342. Parameters
  343. ----------
  344. picker : callable
  345. """
  346. self._contains = picker
  347. def get_contains(self):
  348. """
  349. Return the _contains test used by the artist, or *None* for default.
  350. """
  351. return self._contains
  352. def pickable(self):
  353. 'Return *True* if :class:`Artist` is pickable.'
  354. return (self.figure is not None and
  355. self.figure.canvas is not None and
  356. self._picker is not None)
  357. def pick(self, mouseevent):
  358. """
  359. Process pick event
  360. each child artist will fire a pick event if *mouseevent* is over
  361. the artist and the artist has picker set
  362. """
  363. # Pick self
  364. if self.pickable():
  365. picker = self.get_picker()
  366. if callable(picker):
  367. inside, prop = picker(self, mouseevent)
  368. else:
  369. inside, prop = self.contains(mouseevent)
  370. if inside:
  371. self.figure.canvas.pick_event(mouseevent, self, **prop)
  372. # Pick children
  373. for a in self.get_children():
  374. # make sure the event happened in the same axes
  375. ax = getattr(a, 'axes', None)
  376. if (mouseevent.inaxes is None or ax is None
  377. or mouseevent.inaxes == ax):
  378. # we need to check if mouseevent.inaxes is None
  379. # because some objects associated with an axes (e.g., a
  380. # tick label) can be outside the bounding box of the
  381. # axes and inaxes will be None
  382. # also check that ax is None so that it traverse objects
  383. # which do no have an axes property but children might
  384. a.pick(mouseevent)
  385. def set_picker(self, picker):
  386. """
  387. Set the epsilon for picking used by this artist
  388. *picker* can be one of the following:
  389. * *None*: picking is disabled for this artist (default)
  390. * A boolean: if *True* then picking will be enabled and the
  391. artist will fire a pick event if the mouse event is over
  392. the artist
  393. * A float: if picker is a number it is interpreted as an
  394. epsilon tolerance in points and the artist will fire
  395. off an event if it's data is within epsilon of the mouse
  396. event. For some artists like lines and patch collections,
  397. the artist may provide additional data to the pick event
  398. that is generated, e.g., the indices of the data within
  399. epsilon of the pick event
  400. * A function: if picker is callable, it is a user supplied
  401. function which determines whether the artist is hit by the
  402. mouse event::
  403. hit, props = picker(artist, mouseevent)
  404. to determine the hit test. if the mouse event is over the
  405. artist, return *hit=True* and props is a dictionary of
  406. properties you want added to the PickEvent attributes.
  407. Parameters
  408. ----------
  409. picker : None or bool or float or callable
  410. """
  411. self._picker = picker
  412. def get_picker(self):
  413. """Return the picker object used by this artist."""
  414. return self._picker
  415. @cbook.deprecated("2.2", "artist.figure is not None")
  416. def is_figure_set(self):
  417. """Returns whether the artist is assigned to a `.Figure`."""
  418. return self.figure is not None
  419. def get_url(self):
  420. """Returns the url."""
  421. return self._url
  422. def set_url(self, url):
  423. """
  424. Sets the url for the artist.
  425. Parameters
  426. ----------
  427. url : str
  428. """
  429. self._url = url
  430. def get_gid(self):
  431. """Returns the group id."""
  432. return self._gid
  433. def set_gid(self, gid):
  434. """
  435. Sets the (group) id for the artist.
  436. Parameters
  437. ----------
  438. gid : str
  439. """
  440. self._gid = gid
  441. def get_snap(self):
  442. """
  443. Returns the snap setting which may be:
  444. * True: snap vertices to the nearest pixel center
  445. * False: leave vertices as-is
  446. * None: (auto) If the path contains only rectilinear line
  447. segments, round to the nearest pixel center
  448. Only supported by the Agg and MacOSX backends.
  449. """
  450. if rcParams['path.snap']:
  451. return self._snap
  452. else:
  453. return False
  454. def set_snap(self, snap):
  455. """
  456. Sets the snap setting which may be:
  457. * True: snap vertices to the nearest pixel center
  458. * False: leave vertices as-is
  459. * None: (auto) If the path contains only rectilinear line
  460. segments, round to the nearest pixel center
  461. Only supported by the Agg and MacOSX backends.
  462. Parameters
  463. ----------
  464. snap : bool or None
  465. """
  466. self._snap = snap
  467. self.stale = True
  468. def get_sketch_params(self):
  469. """
  470. Returns the sketch parameters for the artist.
  471. Returns
  472. -------
  473. sketch_params : tuple or `None`
  474. A 3-tuple with the following elements:
  475. * `scale`: The amplitude of the wiggle perpendicular to the
  476. source line.
  477. * `length`: The length of the wiggle along the line.
  478. * `randomness`: The scale factor by which the length is
  479. shrunken or expanded.
  480. May return `None` if no sketch parameters were set.
  481. """
  482. return self._sketch
  483. def set_sketch_params(self, scale=None, length=None, randomness=None):
  484. """
  485. Sets the sketch parameters.
  486. Parameters
  487. ----------
  488. scale : float, optional
  489. The amplitude of the wiggle perpendicular to the source
  490. line, in pixels. If scale is `None`, or not provided, no
  491. sketch filter will be provided.
  492. length : float, optional
  493. The length of the wiggle along the line, in pixels
  494. (default 128.0)
  495. randomness : float, optional
  496. The scale factor by which the length is shrunken or
  497. expanded (default 16.0)
  498. .. ACCEPTS: (scale: float, length: float, randomness: float)
  499. """
  500. if scale is None:
  501. self._sketch = None
  502. else:
  503. self._sketch = (scale, length or 128.0, randomness or 16.0)
  504. self.stale = True
  505. def set_path_effects(self, path_effects):
  506. """Set the path effects.
  507. Parameters
  508. ----------
  509. path_effects : `.AbstractPathEffect`
  510. """
  511. self._path_effects = path_effects
  512. self.stale = True
  513. def get_path_effects(self):
  514. return self._path_effects
  515. def get_figure(self):
  516. """Return the `.Figure` instance the artist belongs to."""
  517. return self.figure
  518. def set_figure(self, fig):
  519. """
  520. Set the `.Figure` instance the artist belongs to.
  521. Parameters
  522. ----------
  523. fig : `.Figure`
  524. """
  525. # if this is a no-op just return
  526. if self.figure is fig:
  527. return
  528. # if we currently have a figure (the case of both `self.figure`
  529. # and `fig` being none is taken care of above) we then user is
  530. # trying to change the figure an artist is associated with which
  531. # is not allowed for the same reason as adding the same instance
  532. # to more than one Axes
  533. if self.figure is not None:
  534. raise RuntimeError("Can not put single artist in "
  535. "more than one figure")
  536. self.figure = fig
  537. if self.figure and self.figure is not self:
  538. self.pchanged()
  539. self.stale = True
  540. def set_clip_box(self, clipbox):
  541. """
  542. Set the artist's clip `.Bbox`.
  543. Parameters
  544. ----------
  545. clipbox : `.Bbox`
  546. """
  547. self.clipbox = clipbox
  548. self.pchanged()
  549. self.stale = True
  550. def set_clip_path(self, path, transform=None):
  551. """
  552. Set the artist's clip path, which may be:
  553. - a :class:`~matplotlib.patches.Patch` (or subclass) instance; or
  554. - a :class:`~matplotlib.path.Path` instance, in which case a
  555. :class:`~matplotlib.transforms.Transform` instance, which will be
  556. applied to the path before using it for clipping, must be provided;
  557. or
  558. - ``None``, to remove a previously set clipping path.
  559. For efficiency, if the path happens to be an axis-aligned rectangle,
  560. this method will set the clipping box to the corresponding rectangle
  561. and set the clipping path to ``None``.
  562. ACCEPTS: [(`~matplotlib.path.Path`, `.Transform`) | `.Patch` | None]
  563. """
  564. from matplotlib.patches import Patch, Rectangle
  565. success = False
  566. if transform is None:
  567. if isinstance(path, Rectangle):
  568. self.clipbox = TransformedBbox(Bbox.unit(),
  569. path.get_transform())
  570. self._clippath = None
  571. success = True
  572. elif isinstance(path, Patch):
  573. self._clippath = TransformedPatchPath(path)
  574. success = True
  575. elif isinstance(path, tuple):
  576. path, transform = path
  577. if path is None:
  578. self._clippath = None
  579. success = True
  580. elif isinstance(path, Path):
  581. self._clippath = TransformedPath(path, transform)
  582. success = True
  583. elif isinstance(path, TransformedPatchPath):
  584. self._clippath = path
  585. success = True
  586. elif isinstance(path, TransformedPath):
  587. self._clippath = path
  588. success = True
  589. if not success:
  590. raise TypeError(
  591. "Invalid arguments to set_clip_path, of type {} and {}"
  592. .format(type(path).__name__, type(transform).__name__))
  593. # This may result in the callbacks being hit twice, but guarantees they
  594. # will be hit at least once.
  595. self.pchanged()
  596. self.stale = True
  597. def get_alpha(self):
  598. """
  599. Return the alpha value used for blending - not supported on all
  600. backends
  601. """
  602. return self._alpha
  603. def get_visible(self):
  604. "Return the artist's visiblity"
  605. return self._visible
  606. def get_animated(self):
  607. "Return the artist's animated state"
  608. return self._animated
  609. def get_in_layout(self):
  610. """
  611. Return boolean flag, ``True`` if artist is included in layout
  612. calculations.
  613. E.g. :doc:`/tutorials/intermediate/constrainedlayout_guide`,
  614. `.Figure.tight_layout()`, and
  615. ``fig.savefig(fname, bbox_inches='tight')``.
  616. """
  617. return self._in_layout
  618. def get_clip_on(self):
  619. 'Return whether artist uses clipping'
  620. return self._clipon
  621. def get_clip_box(self):
  622. 'Return artist clipbox'
  623. return self.clipbox
  624. def get_clip_path(self):
  625. 'Return artist clip path'
  626. return self._clippath
  627. def get_transformed_clip_path_and_affine(self):
  628. '''
  629. Return the clip path with the non-affine part of its
  630. transformation applied, and the remaining affine part of its
  631. transformation.
  632. '''
  633. if self._clippath is not None:
  634. return self._clippath.get_transformed_path_and_affine()
  635. return None, None
  636. def set_clip_on(self, b):
  637. """
  638. Set whether artist uses clipping.
  639. When False artists will be visible out side of the axes which
  640. can lead to unexpected results.
  641. Parameters
  642. ----------
  643. b : bool
  644. """
  645. self._clipon = b
  646. # This may result in the callbacks being hit twice, but ensures they
  647. # are hit at least once
  648. self.pchanged()
  649. self.stale = True
  650. def _set_gc_clip(self, gc):
  651. 'Set the clip properly for the gc'
  652. if self._clipon:
  653. if self.clipbox is not None:
  654. gc.set_clip_rectangle(self.clipbox)
  655. gc.set_clip_path(self._clippath)
  656. else:
  657. gc.set_clip_rectangle(None)
  658. gc.set_clip_path(None)
  659. def get_rasterized(self):
  660. """Return whether the artist is to be rasterized."""
  661. return self._rasterized
  662. def set_rasterized(self, rasterized):
  663. """
  664. Force rasterized (bitmap) drawing in vector backend output.
  665. Defaults to None, which implies the backend's default behavior.
  666. Parameters
  667. ----------
  668. rasterized : bool or None
  669. """
  670. if rasterized and not hasattr(self.draw, "_supports_rasterization"):
  671. warnings.warn("Rasterization of '%s' will be ignored" % self)
  672. self._rasterized = rasterized
  673. def get_agg_filter(self):
  674. """Return filter function to be used for agg filter."""
  675. return self._agg_filter
  676. def set_agg_filter(self, filter_func):
  677. """Set the agg filter.
  678. Parameters
  679. ----------
  680. filter_func : callable
  681. A filter function, which takes a (m, n, 3) float array and a dpi
  682. value, and returns a (m, n, 3) array.
  683. .. ACCEPTS: a filter function, which takes a (m, n, 3) float array
  684. and a dpi value, and returns a (m, n, 3) array
  685. """
  686. self._agg_filter = filter_func
  687. self.stale = True
  688. def draw(self, renderer, *args, **kwargs):
  689. 'Derived classes drawing method'
  690. if not self.get_visible():
  691. return
  692. self.stale = False
  693. def set_alpha(self, alpha):
  694. """
  695. Set the alpha value used for blending - not supported on all backends.
  696. Parameters
  697. ----------
  698. alpha : float
  699. """
  700. self._alpha = alpha
  701. self.pchanged()
  702. self.stale = True
  703. def set_visible(self, b):
  704. """
  705. Set the artist's visibility.
  706. Parameters
  707. ----------
  708. b : bool
  709. """
  710. self._visible = b
  711. self.pchanged()
  712. self.stale = True
  713. def set_animated(self, b):
  714. """
  715. Set the artist's animation state.
  716. Parameters
  717. ----------
  718. b : bool
  719. """
  720. if self._animated != b:
  721. self._animated = b
  722. self.pchanged()
  723. def set_in_layout(self, in_layout):
  724. """
  725. Set if artist is to be included in layout calculations,
  726. E.g. :doc:`/tutorials/intermediate/constrainedlayout_guide`,
  727. `.Figure.tight_layout()`, and
  728. ``fig.savefig(fname, bbox_inches='tight')``.
  729. Parameters
  730. ----------
  731. in_layout : bool
  732. """
  733. self._in_layout = in_layout
  734. def update(self, props):
  735. """
  736. Update this artist's properties from the dictionary *prop*.
  737. """
  738. def _update_property(self, k, v):
  739. """Sorting out how to update property (setter or setattr).
  740. Parameters
  741. ----------
  742. k : str
  743. The name of property to update
  744. v : obj
  745. The value to assign to the property
  746. Returns
  747. -------
  748. ret : obj or None
  749. If using a `set_*` method return it's return, else None.
  750. """
  751. k = k.lower()
  752. # white list attributes we want to be able to update through
  753. # art.update, art.set, setp
  754. if k in {'axes'}:
  755. return setattr(self, k, v)
  756. else:
  757. func = getattr(self, 'set_' + k, None)
  758. if not callable(func):
  759. raise AttributeError('Unknown property %s' % k)
  760. return func(v)
  761. with cbook._setattr_cm(self, eventson=False):
  762. ret = [_update_property(self, k, v) for k, v in props.items()]
  763. if len(ret):
  764. self.pchanged()
  765. self.stale = True
  766. return ret
  767. def get_label(self):
  768. """Get the label used for this artist in the legend."""
  769. return self._label
  770. def set_label(self, s):
  771. """
  772. Set the label to *s* for auto legend.
  773. Parameters
  774. ----------
  775. s : object
  776. *s* will be converted to a string by calling `str`.
  777. """
  778. if s is not None:
  779. self._label = str(s)
  780. else:
  781. self._label = None
  782. self.pchanged()
  783. self.stale = True
  784. def get_zorder(self):
  785. """Return the artist's zorder."""
  786. return self.zorder
  787. def set_zorder(self, level):
  788. """
  789. Set the zorder for the artist. Artists with lower zorder
  790. values are drawn first.
  791. Parameters
  792. ----------
  793. level : float
  794. """
  795. if level is None:
  796. level = self.__class__.zorder
  797. self.zorder = level
  798. self.pchanged()
  799. self.stale = True
  800. @property
  801. def sticky_edges(self):
  802. """
  803. `x` and `y` sticky edge lists.
  804. When performing autoscaling, if a data limit coincides with a value in
  805. the corresponding sticky_edges list, then no margin will be added--the
  806. view limit "sticks" to the edge. A typical usecase is histograms,
  807. where one usually expects no margin on the bottom edge (0) of the
  808. histogram.
  809. This attribute cannot be assigned to; however, the `x` and `y` lists
  810. can be modified in place as needed.
  811. Examples
  812. --------
  813. >>> artist.sticky_edges.x[:] = (xmin, xmax)
  814. >>> artist.sticky_edges.y[:] = (ymin, ymax)
  815. """
  816. return self._sticky_edges
  817. def update_from(self, other):
  818. 'Copy properties from *other* to *self*.'
  819. self._transform = other._transform
  820. self._transformSet = other._transformSet
  821. self._visible = other._visible
  822. self._alpha = other._alpha
  823. self.clipbox = other.clipbox
  824. self._clipon = other._clipon
  825. self._clippath = other._clippath
  826. self._label = other._label
  827. self._sketch = other._sketch
  828. self._path_effects = other._path_effects
  829. self.sticky_edges.x[:] = other.sticky_edges.x[:]
  830. self.sticky_edges.y[:] = other.sticky_edges.y[:]
  831. self.pchanged()
  832. self.stale = True
  833. def properties(self):
  834. """
  835. return a dictionary mapping property name -> value for all Artist props
  836. """
  837. return ArtistInspector(self).properties()
  838. def set(self, **kwargs):
  839. """A property batch setter. Pass *kwargs* to set properties.
  840. """
  841. props = OrderedDict(
  842. sorted(kwargs.items(), reverse=True,
  843. key=lambda x: (self._prop_order.get(x[0], 0), x[0])))
  844. return self.update(props)
  845. def findobj(self, match=None, include_self=True):
  846. """
  847. Find artist objects.
  848. Recursively find all :class:`~matplotlib.artist.Artist` instances
  849. contained in self.
  850. *match* can be
  851. - None: return all objects contained in artist.
  852. - function with signature ``boolean = match(artist)``
  853. used to filter matches
  854. - class instance: e.g., Line2D. Only return artists of class type.
  855. If *include_self* is True (default), include self in the list to be
  856. checked for a match.
  857. """
  858. if match is None: # always return True
  859. def matchfunc(x):
  860. return True
  861. elif isinstance(match, type) and issubclass(match, Artist):
  862. def matchfunc(x):
  863. return isinstance(x, match)
  864. elif callable(match):
  865. matchfunc = match
  866. else:
  867. raise ValueError('match must be None, a matplotlib.artist.Artist '
  868. 'subclass, or a callable')
  869. artists = sum([c.findobj(matchfunc) for c in self.get_children()], [])
  870. if include_self and matchfunc(self):
  871. artists.append(self)
  872. return artists
  873. def get_cursor_data(self, event):
  874. """
  875. Get the cursor data for a given event.
  876. """
  877. return None
  878. def format_cursor_data(self, data):
  879. """
  880. Return *cursor data* string formatted.
  881. """
  882. try:
  883. data[0]
  884. except (TypeError, IndexError):
  885. data = [data]
  886. data_str = ', '.join('{:0.3g}'.format(item) for item in data if
  887. isinstance(item, (np.floating, np.integer, int, float)))
  888. return "[" + data_str + "]"
  889. @property
  890. def mouseover(self):
  891. return self._mouseover
  892. @mouseover.setter
  893. def mouseover(self, val):
  894. val = bool(val)
  895. self._mouseover = val
  896. ax = self.axes
  897. if ax:
  898. if val:
  899. ax._mouseover_set.add(self)
  900. else:
  901. ax._mouseover_set.discard(self)
  902. class ArtistInspector(object):
  903. """
  904. A helper class to inspect an `~matplotlib.artist.Artist` and return
  905. information about its settable properties and their current values.
  906. """
  907. def __init__(self, o):
  908. r"""
  909. Initialize the artist inspector with an `Artist` or an iterable of
  910. `Artist`\s. If an iterable is used, we assume it is a homogeneous
  911. sequence (all `Artists` are of the same type) and it is your
  912. responsibility to make sure this is so.
  913. """
  914. if not isinstance(o, Artist):
  915. if cbook.iterable(o):
  916. o = list(o)
  917. if len(o):
  918. o = o[0]
  919. self.oorig = o
  920. if not inspect.isclass(o):
  921. o = type(o)
  922. self.o = o
  923. self.aliasd = self.get_aliases()
  924. def get_aliases(self):
  925. """
  926. Get a dict mapping property fullnames to sets of aliases for each alias
  927. in the :class:`~matplotlib.artist.ArtistInspector`.
  928. e.g., for lines::
  929. {'markerfacecolor': {'mfc'},
  930. 'linewidth' : {'lw'},
  931. }
  932. """
  933. names = [name for name in dir(self.o)
  934. if name.startswith(('set_', 'get_'))
  935. and callable(getattr(self.o, name))]
  936. aliases = {}
  937. for name in names:
  938. func = getattr(self.o, name)
  939. if not self.is_alias(func):
  940. continue
  941. propname = re.search("`({}.*)`".format(name[:4]), # get_.*/set_.*
  942. func.__doc__).group(1)
  943. aliases.setdefault(propname, set()).add(name[4:])
  944. return aliases
  945. _get_valid_values_regex = re.compile(
  946. r"\n\s*(?:\.\.\s+)?ACCEPTS:\s*((?:.|\n)*?)(?:$|(?:\n\n))"
  947. )
  948. def get_valid_values(self, attr):
  949. """
  950. Get the legal arguments for the setter associated with *attr*.
  951. This is done by querying the docstring of the function *set_attr*
  952. for a line that begins with "ACCEPTS" or ".. ACCEPTS":
  953. e.g., for a line linestyle, return
  954. "[ ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'steps'`` | ``'None'``
  955. ]"
  956. """
  957. name = 'set_%s' % attr
  958. if not hasattr(self.o, name):
  959. raise AttributeError('%s has no function %s' % (self.o, name))
  960. func = getattr(self.o, name)
  961. docstring = func.__doc__
  962. if docstring is None:
  963. return 'unknown'
  964. if docstring.startswith('alias for '):
  965. return None
  966. match = self._get_valid_values_regex.search(docstring)
  967. if match is not None:
  968. return re.sub("\n *", " ", match.group(1))
  969. # Much faster than list(inspect.signature(func).parameters)[1],
  970. # although barely relevant wrt. matplotlib's total import time.
  971. param_name = func.__code__.co_varnames[1]
  972. match = re.search("(?m)^ *{} : (.+)".format(param_name), docstring)
  973. if match:
  974. return match.group(1)
  975. return 'unknown'
  976. def _get_setters_and_targets(self):
  977. """
  978. Get the attribute strings and a full path to where the setter
  979. is defined for all setters in an object.
  980. """
  981. setters = []
  982. for name in dir(self.o):
  983. if not name.startswith('set_'):
  984. continue
  985. func = getattr(self.o, name)
  986. if not callable(func):
  987. continue
  988. nargs = len(inspect.getfullargspec(func).args)
  989. if nargs < 2 or self.is_alias(func):
  990. continue
  991. source_class = self.o.__module__ + "." + self.o.__name__
  992. for cls in self.o.mro():
  993. if name in cls.__dict__:
  994. source_class = cls.__module__ + "." + cls.__name__
  995. break
  996. source_class = self._replace_path(source_class)
  997. setters.append((name[4:], source_class + "." + name))
  998. return setters
  999. def _replace_path(self, source_class):
  1000. """
  1001. Changes the full path to the public API path that is used
  1002. in sphinx. This is needed for links to work.
  1003. """
  1004. replace_dict = {'_base._AxesBase': 'Axes',
  1005. '_axes.Axes': 'Axes'}
  1006. for key, value in replace_dict.items():
  1007. source_class = source_class.replace(key, value)
  1008. return source_class
  1009. def get_setters(self):
  1010. """
  1011. Get the attribute strings with setters for object. e.g., for a line,
  1012. return ``['markerfacecolor', 'linewidth', ....]``.
  1013. """
  1014. return [prop for prop, target in self._get_setters_and_targets()]
  1015. def is_alias(self, o):
  1016. """
  1017. Return *True* if method object *o* is an alias for another
  1018. function.
  1019. """
  1020. ds = o.__doc__
  1021. if ds is None:
  1022. return False
  1023. return ds.startswith('alias for ')
  1024. def aliased_name(self, s):
  1025. """
  1026. return 'PROPNAME or alias' if *s* has an alias, else return
  1027. PROPNAME.
  1028. e.g., for the line markerfacecolor property, which has an
  1029. alias, return 'markerfacecolor or mfc' and for the transform
  1030. property, which does not, return 'transform'
  1031. """
  1032. if s in self.aliasd:
  1033. return s + ''.join([' or %s' % x
  1034. for x in sorted(self.aliasd[s])])
  1035. else:
  1036. return s
  1037. def aliased_name_rest(self, s, target):
  1038. """
  1039. return 'PROPNAME or alias' if *s* has an alias, else return
  1040. PROPNAME formatted for ReST
  1041. e.g., for the line markerfacecolor property, which has an
  1042. alias, return 'markerfacecolor or mfc' and for the transform
  1043. property, which does not, return 'transform'
  1044. """
  1045. if s in self.aliasd:
  1046. aliases = ''.join([' or %s' % x
  1047. for x in sorted(self.aliasd[s])])
  1048. else:
  1049. aliases = ''
  1050. return ':meth:`%s <%s>`%s' % (s, target, aliases)
  1051. def pprint_setters(self, prop=None, leadingspace=2):
  1052. """
  1053. If *prop* is *None*, return a list of strings of all settable
  1054. properties and their valid values.
  1055. If *prop* is not *None*, it is a valid property name and that
  1056. property will be returned as a string of property : valid
  1057. values.
  1058. """
  1059. if leadingspace:
  1060. pad = ' ' * leadingspace
  1061. else:
  1062. pad = ''
  1063. if prop is not None:
  1064. accepts = self.get_valid_values(prop)
  1065. return '%s%s: %s' % (pad, prop, accepts)
  1066. attrs = self._get_setters_and_targets()
  1067. attrs.sort()
  1068. lines = []
  1069. for prop, path in attrs:
  1070. accepts = self.get_valid_values(prop)
  1071. name = self.aliased_name(prop)
  1072. lines.append('%s%s: %s' % (pad, name, accepts))
  1073. return lines
  1074. def pprint_setters_rest(self, prop=None, leadingspace=4):
  1075. """
  1076. If *prop* is *None*, return a list of strings of all settable
  1077. properties and their valid values. Format the output for ReST
  1078. If *prop* is not *None*, it is a valid property name and that
  1079. property will be returned as a string of property : valid
  1080. values.
  1081. """
  1082. if leadingspace:
  1083. pad = ' ' * leadingspace
  1084. else:
  1085. pad = ''
  1086. if prop is not None:
  1087. accepts = self.get_valid_values(prop)
  1088. return '%s%s: %s' % (pad, prop, accepts)
  1089. attrs = self._get_setters_and_targets()
  1090. attrs.sort()
  1091. lines = []
  1092. ########
  1093. names = [self.aliased_name_rest(prop, target)
  1094. for prop, target in attrs]
  1095. accepts = [self.get_valid_values(prop) for prop, target in attrs]
  1096. col0_len = max(len(n) for n in names)
  1097. col1_len = max(len(a) for a in accepts)
  1098. lines.append('')
  1099. lines.append(pad + '.. table::')
  1100. lines.append(pad + ' :class: property-table')
  1101. pad += ' '
  1102. table_formatstr = pad + '=' * col0_len + ' ' + '=' * col1_len
  1103. lines.append('')
  1104. lines.append(table_formatstr)
  1105. lines.append(pad + 'Property'.ljust(col0_len + 3) +
  1106. 'Description'.ljust(col1_len))
  1107. lines.append(table_formatstr)
  1108. lines.extend([pad + n.ljust(col0_len + 3) + a.ljust(col1_len)
  1109. for n, a in zip(names, accepts)])
  1110. lines.append(table_formatstr)
  1111. lines.append('')
  1112. return lines
  1113. def properties(self):
  1114. """
  1115. return a dictionary mapping property name -> value
  1116. """
  1117. o = self.oorig
  1118. getters = [name for name in dir(o)
  1119. if name.startswith('get_') and callable(getattr(o, name))]
  1120. getters.sort()
  1121. d = dict()
  1122. for name in getters:
  1123. func = getattr(o, name)
  1124. if self.is_alias(func):
  1125. continue
  1126. try:
  1127. with warnings.catch_warnings():
  1128. warnings.simplefilter('ignore')
  1129. val = func()
  1130. except:
  1131. continue
  1132. else:
  1133. d[name[4:]] = val
  1134. return d
  1135. def pprint_getters(self):
  1136. """
  1137. Return the getters and actual values as list of strings.
  1138. """
  1139. lines = []
  1140. for name, val in sorted(self.properties().items()):
  1141. if getattr(val, 'shape', ()) != () and len(val) > 6:
  1142. s = str(val[:6]) + '...'
  1143. else:
  1144. s = str(val)
  1145. s = s.replace('\n', ' ')
  1146. if len(s) > 50:
  1147. s = s[:50] + '...'
  1148. name = self.aliased_name(name)
  1149. lines.append(' %s = %s' % (name, s))
  1150. return lines
  1151. def getp(obj, property=None):
  1152. """
  1153. Return the value of object's property. *property* is an optional string
  1154. for the property you want to return
  1155. Example usage::
  1156. getp(obj) # get all the object properties
  1157. getp(obj, 'linestyle') # get the linestyle property
  1158. *obj* is a :class:`Artist` instance, e.g.,
  1159. :class:`~matplotllib.lines.Line2D` or an instance of a
  1160. :class:`~matplotlib.axes.Axes` or :class:`matplotlib.text.Text`.
  1161. If the *property* is 'somename', this function returns
  1162. obj.get_somename()
  1163. :func:`getp` can be used to query all the gettable properties with
  1164. ``getp(obj)``. Many properties have aliases for shorter typing, e.g.
  1165. 'lw' is an alias for 'linewidth'. In the output, aliases and full
  1166. property names will be listed as:
  1167. property or alias = value
  1168. e.g.:
  1169. linewidth or lw = 2
  1170. """
  1171. if property is None:
  1172. insp = ArtistInspector(obj)
  1173. ret = insp.pprint_getters()
  1174. print('\n'.join(ret))
  1175. return
  1176. func = getattr(obj, 'get_' + property)
  1177. return func()
  1178. # alias
  1179. get = getp
  1180. def setp(obj, *args, **kwargs):
  1181. """
  1182. Set a property on an artist object.
  1183. matplotlib supports the use of :func:`setp` ("set property") and
  1184. :func:`getp` to set and get object properties, as well as to do
  1185. introspection on the object. For example, to set the linestyle of a
  1186. line to be dashed, you can do::
  1187. >>> line, = plot([1,2,3])
  1188. >>> setp(line, linestyle='--')
  1189. If you want to know the valid types of arguments, you can provide
  1190. the name of the property you want to set without a value::
  1191. >>> setp(line, 'linestyle')
  1192. linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]
  1193. If you want to see all the properties that can be set, and their
  1194. possible values, you can do::
  1195. >>> setp(line)
  1196. ... long output listing omitted
  1197. You may specify another output file to `setp` if `sys.stdout` is not
  1198. acceptable for some reason using the `file` keyword-only argument::
  1199. >>> with fopen('output.log') as f:
  1200. >>> setp(line, file=f)
  1201. :func:`setp` operates on a single instance or a iterable of
  1202. instances. If you are in query mode introspecting the possible
  1203. values, only the first instance in the sequence is used. When
  1204. actually setting values, all the instances will be set. e.g.,
  1205. suppose you have a list of two lines, the following will make both
  1206. lines thicker and red::
  1207. >>> x = arange(0,1.0,0.01)
  1208. >>> y1 = sin(2*pi*x)
  1209. >>> y2 = sin(4*pi*x)
  1210. >>> lines = plot(x, y1, x, y2)
  1211. >>> setp(lines, linewidth=2, color='r')
  1212. :func:`setp` works with the MATLAB style string/value pairs or
  1213. with python kwargs. For example, the following are equivalent::
  1214. >>> setp(lines, 'linewidth', 2, 'color', 'r') # MATLAB style
  1215. >>> setp(lines, linewidth=2, color='r') # python style
  1216. """
  1217. if isinstance(obj, Artist):
  1218. objs = [obj]
  1219. else:
  1220. objs = list(cbook.flatten(obj))
  1221. if not objs:
  1222. return
  1223. insp = ArtistInspector(objs[0])
  1224. # file has to be popped before checking if kwargs is empty
  1225. printArgs = {}
  1226. if 'file' in kwargs:
  1227. printArgs['file'] = kwargs.pop('file')
  1228. if not kwargs and len(args) < 2:
  1229. if args:
  1230. print(insp.pprint_setters(prop=args[0]), **printArgs)
  1231. else:
  1232. print('\n'.join(insp.pprint_setters()), **printArgs)
  1233. return
  1234. if len(args) % 2:
  1235. raise ValueError('The set args must be string, value pairs')
  1236. # put args into ordereddict to maintain order
  1237. funcvals = OrderedDict((k, v) for k, v in zip(args[::2], args[1::2]))
  1238. ret = [o.update(funcvals) for o in objs] + [o.set(**kwargs) for o in objs]
  1239. return list(cbook.flatten(ret))
  1240. def kwdoc(artist):
  1241. r"""
  1242. Inspect an `~matplotlib.artist.Artist` class and return
  1243. information about its settable properties and their current values.
  1244. It use the class `.ArtistInspector`.
  1245. Parameters
  1246. ----------
  1247. artist : `~matplotlib.artist.Artist` or an iterable of `Artist`\s
  1248. Returns
  1249. -------
  1250. string
  1251. Returns a string with a list or rst table with the settable properties
  1252. of the *artist*. The formating depends on the value of
  1253. :rc:`docstring.hardcopy`. False result in a list that is intended for
  1254. easy reading as a docstring and True result in a rst table intended
  1255. for rendering the documentation with sphinx.
  1256. """
  1257. hardcopy = matplotlib.rcParams['docstring.hardcopy']
  1258. if hardcopy:
  1259. return '\n'.join(ArtistInspector(artist).pprint_setters_rest(
  1260. leadingspace=4))
  1261. else:
  1262. return '\n'.join(ArtistInspector(artist).pprint_setters(
  1263. leadingspace=2))
  1264. docstring.interpd.update(Artist=kwdoc(Artist))