Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab9e58c

Browse files
committed
Improve and simplify text width computation in clabel.
The previous heuristic was wrong for "wide" fonts, and was also overly complicated, as the Text class already has the relevant functionality.
1 parent 1c20824 commit ab9e58c

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

lib/matplotlib/contour.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
import matplotlib.font_manager as font_manager
1818
import matplotlib.text as text
1919
import matplotlib.cbook as cbook
20-
import matplotlib.mathtext as mathtext
2120
import matplotlib.patches as mpatches
22-
import matplotlib.texmanager as texmanager
2321
import matplotlib.transforms as mtransforms
2422

2523
# Import needed for adding manual selection capability to clabel
@@ -243,20 +241,12 @@ def get_label_width(self, lev, fmt, fsize):
243241
"""
244242
if not isinstance(lev, str):
245243
lev = self.get_text(lev, fmt)
246-
lev, ismath = text.Text()._preprocess_math(lev)
247-
if ismath == 'TeX':
248-
lw, _, _ = (texmanager.TexManager()
249-
.get_text_width_height_descent(lev, fsize))
250-
elif ismath:
251-
if not hasattr(self, '_mathtext_parser'):
252-
self._mathtext_parser = mathtext.MathTextParser('agg')
253-
_, _, _, _, _, img, _ = self._mathtext_parser.parse(
254-
lev, dpi=72, prop=self.labelFontProps)
255-
_, lw = np.shape(img) # at dpi=72, the units are PostScript points
256-
else:
257-
# width is much less than "font size"
258-
lw = len(lev) * fsize * 0.6
259-
return lw
244+
fig = self.axes.figure
245+
width = (text.Text(0, 0, lev, figure=fig,
246+
size=fsize, fontproperties=self.labelFontProps)
247+
.get_window_extent(fig.canvas.get_renderer()).width)
248+
width *= 72 / fig.dpi
249+
return width
260250

261251
def set_label_props(self, label, text, color):
262252
"""Set the label properties - color, fontsize, text."""

0 commit comments

Comments
 (0)