contour.py 67 KB

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