@@ -455,64 +455,49 @@ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
455
455
def add_label_near (self , x , y , inline = True , inline_spacing = 5 ,
456
456
transform = None ):
457
457
"""
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)``.
463
459
464
460
Parameters
465
461
----------
466
462
x, y : float
467
463
The approximate location of the label.
468
-
469
464
inline : bool, default: True
470
465
If *True* remove the segment of the contour beneath the label.
471
-
472
466
inline_spacing : int, default: 5
473
467
Space in pixels to leave on each side of label when placing
474
468
inline. This spacing will be exact for labels at locations where
475
469
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.
476
475
"""
477
476
478
477
if transform is None :
479
478
transform = self .axes .transData
480
-
481
479
if transform :
482
480
x , y = transform .transform ((x , y ))
483
481
484
482
# find the nearest contour _in screen units_
485
483
conmin , segmin , imin , xmin , ymin = self .find_nearest_contour (
486
484
x , y , self .labelIndiceList )[:5 ]
487
485
488
- # The calc_label_rot_and_inline routine requires that (xmin, ymin)
486
+ # calc_label_rot_and_inline() requires that (xmin, ymin)
489
487
# 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?
498
491
xcmin = self .axes .transData .inverted ().transform ([xmin , ymin ])
499
- # if there isn't a vertex close enough
500
492
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 )
504
496
paths [segmin ] = mpath .Path (lc )
505
497
506
498
# Get index of nearest level in subset of levels used for labeling
507
499
lmin = self .labelIndiceList .index (conmin )
508
500
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
-
516
501
# Get label width for rotating labels and breaking contours
517
502
lw = self .get_label_width (self .labelLevelList [lmin ],
518
503
self .labelFmt , self .labelFontSizeList [lmin ])
@@ -522,7 +507,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
522
507
523
508
# Figure out label rotation.
524
509
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 )
526
512
527
513
self .add_label (xmin , ymin , rotation , self .labelLevelList [lmin ],
528
514
self .labelCValueList [lmin ])
@@ -1316,7 +1302,7 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
1316
1302
# Nonetheless, improvements could probably be made.
1317
1303
1318
1304
if indices is None :
1319
- indices = list ( range (len (self .levels ) ))
1305
+ indices = range (len (self .levels ))
1320
1306
1321
1307
d2min = np .inf
1322
1308
conmin = None
0 commit comments