@@ -242,10 +242,20 @@ def get_label_coords(self, distances, XX, YY, ysize, lw):
242
242
x , y = XX [ind ][hysize ], YY [ind ][hysize ]
243
243
return x , y , ind
244
244
245
+ def _get_nth_label_width (self , nth ):
246
+ """Return the width of the *nth* label, in pixels."""
247
+ fig = self .axes .figure
248
+ return (
249
+ text .Text (0 , 0 ,
250
+ self .get_text (self .labelLevelList [nth ], self .labelFmt ),
251
+ figure = fig ,
252
+ size = self .labelFontSizeList [nth ],
253
+ fontproperties = self .labelFontProps )
254
+ .get_window_extent (mpl .tight_layout .get_renderer (fig )).width )
255
+
256
+ @_api .deprecated ("3.5" )
245
257
def get_label_width (self , lev , fmt , fsize ):
246
- """
247
- Return the width of the label in points.
248
- """
258
+ """Return the width of the label in points."""
249
259
if not isinstance (lev , str ):
250
260
lev = self .get_text (lev , fmt )
251
261
fig = self .axes .figure
@@ -498,11 +508,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
498
508
lmin = self .labelIndiceList .index (conmin )
499
509
500
510
# Get label width for rotating labels and breaking contours
501
- lw = self .get_label_width (self .labelLevelList [lmin ],
502
- self .labelFmt , self .labelFontSizeList [lmin ])
503
- # lw is in points.
504
- lw *= self .axes .figure .dpi / 72 # scale to screen coordinates
505
- # now lw in pixels
511
+ lw = self ._get_nth_label_width (lmin )
506
512
507
513
# Figure out label rotation.
508
514
rotation , nlc = self .calc_label_rot_and_inline (
@@ -534,14 +540,15 @@ def labels(self, inline, inline_spacing):
534
540
else :
535
541
add_label = self .add_label
536
542
537
- for icon , lev , fsize , cvalue in zip (
538
- self .labelIndiceList , self .labelLevelList ,
539
- self .labelFontSizeList , self .labelCValueList ):
543
+ for idx , (icon , lev , cvalue ) in enumerate (zip (
544
+ self .labelIndiceList ,
545
+ self .labelLevelList ,
546
+ self .labelCValueList ,
547
+ )):
540
548
541
549
con = self .collections [icon ]
542
550
trans = con .get_transform ()
543
- lw = self .get_label_width (lev , self .labelFmt , fsize )
544
- lw *= self .axes .figure .dpi / 72 # scale to screen coordinates
551
+ lw = self ._get_nth_label_width (idx )
545
552
additions = []
546
553
paths = con .get_paths ()
547
554
for segNum , linepath in enumerate (paths ):
0 commit comments