Skip to content

Commit 01da70d

Browse files
committed
DOC : improved add_label_near
- added some in-line comments to explain what is going on
1 parent 1e0b084 commit 01da70d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Diff for: lib/matplotlib/contour.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -562,23 +562,38 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
562562
exact for labels at locations where the contour is
563563
straight, less so for labels on curved contours.
564564
"""
565-
565+
# if 'do the default thing' use the axes data transform
566566
if transform is None:
567567
transform = self.ax.transData
568568

569+
# if transform is 'falsey' don't do any transform (this is a
570+
# really awful API but is what we have)
569571
if transform:
572+
# The point of this transform is to get the location
573+
# of label position into screen units
570574
x, y = transform.transform_point((x, y))
571575

576+
# find the nearest contour _in screen units_
572577
conmin, segmin, imin, xmin, ymin = self.find_nearest_contour(
573578
x, y, self.labelIndiceList)[:5]
574579

575580
# The calc_label_rot_and_inline routine requires that (xmin,ymin)
576581
# be a vertex in the path. So, if it isn't, add a vertex here
582+
583+
# grab the paths from the collections
577584
paths = self.collections[conmin].get_paths()
578-
lc = paths[segmin].vertices
585+
# grab the correct segment
586+
active_path = paths[segmin]
587+
# grab it's verticies
588+
lc = active_path.vertices
589+
# sort out where the new vertex should be added in the path's data
590+
# units
579591
xcmin = self.ax.transData.inverted().transform_point([xmin, ymin])
592+
# if there isn't a vertex close enough
580593
if not np.allclose(xcmin, lc[imin]):
594+
# insert new data into the vertex list
581595
lc = np.r_[lc[:imin], np.array(xcmin)[None, :], lc[imin:]]
596+
# replace the path with the new one
582597
paths[segmin] = mpath.Path(lc)
583598

584599
# Get index of nearest level in subset of levels used for labeling

0 commit comments

Comments
 (0)