contour.py 67 KB

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