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.

1853 lines
69 KiB

4 years ago
  1. """
  2. These are classes to support contour plotting and labelling for the Axes class.
  3. """
  4. from numbers import Integral
  5. import warnings
  6. import numpy as np
  7. from numpy import ma
  8. import matplotlib as mpl
  9. import matplotlib._contour as _contour
  10. import matplotlib.path as mpath
  11. import matplotlib.ticker as ticker
  12. import matplotlib.cm as cm
  13. import matplotlib.colors as mcolors
  14. import matplotlib.collections as mcoll
  15. import matplotlib.font_manager as font_manager
  16. import matplotlib.text as text
  17. import matplotlib.cbook as cbook
  18. import matplotlib.mathtext as mathtext
  19. import matplotlib.patches as mpatches
  20. import matplotlib.texmanager as texmanager
  21. import matplotlib.transforms as mtransforms
  22. # Import needed for adding manual selection capability to clabel
  23. from matplotlib.blocking_input import BlockingContourLabeler
  24. # We can't use a single line collection for contour because a line
  25. # collection can have only a single line style, and we want to be able to have
  26. # dashed negative contours, for example, and solid positive contours.
  27. # We could use a single polygon collection for filled contours, but it
  28. # seems better to keep line and filled contours similar, with one collection
  29. # per level.
  30. class ClabelText(text.Text):
  31. """
  32. Unlike the ordinary text, the get_rotation returns an updated
  33. angle in the pixel coordinate assuming that the input rotation is
  34. an angle in data coordinate (or whatever transform set).
  35. """
  36. def get_rotation(self):
  37. angle = text.Text.get_rotation(self)
  38. trans = self.get_transform()
  39. x, y = self.get_position()
  40. new_angles = trans.transform_angles(np.array([angle]),
  41. np.array([[x, y]]))
  42. return new_angles[0]
  43. class ContourLabeler(object):
  44. """Mixin to provide labelling capability to `.ContourSet`."""
  45. def clabel(self, *args,
  46. fontsize=None, inline=True, inline_spacing=5, fmt='%1.3f',
  47. colors=None, use_clabeltext=False, manual=False,
  48. rightside_up=True):
  49. """
  50. Label a contour plot.
  51. Call signature::
  52. clabel(cs, [levels,] **kwargs)
  53. Adds labels to line contours in *cs*, where *cs* is a
  54. :class:`~matplotlib.contour.ContourSet` object returned by
  55. ``contour()``.
  56. Parameters
  57. ----------
  58. cs : `.ContourSet`
  59. The ContourSet to label.
  60. levels : array-like, optional
  61. A list of level values, that should be labeled. The list must be
  62. a subset of ``cs.levels``. If not given, all levels are labeled.
  63. fontsize : string or float, optional
  64. Size in points or relative size e.g., 'smaller', 'x-large'.
  65. See `.Text.set_size` for accepted string values.
  66. colors : color-spec, optional
  67. The label colors:
  68. - If *None*, the color of each label matches the color of
  69. the corresponding contour.
  70. - If one string color, e.g., *colors* = 'r' or *colors* =
  71. 'red', all labels will be plotted in this color.
  72. - If a tuple of matplotlib color args (string, float, rgb, etc),
  73. different labels will be plotted in different colors in the order
  74. specified.
  75. inline : bool, optional
  76. If ``True`` the underlying contour is removed where the label is
  77. placed. Default is ``True``.
  78. inline_spacing : float, optional
  79. Space in pixels to leave on each side of label when
  80. placing inline. Defaults to 5.
  81. This spacing will be exact for labels at locations where the
  82. contour is straight, less so for labels on curved contours.
  83. fmt : string or dict, optional
  84. A format string for the label. Default is '%1.3f'
  85. Alternatively, this can be a dictionary matching contour
  86. levels with arbitrary strings to use for each contour level
  87. (i.e., fmt[level]=string), or it can be any callable, such
  88. as a :class:`~matplotlib.ticker.Formatter` instance, that
  89. returns a string when called with a numeric contour level.
  90. manual : bool or iterable, optional
  91. If ``True``, contour labels will be placed manually using
  92. mouse clicks. Click the first button near a contour to
  93. add a label, click the second button (or potentially both
  94. mouse buttons at once) to finish adding labels. The third
  95. button can be used to remove the last label added, but
  96. only if labels are not inline. Alternatively, the keyboard
  97. can be used to select label locations (enter to end label
  98. placement, delete or backspace act like the third mouse button,
  99. and any other key will select a label location).
  100. *manual* can also be an iterable object of x,y tuples.
  101. Contour labels will be created as if mouse is clicked at each
  102. x,y positions.
  103. rightside_up : bool, optional
  104. If ``True``, label rotations will always be plus
  105. or minus 90 degrees from level. Default is ``True``.
  106. use_clabeltext : bool, optional
  107. If ``True``, `.ClabelText` class (instead of `.Text`) is used to
  108. create labels. `ClabelText` recalculates rotation angles
  109. of texts during the drawing time, therefore this can be used if
  110. aspect of the axes changes. Default is ``False``.
  111. Returns
  112. -------
  113. labels
  114. A list of `.Text` instances for the labels.
  115. """
  116. """
  117. NOTES on how this all works:
  118. clabel basically takes the input arguments and uses them to
  119. add a list of "label specific" attributes to the ContourSet
  120. object. These attributes are all of the form label* and names
  121. should be fairly self explanatory.
  122. Once these attributes are set, clabel passes control to the
  123. labels method (case of automatic label placement) or
  124. `BlockingContourLabeler` (case of manual label placement).
  125. """
  126. self.labelFmt = fmt
  127. self._use_clabeltext = use_clabeltext
  128. # Detect if manual selection is desired and remove from argument list.
  129. self.labelManual = manual
  130. self.rightside_up = rightside_up
  131. if len(args) == 0:
  132. levels = self.levels
  133. indices = list(range(len(self.cvalues)))
  134. elif len(args) == 1:
  135. levlabs = list(args[0])
  136. indices, levels = [], []
  137. for i, lev in enumerate(self.levels):
  138. if lev in levlabs:
  139. indices.append(i)
  140. levels.append(lev)
  141. if len(levels) < len(levlabs):
  142. raise ValueError("Specified levels {} don't match available "
  143. "levels {}".format(levlabs, self.levels))
  144. else:
  145. raise TypeError("Illegal arguments to clabel, see help(clabel)")
  146. self.labelLevelList = levels
  147. self.labelIndiceList = indices
  148. self.labelFontProps = font_manager.FontProperties()
  149. self.labelFontProps.set_size(fontsize)
  150. font_size_pts = self.labelFontProps.get_size_in_points()
  151. self.labelFontSizeList = [font_size_pts] * len(levels)
  152. if colors is None:
  153. self.labelMappable = self
  154. self.labelCValueList = np.take(self.cvalues, self.labelIndiceList)
  155. else:
  156. cmap = mcolors.ListedColormap(colors, N=len(self.labelLevelList))
  157. self.labelCValueList = list(range(len(self.labelLevelList)))
  158. self.labelMappable = cm.ScalarMappable(cmap=cmap,
  159. norm=mcolors.NoNorm())
  160. self.labelXYs = []
  161. if cbook.iterable(self.labelManual):
  162. for x, y in self.labelManual:
  163. self.add_label_near(x, y, inline,
  164. inline_spacing)
  165. elif self.labelManual:
  166. print('Select label locations manually using first mouse button.')
  167. print('End manual selection with second mouse button.')
  168. if not inline:
  169. print('Remove last label by clicking third mouse button.')
  170. blocking_contour_labeler = BlockingContourLabeler(self)
  171. blocking_contour_labeler(inline, inline_spacing)
  172. else:
  173. self.labels(inline, inline_spacing)
  174. self.labelTextsList = cbook.silent_list('text.Text', self.labelTexts)
  175. return self.labelTextsList
  176. cl = property(cbook.deprecated("3.0", alternative="labelTexts")(
  177. lambda self: self.labelTexts))
  178. cl_xy = property(cbook.deprecated("3.0", alternative="labelXYs")(
  179. lambda self: self.labelXYs))
  180. cl_cvalues = property(cbook.deprecated("3.0", alternative="labelCValues")(
  181. lambda self: self.labelCValues))
  182. def print_label(self, linecontour, labelwidth):
  183. "Return *False* if contours are too short for a label."
  184. return (len(linecontour) > 10 * labelwidth
  185. or (np.ptp(linecontour, axis=0) > 1.2 * labelwidth).any())
  186. def too_close(self, x, y, lw):
  187. "Return *True* if a label is already near this location."
  188. for loc in self.labelXYs:
  189. d = np.sqrt((x - loc[0]) ** 2 + (y - loc[1]) ** 2)
  190. if d < 1.2 * lw:
  191. return True
  192. return False
  193. def get_label_coords(self, distances, XX, YY, ysize, lw):
  194. """
  195. Return x, y, and the index of a label location.
  196. Labels are plotted at a location with the smallest
  197. deviation of the contour from a straight line
  198. unless there is another label nearby, in which case
  199. the next best place on the contour is picked up.
  200. If all such candidates are rejected, the beginning
  201. of the contour is chosen.
  202. """
  203. hysize = int(ysize / 2)
  204. adist = np.argsort(distances)
  205. for ind in adist:
  206. x, y = XX[ind][hysize], YY[ind][hysize]
  207. if self.too_close(x, y, lw):
  208. continue
  209. return x, y, ind
  210. ind = adist[0]
  211. x, y = XX[ind][hysize], YY[ind][hysize]
  212. return x, y, ind
  213. def get_label_width(self, lev, fmt, fsize):
  214. """
  215. Return the width of the label in points.
  216. """
  217. if not isinstance(lev, str):
  218. lev = self.get_text(lev, fmt)
  219. lev, ismath = text.Text.is_math_text(lev)
  220. if ismath == 'TeX':
  221. if not hasattr(self, '_TeX_manager'):
  222. self._TeX_manager = texmanager.TexManager()
  223. lw, _, _ = self._TeX_manager.get_text_width_height_descent(lev,
  224. fsize)
  225. elif ismath:
  226. if not hasattr(self, '_mathtext_parser'):
  227. self._mathtext_parser = mathtext.MathTextParser('bitmap')
  228. img, _ = self._mathtext_parser.parse(lev, dpi=72,
  229. prop=self.labelFontProps)
  230. lw = img.get_width() # at dpi=72, the units are PostScript points
  231. else:
  232. # width is much less than "font size"
  233. lw = (len(lev)) * fsize * 0.6
  234. return lw
  235. @cbook.deprecated("2.2")
  236. def get_real_label_width(self, lev, fmt, fsize):
  237. """
  238. This computes actual onscreen label width.
  239. This uses some black magic to determine onscreen extent of non-drawn
  240. label. This magic may not be very robust.
  241. This method is not being used, and may be modified or removed.
  242. """
  243. # Find middle of axes
  244. xx = np.mean(np.asarray(self.ax.axis()).reshape(2, 2), axis=1)
  245. # Temporarily create text object
  246. t = text.Text(xx[0], xx[1])
  247. self.set_label_props(t, self.get_text(lev, fmt), 'k')
  248. # Some black magic to get onscreen extent
  249. # NOTE: This will only work for already drawn figures, as the canvas
  250. # does not have a renderer otherwise. This is the reason this function
  251. # can't be integrated into the rest of the code.
  252. bbox = t.get_window_extent(renderer=self.ax.figure.canvas.renderer)
  253. # difference in pixel extent of image
  254. lw = np.diff(bbox.corners()[0::2, 0])[0]
  255. return lw
  256. def set_label_props(self, label, text, color):
  257. """Set the label properties - color, fontsize, text."""
  258. label.set_text(text)
  259. label.set_color(color)
  260. label.set_fontproperties(self.labelFontProps)
  261. label.set_clip_box(self.ax.bbox)
  262. def get_text(self, lev, fmt):
  263. """Get the text of the label."""
  264. if isinstance(lev, str):
  265. return lev
  266. else:
  267. if isinstance(fmt, dict):
  268. return fmt.get(lev, '%1.3f')
  269. elif callable(fmt):
  270. return fmt(lev)
  271. else:
  272. return fmt % lev
  273. def locate_label(self, linecontour, labelwidth):
  274. """
  275. Find good place to draw a label (relatively flat part of the contour).
  276. """
  277. # Number of contour points
  278. nsize = len(linecontour)
  279. if labelwidth > 1:
  280. xsize = int(np.ceil(nsize / labelwidth))
  281. else:
  282. xsize = 1
  283. if xsize == 1:
  284. ysize = nsize
  285. else:
  286. ysize = int(labelwidth)
  287. XX = np.resize(linecontour[:, 0], (xsize, ysize))
  288. YY = np.resize(linecontour[:, 1], (xsize, ysize))
  289. # I might have fouled up the following:
  290. yfirst = YY[:, :1]
  291. ylast = YY[:, -1:]
  292. xfirst = XX[:, :1]
  293. xlast = XX[:, -1:]
  294. s = (yfirst - YY) * (xlast - xfirst) - (xfirst - XX) * (ylast - yfirst)
  295. L = np.hypot(xlast - xfirst, ylast - yfirst)
  296. # Ignore warning that divide by zero throws, as this is a valid option
  297. with np.errstate(divide='ignore', invalid='ignore'):
  298. dist = np.sum(np.abs(s) / L, axis=-1)
  299. x, y, ind = self.get_label_coords(dist, XX, YY, ysize, labelwidth)
  300. # There must be a more efficient way...
  301. lc = [tuple(l) for l in linecontour]
  302. dind = lc.index((x, y))
  303. return x, y, dind
  304. def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
  305. """
  306. This function calculates the appropriate label rotation given
  307. the linecontour coordinates in screen units, the index of the
  308. label location and the label width.
  309. It will also break contour and calculate inlining if *lc* is
  310. not empty (lc defaults to the empty list if None). *spacing*
  311. is the space around the label in pixels to leave empty.
  312. Do both of these tasks at once to avoid calculating path lengths
  313. multiple times, which is relatively costly.
  314. The method used here involves calculating the path length
  315. along the contour in pixel coordinates and then looking
  316. approximately label width / 2 away from central point to
  317. determine rotation and then to break contour if desired.
  318. """
  319. if lc is None:
  320. lc = []
  321. # Half the label width
  322. hlw = lw / 2.0
  323. # Check if closed and, if so, rotate contour so label is at edge
  324. closed = _is_closed_polygon(slc)
  325. if closed:
  326. slc = np.r_[slc[ind:-1], slc[:ind + 1]]
  327. if len(lc): # Rotate lc also if not empty
  328. lc = np.r_[lc[ind:-1], lc[:ind + 1]]
  329. ind = 0
  330. # Calculate path lengths
  331. pl = np.zeros(slc.shape[0], dtype=float)
  332. dx = np.diff(slc, axis=0)
  333. pl[1:] = np.cumsum(np.hypot(dx[:, 0], dx[:, 1]))
  334. pl = pl - pl[ind]
  335. # Use linear interpolation to get points around label
  336. xi = np.array([-hlw, hlw])
  337. if closed: # Look at end also for closed contours
  338. dp = np.array([pl[-1], 0])
  339. else:
  340. dp = np.zeros_like(xi)
  341. # Get angle of vector between the two ends of the label - must be
  342. # calculated in pixel space for text rotation to work correctly.
  343. (dx,), (dy,) = (np.diff(np.interp(dp + xi, pl, slc_col))
  344. for slc_col in slc.T)
  345. rotation = np.rad2deg(np.arctan2(dy, dx))
  346. if self.rightside_up:
  347. # Fix angle so text is never upside-down
  348. rotation = (rotation + 90) % 180 - 90
  349. # Break contour if desired
  350. nlc = []
  351. if len(lc):
  352. # Expand range by spacing
  353. xi = dp + xi + np.array([-spacing, spacing])
  354. # Get (integer) indices near points of interest; use -1 as marker
  355. # for out of bounds.
  356. I = np.interp(xi, pl, np.arange(len(pl)), left=-1, right=-1)
  357. I = [np.floor(I[0]).astype(int), np.ceil(I[1]).astype(int)]
  358. if I[0] != -1:
  359. xy1 = [np.interp(xi[0], pl, lc_col) for lc_col in lc.T]
  360. if I[1] != -1:
  361. xy2 = [np.interp(xi[1], pl, lc_col) for lc_col in lc.T]
  362. # Actually break contours
  363. if closed:
  364. # This will remove contour if shorter than label
  365. if all(i != -1 for i in I):
  366. nlc.append(np.row_stack([xy2, lc[I[1]:I[0]+1], xy1]))
  367. else:
  368. # These will remove pieces of contour if they have length zero
  369. if I[0] != -1:
  370. nlc.append(np.row_stack([lc[:I[0]+1], xy1]))
  371. if I[1] != -1:
  372. nlc.append(np.row_stack([xy2, lc[I[1]:]]))
  373. # The current implementation removes contours completely
  374. # covered by labels. Uncomment line below to keep
  375. # original contour if this is the preferred behavior.
  376. # if not len(nlc): nlc = [ lc ]
  377. return rotation, nlc
  378. def _get_label_text(self, x, y, rotation):
  379. dx, dy = self.ax.transData.inverted().transform_point((x, y))
  380. t = text.Text(dx, dy, rotation=rotation,
  381. horizontalalignment='center',
  382. verticalalignment='center')
  383. return t
  384. def _get_label_clabeltext(self, x, y, rotation):
  385. # x, y, rotation is given in pixel coordinate. Convert them to
  386. # the data coordinate and create a label using ClabelText
  387. # class. This way, the roation of the clabel is along the
  388. # contour line always.
  389. transDataInv = self.ax.transData.inverted()
  390. dx, dy = transDataInv.transform_point((x, y))
  391. drotation = transDataInv.transform_angles(np.array([rotation]),
  392. np.array([[x, y]]))
  393. t = ClabelText(dx, dy, rotation=drotation[0],
  394. horizontalalignment='center',
  395. verticalalignment='center')
  396. return t
  397. def _add_label(self, t, x, y, lev, cvalue):
  398. color = self.labelMappable.to_rgba(cvalue, alpha=self.alpha)
  399. _text = self.get_text(lev, self.labelFmt)
  400. self.set_label_props(t, _text, color)
  401. self.labelTexts.append(t)
  402. self.labelCValues.append(cvalue)
  403. self.labelXYs.append((x, y))
  404. # Add label to plot here - useful for manual mode label selection
  405. self.ax.add_artist(t)
  406. def add_label(self, x, y, rotation, lev, cvalue):
  407. """
  408. Add contour label using :class:`~matplotlib.text.Text` class.
  409. """
  410. t = self._get_label_text(x, y, rotation)
  411. self._add_label(t, x, y, lev, cvalue)
  412. def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
  413. """
  414. Add contour label using :class:`ClabelText` class.
  415. """
  416. # x, y, rotation is given in pixel coordinate. Convert them to
  417. # the data coordinate and create a label using ClabelText
  418. # class. This way, the roation of the clabel is along the
  419. # contour line always.
  420. t = self._get_label_clabeltext(x, y, rotation)
  421. self._add_label(t, x, y, lev, cvalue)
  422. def add_label_near(self, x, y, inline=True, inline_spacing=5,
  423. transform=None):
  424. """
  425. Add a label near the point (x, y). If transform is None
  426. (default), (x, y) is in data coordinates; if transform is
  427. False, (x, y) is in display coordinates; otherwise, the
  428. specified transform will be used to translate (x, y) into
  429. display coordinates.
  430. Parameters
  431. ----------
  432. x, y : float
  433. The approximate location of the label.
  434. inline : bool, optional, default: True
  435. If *True* remove the segment of the contour beneath the label.
  436. inline_spacing : int, optional, default: 5
  437. Space in pixels to leave on each side of label when placing
  438. inline. This spacing will be exact for labels at locations where
  439. the contour is straight, less so for labels on curved contours.
  440. """
  441. if transform is None:
  442. transform = self.ax.transData
  443. if transform:
  444. x, y = transform.transform_point((x, y))
  445. # find the nearest contour _in screen units_
  446. conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
  447. x, y, self.labelIndiceList)[:5]
  448. # The calc_label_rot_and_inline routine requires that (xmin,ymin)
  449. # be a vertex in the path. So, if it isn't, add a vertex here
  450. # grab the paths from the collections
  451. paths = self.collections[conmin].get_paths()
  452. # grab the correct segment
  453. active_path = paths[segmin]
  454. # grab its vertices
  455. lc = active_path.vertices
  456. # sort out where the new vertex should be added data-units
  457. xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
  458. # if there isn't a vertex close enough
  459. if not np.allclose(xcmin, lc[imin]):
  460. # insert new data into the vertex list
  461. lc = np.r_[lc[:imin], np.array(xcmin)[None, :], lc[imin:]]
  462. # replace the path with the new one
  463. paths[segmin] = mpath.Path(lc)
  464. # Get index of nearest level in subset of levels used for labeling
  465. lmin = self.labelIndiceList.index(conmin)
  466. # Coordinates of contour
  467. paths = self.collections[conmin].get_paths()
  468. lc = paths[segmin].vertices
  469. # In pixel/screen space
  470. slc = self.ax.transData.transform(lc)
  471. # Get label width for rotating labels and breaking contours
  472. lw = self.get_label_width(self.labelLevelList[lmin],
  473. self.labelFmt, self.labelFontSizeList[lmin])
  474. # lw is in points.
  475. lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
  476. # now lw in pixels
  477. # Figure out label rotation.
  478. if inline:
  479. lcarg = lc
  480. else:
  481. lcarg = None
  482. rotation, nlc = self.calc_label_rot_and_inline(
  483. slc, imin, lw, lcarg,
  484. inline_spacing)
  485. self.add_label(xmin, ymin, rotation, self.labelLevelList[lmin],
  486. self.labelCValueList[lmin])
  487. if inline:
  488. # Remove old, not looping over paths so we can do this up front
  489. paths.pop(segmin)
  490. # Add paths if not empty or single point
  491. for n in nlc:
  492. if len(n) > 1:
  493. paths.append(mpath.Path(n))
  494. def pop_label(self, index=-1):
  495. """Defaults to removing last label, but any index can be supplied"""
  496. self.labelCValues.pop(index)
  497. t = self.labelTexts.pop(index)
  498. t.remove()
  499. def labels(self, inline, inline_spacing):
  500. if self._use_clabeltext:
  501. add_label = self.add_label_clabeltext
  502. else:
  503. add_label = self.add_label
  504. for icon, lev, fsize, cvalue in zip(
  505. self.labelIndiceList, self.labelLevelList,
  506. self.labelFontSizeList, self.labelCValueList):
  507. con = self.collections[icon]
  508. trans = con.get_transform()
  509. lw = self.get_label_width(lev, self.labelFmt, fsize)
  510. lw *= self.ax.figure.dpi / 72.0 # scale to screen coordinates
  511. additions = []
  512. paths = con.get_paths()
  513. for segNum, linepath in enumerate(paths):
  514. lc = linepath.vertices # Line contour
  515. slc0 = trans.transform(lc) # Line contour in screen coords
  516. # For closed polygons, add extra point to avoid division by
  517. # zero in print_label and locate_label. Other than these
  518. # functions, this is not necessary and should probably be
  519. # eventually removed.
  520. if _is_closed_polygon(lc):
  521. slc = np.r_[slc0, slc0[1:2, :]]
  522. else:
  523. slc = slc0
  524. # Check if long enough for a label
  525. if self.print_label(slc, lw):
  526. x, y, ind = self.locate_label(slc, lw)
  527. if inline:
  528. lcarg = lc
  529. else:
  530. lcarg = None
  531. rotation, new = self.calc_label_rot_and_inline(
  532. slc0, ind, lw, lcarg,
  533. inline_spacing)
  534. # Actually add the label
  535. add_label(x, y, rotation, lev, cvalue)
  536. # If inline, add new contours
  537. if inline:
  538. for n in new:
  539. # Add path if not empty or single point
  540. if len(n) > 1:
  541. additions.append(mpath.Path(n))
  542. else: # If not adding label, keep old path
  543. additions.append(linepath)
  544. # After looping over all segments on a contour, remove old
  545. # paths and add new ones if inlining
  546. if inline:
  547. del paths[:]
  548. paths.extend(additions)
  549. def _find_closest_point_on_leg(p1, p2, p0):
  550. """Find the closest point to p0 on line segment connecting p1 and p2."""
  551. # handle degenerate case
  552. if np.all(p2 == p1):
  553. d = np.sum((p0 - p1)**2)
  554. return d, p1
  555. d21 = p2 - p1
  556. d01 = p0 - p1
  557. # project on to line segment to find closest point
  558. proj = np.dot(d01, d21) / np.dot(d21, d21)
  559. if proj < 0:
  560. proj = 0
  561. if proj > 1:
  562. proj = 1
  563. pc = p1 + proj * d21
  564. # find squared distance
  565. d = np.sum((pc-p0)**2)
  566. return d, pc
  567. def _is_closed_polygon(X):
  568. """
  569. Return whether first and last object in a sequence are the same. These are
  570. presumably coordinates on a polygonal curve, in which case this function
  571. tests if that curve is closed.
  572. """
  573. return np.all(X[0] == X[-1])
  574. def _find_closest_point_on_path(lc, point):
  575. """
  576. lc: coordinates of vertices
  577. point: coordinates of test point
  578. """
  579. # find index of closest vertex for this segment
  580. ds = np.sum((lc - point[None, :])**2, 1)
  581. imin = np.argmin(ds)
  582. dmin = np.inf
  583. xcmin = None
  584. legmin = (None, None)
  585. closed = _is_closed_polygon(lc)
  586. # build list of legs before and after this vertex
  587. legs = []
  588. if imin > 0 or closed:
  589. legs.append(((imin-1) % len(lc), imin))
  590. if imin < len(lc) - 1 or closed:
  591. legs.append((imin, (imin+1) % len(lc)))
  592. for leg in legs:
  593. d, xc = _find_closest_point_on_leg(lc[leg[0]], lc[leg[1]], point)
  594. if d < dmin:
  595. dmin = d
  596. xcmin = xc
  597. legmin = leg
  598. return (dmin, xcmin, legmin)
  599. class ContourSet(cm.ScalarMappable, ContourLabeler):
  600. """
  601. Store a set of contour lines or filled regions.
  602. User-callable method: `~.axes.Axes.clabel`
  603. Parameters
  604. ----------
  605. ax : `~.axes.Axes`
  606. levels : [level0, level1, ..., leveln]
  607. A list of floating point numbers indicating the contour
  608. levels.
  609. allsegs : [level0segs, level1segs, ...]
  610. List of all the polygon segments for all the *levels*.
  611. For contour lines ``len(allsegs) == len(levels)``, and for
  612. filled contour regions ``len(allsegs) = len(levels)-1``. The lists
  613. should look like::
  614. level0segs = [polygon0, polygon1, ...]
  615. polygon0 = array_like [[x0,y0], [x1,y1], ...]
  616. allkinds : ``None`` or [level0kinds, level1kinds, ...]
  617. Optional list of all the polygon vertex kinds (code types), as
  618. described and used in Path. This is used to allow multiply-
  619. connected paths such as holes within filled polygons.
  620. If not ``None``, ``len(allkinds) == len(allsegs)``. The lists
  621. should look like::
  622. level0kinds = [polygon0kinds, ...]
  623. polygon0kinds = [vertexcode0, vertexcode1, ...]
  624. If *allkinds* is not ``None``, usually all polygons for a
  625. particular contour level are grouped together so that
  626. ``level0segs = [polygon0]`` and ``level0kinds = [polygon0kinds]``.
  627. kwargs :
  628. Keyword arguments are as described in the docstring of
  629. `~.axes.Axes.contour`.
  630. Attributes
  631. ----------
  632. ax:
  633. The axes object in which the contours are drawn.
  634. collections:
  635. A silent_list of LineCollections or PolyCollections.
  636. levels:
  637. Contour levels.
  638. layers:
  639. Same as levels for line contours; half-way between
  640. levels for filled contours. See :meth:`_process_colors`.
  641. """
  642. def __init__(self, ax, *args,
  643. levels=None, filled=False, linewidths=None, linestyles=None,
  644. alpha=None, origin=None, extent=None,
  645. cmap=None, colors=None, norm=None, vmin=None, vmax=None,
  646. extend='neither', antialiased=None,
  647. **kwargs):
  648. """
  649. Draw contour lines or filled regions, depending on
  650. whether keyword arg *filled* is ``False`` (default) or ``True``.
  651. Call signature::
  652. ContourSet(ax, levels, allsegs, [allkinds], **kwargs)
  653. Parameters
  654. ----------
  655. ax :
  656. The `~.axes.Axes` object to draw on.
  657. levels : [level0, level1, ..., leveln]
  658. A list of floating point numbers indicating the contour
  659. levels.
  660. allsegs : [level0segs, level1segs, ...]
  661. List of all the polygon segments for all the *levels*.
  662. For contour lines ``len(allsegs) == len(levels)``, and for
  663. filled contour regions ``len(allsegs) = len(levels)-1``. The lists
  664. should look like::
  665. level0segs = [polygon0, polygon1, ...]
  666. polygon0 = array_like [[x0,y0], [x1,y1], ...]
  667. allkinds : [level0kinds, level1kinds, ...], optional
  668. Optional list of all the polygon vertex kinds (code types), as
  669. described and used in Path. This is used to allow multiply-
  670. connected paths such as holes within filled polygons.
  671. If not ``None``, ``len(allkinds) == len(allsegs)``. The lists
  672. should look like::
  673. level0kinds = [polygon0kinds, ...]
  674. polygon0kinds = [vertexcode0, vertexcode1, ...]
  675. If *allkinds* is not ``None``, usually all polygons for a
  676. particular contour level are grouped together so that
  677. ``level0segs = [polygon0]`` and ``level0kinds = [polygon0kinds]``.
  678. **kwargs
  679. Keyword arguments are as described in the docstring of
  680. `~axes.Axes.contour`.
  681. """
  682. self.ax = ax
  683. self.levels = levels
  684. self.filled = filled
  685. self.linewidths = linewidths
  686. self.linestyles = linestyles
  687. self.hatches = kwargs.pop('hatches', [None])
  688. self.alpha = alpha
  689. self.origin = origin
  690. self.extent = extent
  691. self.colors = colors
  692. self.extend = extend
  693. self.antialiased = antialiased
  694. if self.antialiased is None and self.filled:
  695. self.antialiased = False # eliminate artifacts; we are not
  696. # stroking the boundaries.
  697. # The default for line contours will be taken from
  698. # the LineCollection default, which uses the
  699. # rcParams['lines.antialiased']
  700. self.nchunk = kwargs.pop('nchunk', 0)
  701. self.locator = kwargs.pop('locator', None)
  702. if (isinstance(norm, mcolors.LogNorm)
  703. or isinstance(self.locator, ticker.LogLocator)):
  704. self.logscale = True
  705. if norm is None:
  706. norm = mcolors.LogNorm()
  707. else:
  708. self.logscale = False
  709. if self.origin not in [None, 'lower', 'upper', 'image']:
  710. raise ValueError("If given, *origin* must be one of [ 'lower' |"
  711. " 'upper' | 'image']")
  712. if self.extent is not None and len(self.extent) != 4:
  713. raise ValueError("If given, *extent* must be '[ *None* |"
  714. " (x0,x1,y0,y1) ]'")
  715. if self.colors is not None and cmap is not None:
  716. raise ValueError('Either colors or cmap must be None')
  717. if self.origin == 'image':
  718. self.origin = mpl.rcParams['image.origin']
  719. self._transform = kwargs.pop('transform', None)
  720. kwargs = self._process_args(*args, **kwargs)
  721. self._process_levels()
  722. if self.colors is not None:
  723. ncolors = len(self.levels)
  724. if self.filled:
  725. ncolors -= 1
  726. i0 = 0
  727. # Handle the case where colors are given for the extended
  728. # parts of the contour.
  729. extend_min = self.extend in ['min', 'both']
  730. extend_max = self.extend in ['max', 'both']
  731. use_set_under_over = False
  732. # if we are extending the lower end, and we've been given enough
  733. # colors then skip the first color in the resulting cmap. For the
  734. # extend_max case we don't need to worry about passing more colors
  735. # than ncolors as ListedColormap will clip.
  736. total_levels = ncolors + int(extend_min) + int(extend_max)
  737. if len(self.colors) == total_levels and (extend_min or extend_max):
  738. use_set_under_over = True
  739. if extend_min:
  740. i0 = 1
  741. cmap = mcolors.ListedColormap(self.colors[i0:None], N=ncolors)
  742. if use_set_under_over:
  743. if extend_min:
  744. cmap.set_under(self.colors[0])
  745. if extend_max:
  746. cmap.set_over(self.colors[-1])
  747. if self.filled:
  748. self.collections = cbook.silent_list('mcoll.PathCollection')
  749. else:
  750. self.collections = cbook.silent_list('mcoll.LineCollection')
  751. # label lists must be initialized here
  752. self.labelTexts = []
  753. self.labelCValues = []
  754. kw = {'cmap': cmap}
  755. if norm is not None:
  756. kw['norm'] = norm
  757. # sets self.cmap, norm if needed;
  758. cm.ScalarMappable.__init__(self, **kw)
  759. if vmin is not None:
  760. self.norm.vmin = vmin
  761. if vmax is not None:
  762. self.norm.vmax = vmax
  763. self._process_colors()
  764. self.allsegs, self.allkinds = self._get_allsegs_and_allkinds()
  765. if self.filled:
  766. if self.linewidths is not None:
  767. warnings.warn('linewidths is ignored by contourf')
  768. # Lower and upper contour levels.
  769. lowers, uppers = self._get_lowers_and_uppers()
  770. # Ensure allkinds can be zipped below.
  771. if self.allkinds is None:
  772. self.allkinds = [None] * len(self.allsegs)
  773. # Default zorder taken from Collection
  774. zorder = kwargs.pop('zorder', 1)
  775. for level, level_upper, segs, kinds in \
  776. zip(lowers, uppers, self.allsegs, self.allkinds):
  777. paths = self._make_paths(segs, kinds)
  778. col = mcoll.PathCollection(
  779. paths,
  780. antialiaseds=(self.antialiased,),
  781. edgecolors='none',
  782. alpha=self.alpha,
  783. transform=self.get_transform(),
  784. zorder=zorder)
  785. self.ax.add_collection(col, autolim=False)
  786. self.collections.append(col)
  787. else:
  788. tlinewidths = self._process_linewidths()
  789. self.tlinewidths = tlinewidths
  790. tlinestyles = self._process_linestyles()
  791. aa = self.antialiased
  792. if aa is not None:
  793. aa = (self.antialiased,)
  794. # Default zorder taken from LineCollection
  795. zorder = kwargs.pop('zorder', 2)
  796. for level, width, lstyle, segs in \
  797. zip(self.levels, tlinewidths, tlinestyles, self.allsegs):
  798. col = mcoll.LineCollection(
  799. segs,
  800. antialiaseds=aa,
  801. linewidths=width,
  802. linestyles=[lstyle],
  803. alpha=self.alpha,
  804. transform=self.get_transform(),
  805. zorder=zorder)
  806. col.set_label('_nolegend_')
  807. self.ax.add_collection(col, autolim=False)
  808. self.collections.append(col)
  809. for col in self.collections:
  810. col.sticky_edges.x[:] = [self._mins[0], self._maxs[0]]
  811. col.sticky_edges.y[:] = [self._mins[1], self._maxs[1]]
  812. self.ax.update_datalim([self._mins, self._maxs])
  813. self.ax.autoscale_view(tight=True)
  814. self.changed() # set the colors
  815. if kwargs:
  816. s = ", ".join(map(repr, kwargs))
  817. warnings.warn('The following kwargs were not used by contour: ' +
  818. s)
  819. def get_transform(self):
  820. """
  821. Return the :class:`~matplotlib.transforms.Transform`
  822. instance used by this ContourSet.
  823. """
  824. if self._transform is None:
  825. self._transform = self.ax.transData
  826. elif (not isinstance(self._transform, mtransforms.Transform)
  827. and hasattr(self._transform, '_as_mpl_transform')):
  828. self._transform = self._transform._as_mpl_transform(self.ax)
  829. return self._transform
  830. def __getstate__(self):
  831. state = self.__dict__.copy()
  832. # the C object _contour_generator cannot currently be pickled. This
  833. # isn't a big issue as it is not actually used once the contour has
  834. # been calculated.
  835. state['_contour_generator'] = None
  836. return state
  837. def legend_elements(self, variable_name='x', str_format=str):
  838. """
  839. Return a list of artists and labels suitable for passing through
  840. to :func:`plt.legend` which represent this ContourSet.
  841. The labels have the form "0 < x <= 1" stating the data ranges which
  842. the artists represent.
  843. Parameters
  844. ----------
  845. variable_name : str
  846. The string used inside the inequality used on the labels.
  847. str_format : function: float -> str
  848. Function used to format the numbers in the labels.
  849. Returns
  850. -------
  851. artists : List[`.Artist`]
  852. A list of the artists.
  853. labels : List[str]
  854. A list of the labels.
  855. """
  856. artists = []
  857. labels = []
  858. if self.filled:
  859. lowers, uppers = self._get_lowers_and_uppers()
  860. n_levels = len(self.collections)
  861. for i, (collection, lower, upper) in enumerate(
  862. zip(self.collections, lowers, uppers)):
  863. patch = mpatches.Rectangle(
  864. (0, 0), 1, 1,
  865. facecolor=collection.get_facecolor()[0],
  866. hatch=collection.get_hatch(),
  867. alpha=collection.get_alpha())
  868. artists.append(patch)
  869. lower = str_format(lower)
  870. upper = str_format(upper)
  871. if i == 0 and self.extend in ('min', 'both'):
  872. labels.append(r'$%s \leq %s$' % (variable_name,
  873. lower))
  874. elif i == n_levels - 1 and self.extend in ('max', 'both'):
  875. labels.append(r'$%s > %s$' % (variable_name,
  876. upper))
  877. else:
  878. labels.append(r'$%s < %s \leq %s$' % (lower,
  879. variable_name,
  880. upper))
  881. else:
  882. for collection, level in zip(self.collections, self.levels):
  883. patch = mcoll.LineCollection(None)
  884. patch.update_from(collection)
  885. artists.append(patch)
  886. # format the level for insertion into the labels
  887. level = str_format(level)
  888. labels.append(r'$%s = %s$' % (variable_name, level))
  889. return artists, labels
  890. def _process_args(self, *args, **kwargs):
  891. """
  892. Process *args* and *kwargs*; override in derived classes.
  893. Must set self.levels, self.zmin and self.zmax, and update axes
  894. limits.
  895. """
  896. self.levels = args[0]
  897. self.allsegs = args[1]
  898. self.allkinds = len(args) > 2 and args[2] or None
  899. self.zmax = np.max(self.levels)
  900. self.zmin = np.min(self.levels)
  901. self._auto = False
  902. # Check lengths of levels and allsegs.
  903. if self.filled:
  904. if len(self.allsegs) != len(self.levels) - 1:
  905. raise ValueError('must be one less number of segments as '
  906. 'levels')
  907. else:
  908. if len(self.allsegs) != len(self.levels):
  909. raise ValueError('must be same number of segments as levels')
  910. # Check length of allkinds.
  911. if (self.allkinds is not None and
  912. len(self.allkinds) != len(self.allsegs)):
  913. raise ValueError('allkinds has different length to allsegs')
  914. # Determine x,y bounds and update axes data limits.
  915. flatseglist = [s for seg in self.allsegs for s in seg]
  916. points = np.concatenate(flatseglist, axis=0)
  917. self._mins = points.min(axis=0)
  918. self._maxs = points.max(axis=0)
  919. return kwargs
  920. def _get_allsegs_and_allkinds(self):
  921. """
  922. Override in derived classes to create and return allsegs and allkinds.
  923. allkinds can be None.
  924. """
  925. return self.allsegs, self.allkinds
  926. def _get_lowers_and_uppers(self):
  927. """
  928. Return (lowers,uppers) for filled contours.
  929. """
  930. lowers = self._levels[:-1]
  931. if self.zmin == lowers[0]:
  932. # Include minimum values in lowest interval
  933. lowers = lowers.copy() # so we don't change self._levels
  934. if self.logscale:
  935. lowers[0] = 0.99 * self.zmin
  936. else:
  937. lowers[0] -= 1
  938. uppers = self._levels[1:]
  939. return (lowers, uppers)
  940. def _make_paths(self, segs, kinds):
  941. if kinds is not None:
  942. return [mpath.Path(seg, codes=kind)
  943. for seg, kind in zip(segs, kinds)]
  944. else:
  945. return [mpath.Path(seg) for seg in segs]
  946. def changed(self):
  947. tcolors = [(tuple(rgba),)
  948. for rgba in self.to_rgba(self.cvalues, alpha=self.alpha)]
  949. self.tcolors = tcolors
  950. hatches = self.hatches * len(tcolors)
  951. for color, hatch, collection in zip(tcolors, hatches,
  952. self.collections):
  953. if self.filled:
  954. collection.set_facecolor(color)
  955. # update the collection's hatch (may be None)
  956. collection.set_hatch(hatch)
  957. else:
  958. collection.set_color(color)
  959. for label, cv in zip(self.labelTexts, self.labelCValues):
  960. label.set_alpha(self.alpha)
  961. label.set_color(self.labelMappable.to_rgba(cv))
  962. # add label colors
  963. cm.ScalarMappable.changed(self)
  964. def _autolev(self, N):
  965. """
  966. Select contour levels to span the data.
  967. The target number of levels, *N*, is used only when the
  968. scale is not log and default locator is used.
  969. We need two more levels for filled contours than for
  970. line contours, because for the latter we need to specify
  971. the lower and upper boundary of each range. For example,
  972. a single contour boundary, say at z = 0, requires only
  973. one contour line, but two filled regions, and therefore
  974. three levels to provide boundaries for both regions.
  975. """
  976. self._auto = True
  977. if self.locator is None:
  978. if self.logscale:
  979. self.locator = ticker.LogLocator()
  980. else:
  981. self.locator = ticker.MaxNLocator(N + 1, min_n_ticks=1)
  982. lev = self.locator.tick_values(self.zmin, self.zmax)
  983. try:
  984. if self.locator._symmetric:
  985. return lev
  986. except AttributeError:
  987. pass
  988. # Trim excess levels the locator may have supplied.
  989. under = np.nonzero(lev < self.zmin)[0]
  990. i0 = under[-1] if len(under) else 0
  991. over = np.nonzero(lev > self.zmax)[0]
  992. i1 = over[0] + 1 if len(over) else len(lev)
  993. if self.extend in ('min', 'both'):
  994. i0 += 1
  995. if self.extend in ('max', 'both'):
  996. i1 -= 1
  997. if i1 - i0 < 3:
  998. i0, i1 = 0, len(lev)
  999. return lev[i0:i1]
  1000. def _contour_level_args(self, z, args):
  1001. """
  1002. Determine the contour levels and store in self.levels.
  1003. """
  1004. if self.filled:
  1005. fn = 'contourf'
  1006. else:
  1007. fn = 'contour'
  1008. self._auto = False
  1009. if self.levels is None:
  1010. if len(args) == 0:
  1011. levels_arg = 7 # Default, hard-wired.
  1012. else:
  1013. levels_arg = args[0]
  1014. else:
  1015. levels_arg = self.levels
  1016. if isinstance(levels_arg, Integral):
  1017. self.levels = self._autolev(levels_arg)
  1018. else:
  1019. self.levels = np.asarray(levels_arg).astype(np.float64)
  1020. if not self.filled:
  1021. inside = (self.levels > self.zmin) & (self.levels < self.zmax)
  1022. levels_in = self.levels[inside]
  1023. if len(levels_in) == 0:
  1024. self.levels = [self.zmin]
  1025. warnings.warn("No contour levels were found"
  1026. " within the data range.")
  1027. if self.filled and len(self.levels) < 2:
  1028. raise ValueError("Filled contours require at least 2 levels.")
  1029. if len(self.levels) > 1 and np.min(np.diff(self.levels)) <= 0.0:
  1030. raise ValueError("Contour levels must be increasing")
  1031. def _process_levels(self):
  1032. """
  1033. Assign values to :attr:`layers` based on :attr:`levels`,
  1034. adding extended layers as needed if contours are filled.
  1035. For line contours, layers simply coincide with levels;
  1036. a line is a thin layer. No extended levels are needed
  1037. with line contours.
  1038. """
  1039. # Make a private _levels to include extended regions; we
  1040. # want to leave the original levels attribute unchanged.
  1041. # (Colorbar needs this even for line contours.)
  1042. self._levels = list(self.levels)
  1043. if self.logscale:
  1044. lower, upper = 1e-250, 1e250
  1045. else:
  1046. lower, upper = -1e250, 1e250
  1047. if self.extend in ('both', 'min'):
  1048. self._levels.insert(0, lower)
  1049. if self.extend in ('both', 'max'):
  1050. self._levels.append(upper)
  1051. self._levels = np.asarray(self._levels)
  1052. if not self.filled:
  1053. self.layers = self.levels
  1054. return
  1055. # Layer values are mid-way between levels in screen space.
  1056. if self.logscale:
  1057. # Avoid overflow by taking sqrt before multiplying.
  1058. self.layers = (np.sqrt(self._levels[:-1])
  1059. * np.sqrt(self._levels[1:]))
  1060. else:
  1061. self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
  1062. def _process_colors(self):
  1063. """
  1064. Color argument processing for contouring.
  1065. Note that we base the color mapping on the contour levels
  1066. and layers, not on the actual range of the Z values. This
  1067. means we don't have to worry about bad values in Z, and we
  1068. always have the full dynamic range available for the selected
  1069. levels.
  1070. The color is based on the midpoint of the layer, except for
  1071. extended end layers. By default, the norm vmin and vmax
  1072. are the extreme values of the non-extended levels. Hence,
  1073. the layer color extremes are not the extreme values of
  1074. the colormap itself, but approach those values as the number
  1075. of levels increases. An advantage of this scheme is that
  1076. line contours, when added to filled contours, take on
  1077. colors that are consistent with those of the filled regions;
  1078. for example, a contour line on the boundary between two
  1079. regions will have a color intermediate between those
  1080. of the regions.
  1081. """
  1082. self.monochrome = self.cmap.monochrome
  1083. if self.colors is not None:
  1084. # Generate integers for direct indexing.
  1085. i0, i1 = 0, len(self.levels)
  1086. if self.filled:
  1087. i1 -= 1
  1088. # Out of range indices for over and under:
  1089. if self.extend in ('both', 'min'):
  1090. i0 -= 1
  1091. if self.extend in ('both', 'max'):
  1092. i1 += 1
  1093. self.cvalues = list(range(i0, i1))
  1094. self.set_norm(mcolors.NoNorm())
  1095. else:
  1096. self.cvalues = self.layers
  1097. self.set_array(self.levels)
  1098. self.autoscale_None()
  1099. if self.extend in ('both', 'max', 'min'):
  1100. self.norm.clip = False
  1101. # self.tcolors are set by the "changed" method
  1102. def _process_linewidths(self):
  1103. linewidths = self.linewidths
  1104. Nlev = len(self.levels)
  1105. if linewidths is None:
  1106. tlinewidths = [(mpl.rcParams['lines.linewidth'],)] * Nlev
  1107. else:
  1108. if not cbook.iterable(linewidths):
  1109. linewidths = [linewidths] * Nlev
  1110. else:
  1111. linewidths = list(linewidths)
  1112. if len(linewidths) < Nlev:
  1113. nreps = int(np.ceil(Nlev / len(linewidths)))
  1114. linewidths = linewidths * nreps
  1115. if len(linewidths) > Nlev:
  1116. linewidths = linewidths[:Nlev]
  1117. tlinewidths = [(w,) for w in linewidths]
  1118. return tlinewidths
  1119. def _process_linestyles(self):
  1120. linestyles = self.linestyles
  1121. Nlev = len(self.levels)
  1122. if linestyles is None:
  1123. tlinestyles = ['solid'] * Nlev
  1124. if self.monochrome:
  1125. neg_ls = mpl.rcParams['contour.negative_linestyle']
  1126. eps = - (self.zmax - self.zmin) * 1e-15
  1127. for i, lev in enumerate(self.levels):
  1128. if lev < eps:
  1129. tlinestyles[i] = neg_ls
  1130. else:
  1131. if isinstance(linestyles, str):
  1132. tlinestyles = [linestyles] * Nlev
  1133. elif cbook.iterable(linestyles):
  1134. tlinestyles = list(linestyles)
  1135. if len(tlinestyles) < Nlev:
  1136. nreps = int(np.ceil(Nlev / len(linestyles)))
  1137. tlinestyles = tlinestyles * nreps
  1138. if len(tlinestyles) > Nlev:
  1139. tlinestyles = tlinestyles[:Nlev]
  1140. else:
  1141. raise ValueError("Unrecognized type for linestyles kwarg")
  1142. return tlinestyles
  1143. def get_alpha(self):
  1144. """returns alpha to be applied to all ContourSet artists"""
  1145. return self.alpha
  1146. def set_alpha(self, alpha):
  1147. """
  1148. Set the alpha blending value for all ContourSet artists.
  1149. *alpha* must be between 0 (transparent) and 1 (opaque).
  1150. """
  1151. self.alpha = alpha
  1152. self.changed()
  1153. def find_nearest_contour(self, x, y, indices=None, pixel=True):
  1154. """
  1155. Finds contour that is closest to a point. Defaults to
  1156. measuring distance in pixels (screen space - useful for manual
  1157. contour labeling), but this can be controlled via a keyword
  1158. argument.
  1159. Returns a tuple containing the contour, segment, index of
  1160. segment, x & y of segment point and distance to minimum point.
  1161. Optional keyword arguments:
  1162. *indices*:
  1163. Indexes of contour levels to consider when looking for
  1164. nearest point. Defaults to using all levels.
  1165. *pixel*:
  1166. If *True*, measure distance in pixel space, if not, measure
  1167. distance in axes space. Defaults to *True*.
  1168. """
  1169. # This function uses a method that is probably quite
  1170. # inefficient based on converting each contour segment to
  1171. # pixel coordinates and then comparing the given point to
  1172. # those coordinates for each contour. This will probably be
  1173. # quite slow for complex contours, but for normal use it works
  1174. # sufficiently well that the time is not noticeable.
  1175. # Nonetheless, improvements could probably be made.
  1176. if indices is None:
  1177. indices = list(range(len(self.levels)))
  1178. dmin = np.inf
  1179. conmin = None
  1180. segmin = None
  1181. xmin = None
  1182. ymin = None
  1183. point = np.array([x, y])
  1184. for icon in indices:
  1185. con = self.collections[icon]
  1186. trans = con.get_transform()
  1187. paths = con.get_paths()
  1188. for segNum, linepath in enumerate(paths):
  1189. lc = linepath.vertices
  1190. # transfer all data points to screen coordinates if desired
  1191. if pixel:
  1192. lc = trans.transform(lc)
  1193. d, xc, leg = _find_closest_point_on_path(lc, point)
  1194. if d < dmin:
  1195. dmin = d
  1196. conmin = icon
  1197. segmin = segNum
  1198. imin = leg[1]
  1199. xmin = xc[0]
  1200. ymin = xc[1]
  1201. return (conmin, segmin, imin, xmin, ymin, dmin)
  1202. class QuadContourSet(ContourSet):
  1203. """
  1204. Create and store a set of contour lines or filled regions.
  1205. User-callable method: `~axes.Axes.clabel`
  1206. Attributes
  1207. ----------
  1208. ax:
  1209. The axes object in which the contours are drawn.
  1210. collections:
  1211. A silent_list of LineCollections or PolyCollections.
  1212. levels:
  1213. Contour levels.
  1214. layers:
  1215. Same as levels for line contours; half-way between
  1216. levels for filled contours. See :meth:`_process_colors` method.
  1217. """
  1218. def _process_args(self, *args, **kwargs):
  1219. """
  1220. Process args and kwargs.
  1221. """
  1222. if isinstance(args[0], QuadContourSet):
  1223. if self.levels is None:
  1224. self.levels = args[0].levels
  1225. self.zmin = args[0].zmin
  1226. self.zmax = args[0].zmax
  1227. self._corner_mask = args[0]._corner_mask
  1228. contour_generator = args[0]._contour_generator
  1229. self._mins = args[0]._mins
  1230. self._maxs = args[0]._maxs
  1231. else:
  1232. self._corner_mask = kwargs.pop('corner_mask', None)
  1233. if self._corner_mask is None:
  1234. self._corner_mask = mpl.rcParams['contour.corner_mask']
  1235. x, y, z = self._contour_args(args, kwargs)
  1236. _mask = ma.getmask(z)
  1237. if _mask is ma.nomask or not _mask.any():
  1238. _mask = None
  1239. contour_generator = _contour.QuadContourGenerator(
  1240. x, y, z.filled(), _mask, self._corner_mask, self.nchunk)
  1241. t = self.get_transform()
  1242. # if the transform is not trans data, and some part of it
  1243. # contains transData, transform the xs and ys to data coordinates
  1244. if (t != self.ax.transData and
  1245. any(t.contains_branch_seperately(self.ax.transData))):
  1246. trans_to_data = t - self.ax.transData
  1247. pts = (np.vstack([x.flat, y.flat]).T)
  1248. transformed_pts = trans_to_data.transform(pts)
  1249. x = transformed_pts[..., 0]
  1250. y = transformed_pts[..., 1]
  1251. self._mins = [ma.min(x), ma.min(y)]
  1252. self._maxs = [ma.max(x), ma.max(y)]
  1253. self._contour_generator = contour_generator
  1254. return kwargs
  1255. def _get_allsegs_and_allkinds(self):
  1256. """Compute ``allsegs`` and ``allkinds`` using C extension."""
  1257. allsegs = []
  1258. if self.filled:
  1259. lowers, uppers = self._get_lowers_and_uppers()
  1260. allkinds = []
  1261. for level, level_upper in zip(lowers, uppers):
  1262. vertices, kinds = \
  1263. self._contour_generator.create_filled_contour(
  1264. level, level_upper)
  1265. allsegs.append(vertices)
  1266. allkinds.append(kinds)
  1267. else:
  1268. allkinds = None
  1269. for level in self.levels:
  1270. vertices = self._contour_generator.create_contour(level)
  1271. allsegs.append(vertices)
  1272. return allsegs, allkinds
  1273. def _contour_args(self, args, kwargs):
  1274. if self.filled:
  1275. fn = 'contourf'
  1276. else:
  1277. fn = 'contour'
  1278. Nargs = len(args)
  1279. if Nargs <= 2:
  1280. z = ma.asarray(args[0], dtype=np.float64)
  1281. x, y = self._initialize_x_y(z)
  1282. args = args[1:]
  1283. elif Nargs <= 4:
  1284. x, y, z = self._check_xyz(args[:3], kwargs)
  1285. args = args[3:]
  1286. else:
  1287. raise TypeError("Too many arguments to %s; see help(%s)" %
  1288. (fn, fn))
  1289. z = ma.masked_invalid(z, copy=False)
  1290. self.zmax = float(z.max())
  1291. self.zmin = float(z.min())
  1292. if self.logscale and self.zmin <= 0:
  1293. z = ma.masked_where(z <= 0, z)
  1294. warnings.warn('Log scale: values of z <= 0 have been masked')
  1295. self.zmin = float(z.min())
  1296. self._contour_level_args(z, args)
  1297. return (x, y, z)
  1298. def _check_xyz(self, args, kwargs):
  1299. """
  1300. For functions like contour, check that the dimensions
  1301. of the input arrays match; if x and y are 1D, convert
  1302. them to 2D using meshgrid.
  1303. Possible change: I think we should make and use an ArgumentError
  1304. Exception class (here and elsewhere).
  1305. """
  1306. x, y = args[:2]
  1307. kwargs = self.ax._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
  1308. x = self.ax.convert_xunits(x)
  1309. y = self.ax.convert_yunits(y)
  1310. x = np.asarray(x, dtype=np.float64)
  1311. y = np.asarray(y, dtype=np.float64)
  1312. z = ma.asarray(args[2], dtype=np.float64)
  1313. if z.ndim != 2:
  1314. raise TypeError("Input z must be a 2D array.")
  1315. elif z.shape[0] < 2 or z.shape[1] < 2:
  1316. raise TypeError("Input z must be at least a 2x2 array.")
  1317. else:
  1318. Ny, Nx = z.shape
  1319. if x.ndim != y.ndim:
  1320. raise TypeError("Number of dimensions of x and y should match.")
  1321. if x.ndim == 1:
  1322. nx, = x.shape
  1323. ny, = y.shape
  1324. if nx != Nx:
  1325. raise TypeError("Length of x must be number of columns in z.")
  1326. if ny != Ny:
  1327. raise TypeError("Length of y must be number of rows in z.")
  1328. x, y = np.meshgrid(x, y)
  1329. elif x.ndim == 2:
  1330. if x.shape != z.shape:
  1331. raise TypeError("Shape of x does not match that of z: found "
  1332. "{0} instead of {1}.".format(x.shape, z.shape))
  1333. if y.shape != z.shape:
  1334. raise TypeError("Shape of y does not match that of z: found "
  1335. "{0} instead of {1}.".format(y.shape, z.shape))
  1336. else:
  1337. raise TypeError("Inputs x and y must be 1D or 2D.")
  1338. return x, y, z
  1339. def _initialize_x_y(self, z):
  1340. """
  1341. Return X, Y arrays such that contour(Z) will match imshow(Z)
  1342. if origin is not None.
  1343. The center of pixel Z[i,j] depends on origin:
  1344. if origin is None, x = j, y = i;
  1345. if origin is 'lower', x = j + 0.5, y = i + 0.5;
  1346. if origin is 'upper', x = j + 0.5, y = Nrows - i - 0.5
  1347. If extent is not None, x and y will be scaled to match,
  1348. as in imshow.
  1349. If origin is None and extent is not None, then extent
  1350. will give the minimum and maximum values of x and y.
  1351. """
  1352. if z.ndim != 2:
  1353. raise TypeError("Input must be a 2D array.")
  1354. elif z.shape[0] < 2 or z.shape[1] < 2:
  1355. raise TypeError("Input z must be at least a 2x2 array.")
  1356. else:
  1357. Ny, Nx = z.shape
  1358. if self.origin is None: # Not for image-matching.
  1359. if self.extent is None:
  1360. return np.meshgrid(np.arange(Nx), np.arange(Ny))
  1361. else:
  1362. x0, x1, y0, y1 = self.extent
  1363. x = np.linspace(x0, x1, Nx)
  1364. y = np.linspace(y0, y1, Ny)
  1365. return np.meshgrid(x, y)
  1366. # Match image behavior:
  1367. if self.extent is None:
  1368. x0, x1, y0, y1 = (0, Nx, 0, Ny)
  1369. else:
  1370. x0, x1, y0, y1 = self.extent
  1371. dx = (x1 - x0) / Nx
  1372. dy = (y1 - y0) / Ny
  1373. x = x0 + (np.arange(Nx) + 0.5) * dx
  1374. y = y0 + (np.arange(Ny) + 0.5) * dy
  1375. if self.origin == 'upper':
  1376. y = y[::-1]
  1377. return np.meshgrid(x, y)
  1378. _contour_doc = """
  1379. Plot contours.
  1380. Call signature::
  1381. contour([X, Y,] Z, [levels], **kwargs)
  1382. :func:`~matplotlib.pyplot.contour` and
  1383. :func:`~matplotlib.pyplot.contourf` draw contour lines and
  1384. filled contours, respectively. Except as noted, function
  1385. signatures and return values are the same for both versions.
  1386. Parameters
  1387. ----------
  1388. X, Y : array-like, optional
  1389. The coordinates of the values in *Z*.
  1390. *X* and *Y* must both be 2-D with the same shape as *Z* (e.g.
  1391. created via :func:`numpy.meshgrid`), or they must both be 1-D such
  1392. that ``len(X) == M`` is the number of columns in *Z* and
  1393. ``len(Y) == N`` is the number of rows in *Z*.
  1394. If not given, they are assumed to be integer indices, i.e.
  1395. ``X = range(M)``, ``Y = range(N)``.
  1396. Z : array-like(N, M)
  1397. The height values over which the contour is drawn.
  1398. levels : int or array-like, optional
  1399. Determines the number and positions of the contour lines / regions.
  1400. If an int *n*, use *n* data intervals; i.e. draw *n+1* contour
  1401. lines. The level heights are automatically chosen.
  1402. If array-like, draw contour lines at the specified levels.
  1403. The values must be in increasing order.
  1404. Returns
  1405. -------
  1406. c : `~.contour.QuadContourSet`
  1407. Other Parameters
  1408. ----------------
  1409. corner_mask : bool, optional
  1410. Enable/disable corner masking, which only has an effect if *Z* is
  1411. a masked array. If ``False``, any quad touching a masked point is
  1412. masked out. If ``True``, only the triangular corners of quads
  1413. nearest those points are always masked out, other triangular
  1414. corners comprising three unmasked points are contoured as usual.
  1415. Defaults to ``rcParams['contour.corner_mask']``, which defaults to
  1416. ``True``.
  1417. colors : color string or sequence of colors, optional
  1418. The colors of the levels, i.e. the lines for `.contour` and the
  1419. areas for `.contourf`.
  1420. The sequence is cycled for the levels in ascending order. If the
  1421. sequence is shorter than the number of levels, it's repeated.
  1422. As a shortcut, single color strings may be used in place of
  1423. one-element lists, i.e. ``'red'`` instead of ``['red']`` to color
  1424. all levels with the same color. This shortcut does only work for
  1425. color strings, not for other ways of specifying colors.
  1426. By default (value *None*), the colormap specified by *cmap*
  1427. will be used.
  1428. alpha : float, optional
  1429. The alpha blending value, between 0 (transparent) and 1 (opaque).
  1430. cmap : str or `.Colormap`, optional
  1431. A `.Colormap` instance or registered colormap name. The colormap
  1432. maps the level values to colors.
  1433. Defaults to :rc:`image.cmap`.
  1434. If given, *colors* take precedence over *cmap*.
  1435. norm : `~matplotlib.colors.Normalize`, optional
  1436. If a colormap is used, the `.Normalize` instance scales the level
  1437. values to the canonical colormap range [0, 1] for mapping to
  1438. colors. If not given, the default linear scaling is used.
  1439. vmin, vmax : float, optional
  1440. If not *None*, either or both of these values will be supplied to
  1441. the `.Normalize` instance, overriding the default color scaling
  1442. based on *levels*.
  1443. origin : {*None*, 'upper', 'lower', 'image'}, optional
  1444. Determines the orientation and exact position of *Z* by specifying
  1445. the position of ``Z[0, 0]``. This is only relevant, if *X*, *Y*
  1446. are not given.
  1447. - *None*: ``Z[0, 0]`` is at X=0, Y=0 in the lower left corner.
  1448. - 'lower': ``Z[0, 0]`` is at X=0.5, Y=0.5 in the lower left corner.
  1449. - 'upper': ``Z[0, 0]`` is at X=N+0.5, Y=0.5 in the upper left
  1450. corner.
  1451. - 'image': Use the value from :rc:`image.origin`. Note: The value
  1452. *None* in the rcParam is currently handled as 'lower'.
  1453. extent : (x0, x1, y0, y1), optional
  1454. If *origin* is not *None*, then *extent* is interpreted as
  1455. in :func:`matplotlib.pyplot.imshow`: it gives the outer
  1456. pixel boundaries. In this case, the position of Z[0,0]
  1457. is the center of the pixel, not a corner. If *origin* is
  1458. *None*, then (*x0*, *y0*) is the position of Z[0,0], and
  1459. (*x1*, *y1*) is the position of Z[-1,-1].
  1460. This keyword is not active if *X* and *Y* are specified in
  1461. the call to contour.
  1462. locator : ticker.Locator subclass, optional
  1463. The locator is used to determine the contour levels if they
  1464. are not given explicitly via *levels*.
  1465. Defaults to `~.ticker.MaxNLocator`.
  1466. extend : {'neither', 'both', 'min', 'max'}, optional
  1467. Unless this is 'neither', contour levels are automatically
  1468. added to one or both ends of the range so that all data
  1469. are included. These added ranges are then mapped to the
  1470. special colormap values which default to the ends of the
  1471. colormap range, but can be set via
  1472. :meth:`matplotlib.colors.Colormap.set_under` and
  1473. :meth:`matplotlib.colors.Colormap.set_over` methods.
  1474. xunits, yunits : registered units, optional
  1475. Override axis units by specifying an instance of a
  1476. :class:`matplotlib.units.ConversionInterface`.
  1477. antialiased : bool, optinal
  1478. Enable antialiasing, overriding the defaults. For
  1479. filled contours, the default is *True*. For line contours,
  1480. it is taken from :rc:`lines.antialiased`.
  1481. Nchunk : int >= 0, optional
  1482. If 0, no subdivision of the domain. Specify a positive integer to
  1483. divide the domain into subdomains of *nchunk* by *nchunk* quads.
  1484. Chunking reduces the maximum length of polygons generated by the
  1485. contouring algorithm which reduces the rendering workload passed
  1486. on to the backend and also requires slightly less RAM. It can
  1487. however introduce rendering artifacts at chunk boundaries depending
  1488. on the backend, the *antialiased* flag and value of *alpha*.
  1489. linewidths : float or sequence of float, optional
  1490. *Only applies to* `.contour`.
  1491. The line width of the contour lines.
  1492. If a number, all levels will be plotted with this linewidth.
  1493. If a sequence, the levels in ascending order will be plotted with
  1494. the linewidths in the order specified.
  1495. Defaults to :rc:`lines.linewidth`.
  1496. linestyles : {*None*, 'solid', 'dashed', 'dashdot', 'dotted'}, optional
  1497. *Only applies to* `.contour`.
  1498. If *linestyles* is *None*, the default is 'solid' unless the lines
  1499. are monochrome. In that case, negative contours will take their
  1500. linestyle from :rc:`contour.negative_linestyle` setting.
  1501. *linestyles* can also be an iterable of the above strings
  1502. specifying a set of linestyles to be used. If this
  1503. iterable is shorter than the number of contour levels
  1504. it will be repeated as necessary.
  1505. hatches : List[str], optional
  1506. *Only applies to* `.contourf`.
  1507. A list of cross hatch patterns to use on the filled areas.
  1508. If None, no hatching will be added to the contour.
  1509. Hatching is supported in the PostScript, PDF, SVG and Agg
  1510. backends only.
  1511. Notes
  1512. -----
  1513. 1. :func:`~matplotlib.pyplot.contourf` differs from the MATLAB
  1514. version in that it does not draw the polygon edges.
  1515. To draw edges, add line contours with
  1516. calls to :func:`~matplotlib.pyplot.contour`.
  1517. 2. contourf fills intervals that are closed at the top; that
  1518. is, for boundaries *z1* and *z2*, the filled region is::
  1519. z1 < Z <= z2
  1520. There is one exception: if the lowest boundary coincides with
  1521. the minimum value of the *Z* array, then that minimum value
  1522. will be included in the lowest interval.
  1523. """