@@ -1431,59 +1431,56 @@ def __init__(self,
1431
1431
1432
1432
self ._draggable = None
1433
1433
1434
- def _get_xy (self , renderer , x , y , s ):
1435
- if isinstance (s , tuple ):
1436
- s1 , s2 = s
1437
- else :
1438
- s1 , s2 = s , s
1439
- if s1 == 'data' :
1434
+ def _get_xy (self , renderer , xy , coords ):
1435
+ x , y = xy
1436
+ xcoord , ycoord = coords if isinstance (coords , tuple ) else (coords , coords )
1437
+ if xcoord == 'data' :
1440
1438
x = float (self .convert_xunits (x ))
1441
- if s2 == 'data' :
1439
+ if ycoord == 'data' :
1442
1440
y = float (self .convert_yunits (y ))
1443
- return self ._get_xy_transform (renderer , s ).transform ((x , y ))
1441
+ return self ._get_xy_transform (renderer , coords ).transform ((x , y ))
1444
1442
1445
- def _get_xy_transform (self , renderer , s ):
1443
+ def _get_xy_transform (self , renderer , coords ):
1446
1444
1447
- if isinstance (s , tuple ):
1448
- s1 , s2 = s
1445
+ if isinstance (coords , tuple ):
1446
+ xcoord , ycoord = coords
1449
1447
from matplotlib .transforms import blended_transform_factory
1450
- tr1 = self ._get_xy_transform (renderer , s1 )
1451
- tr2 = self ._get_xy_transform (renderer , s2 )
1452
- tr = blended_transform_factory (tr1 , tr2 )
1453
- return tr
1454
- elif callable (s ):
1455
- tr = s (renderer )
1448
+ tr1 = self ._get_xy_transform (renderer , xcoord )
1449
+ tr2 = self ._get_xy_transform (renderer , ycoord )
1450
+ return blended_transform_factory (tr1 , tr2 )
1451
+ elif callable (coords ):
1452
+ tr = coords (renderer )
1456
1453
if isinstance (tr , BboxBase ):
1457
1454
return BboxTransformTo (tr )
1458
1455
elif isinstance (tr , Transform ):
1459
1456
return tr
1460
1457
else :
1461
- raise RuntimeError (
1462
- f"Unexpected return type from callable: "
1463
- f"expected BboxBase or Transform, but got { type (tr ).__name__ } . " )
1464
- elif isinstance (s , Artist ):
1465
- bbox = s .get_window_extent (renderer )
1458
+ raise TypeError (
1459
+ f"xycoords callable must return a BboxBase or Transform, not a "
1460
+ f"{ type (tr ).__name__ } " )
1461
+ elif isinstance (coords , Artist ):
1462
+ bbox = coords .get_window_extent (renderer )
1466
1463
return BboxTransformTo (bbox )
1467
- elif isinstance (s , BboxBase ):
1468
- return BboxTransformTo (s )
1469
- elif isinstance (s , Transform ):
1470
- return s
1471
- elif not isinstance (s , str ):
1472
- raise RuntimeError (
1473
- f"Unexpected type for 'xycoords'. This must be one of str, ( str, str) , "
1474
- f"Artist, Transform, or callable, but got { type (s ).__name__ } . " )
1475
-
1476
- if s == 'data' :
1464
+ elif isinstance (coords , BboxBase ):
1465
+ return BboxTransformTo (coords )
1466
+ elif isinstance (coords , Transform ):
1467
+ return coords
1468
+ elif not isinstance (coords , str ):
1469
+ raise TypeError (
1470
+ f"'xycoords' must be an instance of str, tuple[ str, str], Artist , "
1471
+ f"Transform, or Callable, not a { type (coords ).__name__ } " )
1472
+
1473
+ if coords == 'data' :
1477
1474
return self .axes .transData
1478
- elif s == 'polar' :
1475
+ elif coords == 'polar' :
1479
1476
from matplotlib .projections import PolarAxes
1480
1477
tr = PolarAxes .PolarTransform ()
1481
1478
trans = tr + self .axes .transData
1482
1479
return trans
1483
1480
1484
- s_ = s .split ()
1481
+ s_ = coords .split ()
1485
1482
if len (s_ ) != 2 :
1486
- raise ValueError (f"{ s !r} is not a valid coordinate" )
1483
+ raise ValueError (f"{ coords !r} is not a valid coordinate" )
1487
1484
1488
1485
bbox0 , xy0 = None , None
1489
1486
@@ -1495,16 +1492,11 @@ def _get_xy_transform(self, renderer, s):
1495
1492
bbox0 = self .figure .bbox
1496
1493
elif bbox_name == "axes" :
1497
1494
bbox0 = self .axes .bbox
1498
- # elif bbox_name == "bbox":
1499
- # if bbox is None:
1500
- # raise RuntimeError("bbox is specified as a coordinate but "
1501
- # "never set")
1502
- # bbox0 = self._get_bbox(renderer, bbox)
1503
1495
1504
1496
if bbox0 is not None :
1505
1497
xy0 = bbox0 .p0
1506
1498
elif bbox_name == "offset" :
1507
- xy0 = self ._get_ref_xy (renderer )
1499
+ xy0 = self ._get_position_xy (renderer )
1508
1500
1509
1501
if xy0 is not None :
1510
1502
# reference x, y in display coordinate
@@ -1528,24 +1520,7 @@ def _get_xy_transform(self, renderer, s):
1528
1520
return tr .translate (ref_x , ref_y )
1529
1521
1530
1522
else :
1531
- raise ValueError (f"{ s !r} is not a valid coordinate" )
1532
-
1533
- def _get_ref_xy (self , renderer ):
1534
- """
1535
- Return x, y (in display coordinates) that is to be used for a reference
1536
- of any offset coordinate.
1537
- """
1538
- return self ._get_xy (renderer , * self .xy , self .xycoords )
1539
-
1540
- # def _get_bbox(self, renderer):
1541
- # if hasattr(bbox, "bounds"):
1542
- # return bbox
1543
- # elif hasattr(bbox, "get_window_extent"):
1544
- # bbox = bbox.get_window_extent()
1545
- # return bbox
1546
- # else:
1547
- # raise ValueError("A bbox instance is expected but got %s" %
1548
- # str(bbox))
1523
+ raise ValueError (f"{ coords !r} is not a valid coordinate" )
1549
1524
1550
1525
def set_annotation_clip (self , b ):
1551
1526
"""
@@ -1572,8 +1547,7 @@ def get_annotation_clip(self):
1572
1547
1573
1548
def _get_position_xy (self , renderer ):
1574
1549
"""Return the pixel position of the annotated point."""
1575
- x , y = self .xy
1576
- return self ._get_xy (renderer , x , y , self .xycoords )
1550
+ return self ._get_xy (renderer , self .xy , self .xycoords )
1577
1551
1578
1552
def _check_xy (self , renderer = None ):
1579
1553
"""Check whether the annotation at *xy_pixel* should be drawn."""
0 commit comments