Find the point in the contour plot that is closest to ``(x, y)``. This method does not support filled contours. Parameters ---------- x, y : float The reference point. indices : list of int or None, default: None Indices of c
(self, x, y, indices=None, pixel=True)
| 1228 | return idx_level_min, idx_vtx_min, proj_min |
| 1229 | |
| 1230 | def find_nearest_contour(self, x, y, indices=None, pixel=True): |
| 1231 | """ |
| 1232 | Find the point in the contour plot that is closest to ``(x, y)``. |
| 1233 | |
| 1234 | This method does not support filled contours. |
| 1235 | |
| 1236 | Parameters |
| 1237 | ---------- |
| 1238 | x, y : float |
| 1239 | The reference point. |
| 1240 | indices : list of int or None, default: None |
| 1241 | Indices of contour levels to consider. If None (the default), all |
| 1242 | levels are considered. |
| 1243 | pixel : bool, default: True |
| 1244 | If *True*, measure distance in pixel (screen) space, which is |
| 1245 | useful for manual contour labeling; else, measure distance in axes |
| 1246 | space. |
| 1247 | |
| 1248 | Returns |
| 1249 | ------- |
| 1250 | path : int |
| 1251 | The index of the path that is closest to ``(x, y)``. Each path corresponds |
| 1252 | to one contour level. |
| 1253 | subpath : int |
| 1254 | The index within that closest path of the subpath that is closest to |
| 1255 | ``(x, y)``. Each subpath corresponds to one unbroken contour line. |
| 1256 | index : int |
| 1257 | The index of the vertices within that subpath that are closest to |
| 1258 | ``(x, y)``. |
| 1259 | xmin, ymin : float |
| 1260 | The point in the contour plot that is closest to ``(x, y)``. |
| 1261 | d2 : float |
| 1262 | The squared distance from ``(xmin, ymin)`` to ``(x, y)``. |
| 1263 | """ |
| 1264 | segment = index = d2 = None |
| 1265 | |
| 1266 | with ExitStack() as stack: |
| 1267 | if not pixel: |
| 1268 | # _find_nearest_contour works in pixel space. We want axes space, so |
| 1269 | # effectively disable the transformation here by setting to identity. |
| 1270 | stack.enter_context(self._cm_set( |
| 1271 | transform=mtransforms.IdentityTransform())) |
| 1272 | |
| 1273 | i_level, i_vtx, (xmin, ymin) = self._find_nearest_contour((x, y), indices) |
| 1274 | |
| 1275 | if i_level is not None: |
| 1276 | cc_cumlens = np.cumsum( |
| 1277 | [*map(len, self._paths[i_level]._iter_connected_components())]) |
| 1278 | segment = cc_cumlens.searchsorted(i_vtx, "right") |
| 1279 | index = i_vtx if segment == 0 else i_vtx - cc_cumlens[segment - 1] |
| 1280 | d2 = (xmin-x)**2 + (ymin-y)**2 |
| 1281 | |
| 1282 | return (i_level, segment, index, xmin, ymin, d2) |
| 1283 | |
| 1284 | @artist.allow_rasterization |
| 1285 | def draw(self, renderer): |