@@ -242,10 +242,20 @@ def get_label_coords(self, distances, XX, YY, ysize, lw):
242242 x , y = XX [ind ][hysize ], YY [ind ][hysize ]
243243 return x , y , ind
244244
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" )
245257 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."""
249259 if not isinstance (lev , str ):
250260 lev = self .get_text (lev , fmt )
251261 fig = self .axes .figure
@@ -498,11 +508,7 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
498508 lmin = self .labelIndiceList .index (conmin )
499509
500510 # 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 )
506512
507513 # Figure out label rotation.
508514 rotation , nlc = self .calc_label_rot_and_inline (
@@ -534,14 +540,15 @@ def labels(self, inline, inline_spacing):
534540 else :
535541 add_label = self .add_label
536542
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+ )):
540548
541549 con = self .collections [icon ]
542550 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 )
545552 additions = []
546553 paths = con .get_paths ()
547554 for segNum , linepath in enumerate (paths ):
0 commit comments