Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e102eea

Browse files
committed
Compactify the implementation of ContourLabeler.add_label_near.
The old implementation was quite expansive; compactify it a bit. Also reformat the docs.
1 parent da66560 commit e102eea

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

lib/matplotlib/contour.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -455,64 +455,49 @@ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
455455
def add_label_near(self, x, y, inline=True, inline_spacing=5,
456456
transform=None):
457457
"""
458-
Add a label near the point (x, y). If transform is None
459-
(default), (x, y) is in data coordinates; if transform is
460-
False, (x, y) is in display coordinates; otherwise, the
461-
specified transform will be used to translate (x, y) into
462-
display coordinates.
458+
Add a label near the point ``(x, y)``.
463459
464460
Parameters
465461
----------
466462
x, y : float
467463
The approximate location of the label.
468-
469464
inline : bool, default: True
470465
If *True* remove the segment of the contour beneath the label.
471-
472466
inline_spacing : int, default: 5
473467
Space in pixels to leave on each side of label when placing
474468
inline. This spacing will be exact for labels at locations where
475469
the contour is straight, less so for labels on curved contours.
470+
transform : `.Transform` or `False`, default: ``self.axes.transData``
471+
A transform applied to ``(x, y)`` before labeling. The default
472+
causes ``(x, y)`` to be interpreted as data coordinates. `False`
473+
is a synonym for `.IdentityTransform`; i.e. ``(x, y)`` should be
474+
interpreted as display coordinates.
476475
"""
477476

478477
if transform is None:
479478
transform = self.axes.transData
480-
481479
if transform:
482480
x, y = transform.transform((x, y))
483481

484482
# find the nearest contour _in screen units_
485483
conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
486484
x, y, self.labelIndiceList)[:5]
487485

488-
# The calc_label_rot_and_inline routine requires that (xmin, ymin)
486+
# calc_label_rot_and_inline() requires that (xmin, ymin)
489487
# be a vertex in the path. So, if it isn't, add a vertex here
490-
491-
# grab the paths from the collections
492-
paths = self.collections[conmin].get_paths()
493-
# grab the correct segment
494-
active_path = paths[segmin]
495-
# grab its vertices
496-
lc = active_path.vertices
497-
# sort out where the new vertex should be added data-units
488+
paths = self.collections[conmin].get_paths() # paths of correct coll.
489+
lc = paths[segmin].vertices # vertices of correct segment
490+
# Where should the new vertex be added in data-units?
498491
xcmin = self.axes.transData.inverted().transform([xmin, ymin])
499-
# if there isn't a vertex close enough
500492
if not np.allclose(xcmin, lc[imin]):
501-
# insert new data into the vertex list
502-
lc = np.row_stack([lc[:imin], xcmin, lc[imin:]])
503-
# replace the path with the new one
493+
# No vertex is close enough, so add a new point in the vertices and
494+
# replace the path by the new one.
495+
lc = np.insert(lc, imin, xcmin, axis=0)
504496
paths[segmin] = mpath.Path(lc)
505497

506498
# Get index of nearest level in subset of levels used for labeling
507499
lmin = self.labelIndiceList.index(conmin)
508500

509-
# Coordinates of contour
510-
paths = self.collections[conmin].get_paths()
511-
lc = paths[segmin].vertices
512-
513-
# In pixel/screen space
514-
slc = self.axes.transData.transform(lc)
515-
516501
# Get label width for rotating labels and breaking contours
517502
lw = self.get_label_width(self.labelLevelList[lmin],
518503
self.labelFmt, self.labelFontSizeList[lmin])
@@ -522,7 +507,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
522507

523508
# Figure out label rotation.
524509
rotation, nlc = self.calc_label_rot_and_inline(
525-
slc, imin, lw, lc if inline else None, inline_spacing)
510+
self.axes.transData.transform(lc), # to pixel space.
511+
imin, lw, lc if inline else None, inline_spacing)
526512

527513
self.add_label(xmin, ymin, rotation, self.labelLevelList[lmin],
528514
self.labelCValueList[lmin])
@@ -1316,7 +1302,7 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
13161302
# Nonetheless, improvements could probably be made.
13171303

13181304
if indices is None:
1319-
indices = list(range(len(self.levels)))
1305+
indices = range(len(self.levels))
13201306

13211307
d2min = np.inf
13221308
conmin = None

0 commit comments

Comments
 (0)