contour.py 68 KB

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