Escaping in matplotlib.font_manager.findfont('sans\\-serif')

Hi,
  I wondered why the matplotlib.font_manager.rcParams contains sometimes
escaped minus signs in font names:

font_manager.rcParams['mathtext.sf']

'sans\\-serif'

font_manager.findfont('sans-serif')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1327, in findfont
    font = fontManager.findfont(prop, **kw)
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1172, in findfont
    prop = FontProperties(prop)
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 673, in __init__
    self.set_fontconfig_pattern(family)
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 893, in set_fontconfig_pattern
    for key, val in self._parse_fontconfig_pattern(pattern).items():
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 685, in _parse_fontconfig_pattern
    return parse_fontconfig_pattern(pattern)
  File "/usr/lib64/python2.7/site-packages/matplotlib/fontconfig_pattern.py", line 127, in parse
    raise ValueError("Could not parse font string: '%s'\n%s" % (pattern, e))
ValueError: Could not parse font string: 'sans-serif'
Expected end of text (at char 4), (line:1, col:5)

font_manager.findfont('sans\\-serif')

'/usr/share/fonts/mathematica-fonts/Vera.ttf'

And there is nothing to read in the help string for this function, definitely
a place where the escaping should be explained.

help(font_manager.findfont)

Anyway, I think the requirement for the escaping should be removed, it should be
escaped internally in the routine. So why is it so? For historic reasons? :wink:

Isn't the escaping a cause for the seemingly missing fonts, like reported here?
http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg22860.html

I am also getting:

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmb10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmss10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

The font to be used is assembled from several fields in the rcParams. I guess from:

[x for x in font_manager.rcParams.keys() if x.startswith('font')]

['font.cursive', 'font.family', 'font.fantasy', 'font.monospace', 'font.sans-serif', 'font.serif', 'font.size', 'font.stretch', 'font.style', 'font.variant', 'font.weight']

[x for x in font_manager.rcParams.values() if x.startswith('cm')]

/usr/lib64/python2.7/site-packages/matplotlib/__init__.py:658: UserWarning: svg.embed_char_paths is deprecated and replaced with svg.fonttype; please use the latter.
  warnings.warn(self.msg_depr % (key, alt))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'startswith'

[x for x in font_manager.rcParams.items() if x[1]=='cm']

[('mathtext.fontset', 'cm')]

What else is used to give out the 'cmb10', 'cmtt10' and 'cmss10'?

font_manager.findfont('cmb10')

'/usr/share/fonts/mathematica-fonts/Vera.ttf'

font_manager.findfont('cmtt10')

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'/usr/share/fonts/mathematica-fonts/Vera.ttf'

font_manager.findfont('cmss10')

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmss10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'/usr/share/fonts/mathematica-fonts/Vera.ttf'

Thanks,
Martin

Hi,
   I wondered why the matplotlib.font_manager.rcParams contains sometimes
escaped minus signs in font names:

font_manager.rcParams['mathtext.sf']

'sans\\-serif'

font_manager.findfont('sans-serif')

Traceback (most recent call last):
   File "<stdin>", line 1, in<module>
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1327, in findfont
     font = fontManager.findfont(prop, **kw)
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1172, in findfont
     prop = FontProperties(prop)
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 673, in __init__
     self.set_fontconfig_pattern(family)
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 893, in set_fontconfig_pattern
     for key, val in self._parse_fontconfig_pattern(pattern).items():
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 685, in _parse_fontconfig_pattern
     return parse_fontconfig_pattern(pattern)
   File "/usr/lib64/python2.7/site-packages/matplotlib/fontconfig_pattern.py", line 127, in parse
     raise ValueError("Could not parse font string: '%s'\n%s" % (pattern, e))
ValueError: Could not parse font string: 'sans-serif'
Expected end of text (at char 4), (line:1, col:5)

font_manager.findfont('sans\\-serif')

'/usr/share/fonts/mathematica-fonts/Vera.ttf'

And there is nothing to read in the help string for this function, definitely
a place where the escaping should be explained.

help(font_manager.findfont)

Anyway, I think the requirement for the escaping should be removed, it should be
escaped internally in the routine. So why is it so? For historic reasons? :wink:

I've never seen this happen before. Are you setting "mathtext.sf" in a matplotlibrc file or in Python code? Can you provide either so I can see if I can reproduce the problem here?

Isn't the escaping a cause for the seemingly missing fonts, like reported here?
http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg22860.html

No -- that appears to be caused by either using the wrong font name or having an outdated font cache.

I am also getting:

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmb10'] not found. Falling back to Bitstream Vera Sans
   (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans
   (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmss10'] not found. Falling back to Bitstream Vera Sans
   (prop.get_family(), self.defaultFamily[fontext]))

The font to be used is assembled from several fields in the rcParams. I guess from:

[x for x in font_manager.rcParams.keys() if x.startswith('font')]

['font.cursive', 'font.family', 'font.fantasy', 'font.monospace', 'font.sans-serif', 'font.serif', 'font.size', 'font.stretch', 'font.style', 'font.variant', 'font.weight']

[x for x in font_manager.rcParams.values() if x.startswith('cm')]

/usr/lib64/python2.7/site-packages/matplotlib/__init__.py:658: UserWarning: svg.embed_char_paths is deprecated and replaced with svg.fonttype; please use the latter.
   warnings.warn(self.msg_depr % (key, alt))
Traceback (most recent call last):
   File "<stdin>", line 1, in<module>
AttributeError: 'int' object has no attribute 'startswith'

[x for x in font_manager.rcParams.items() if x[1]=='cm']

[('mathtext.fontset', 'cm')]
What else is used to give out the 'cmb10', 'cmtt10' and 'cmss10'?

These are hardcoded in the mathtext engine and used when `mathtext.fontset` is set to `cm`.

Have you tried deleting your font cache? (~/.matplotlib/fontList.cache)

Mike

···

On 03/26/2012 04:36 PM, Martin Mokrejs wrote:

Hi Michael,
  I was playing with python directly, so:

$ python
Python 2.7.2 (default, Mar 10 2012, 02:39:52)
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

from matplotlib import font_manager
font_manager.path

'/home/mmokrejs/.fonts'

font_manager.rcParams

{'axes.formatter.use_locale': False, 'figure.subplot.right': 0.9, 'mathtext.cal': 'cursive', 'font.fantasy': ['Comic Sans MS', 'Chicago', 'Charcoal', 'ImpactWestern', 'fantasy'], 'xtick.minor.pad': 4, 'tk.pythoninspect': False, 'image.aspect': 'equal', 'font.cursive': ['Apple Chancery', 'Textile', 'Zapf Chancery', 'Sand', 'cursive'], 'figure.subplot.hspace': 0.2, 'keymap.fullscreen': 'f', 'examples.directory': '/usr/share/doc/matplotlib-1.1.0/examples', 'xtick.direction': 'in', 'axes.facecolor': 'w', 'mathtext.fontset': 'cm', 'ytick.direction': 'in', 'keymap.pan': 'p', 'path.snap': True, 'axes.axisbelow': False, 'lines.markersize': 6, 'figure.dpi': 80, 'text.usetex': False, 'image.origin': 'upper', 'patch.edgecolor': 'k', 'legend.labelspacing': 0.5, 'ps.useafm': False, 'mathtext.bf': 'serif:bold', 'lines.solid_joinstyle': 'round', 'font.monospace': ['Bitstream Vera Sans Mono', 'DejaVu Sans Mono', 'Andale Mono', 'Nimbus Mono L', 'Courier New', 'Courier', 'Fixed', 'Terminal', 'monospace
'], 'xtick.minor.size': 2, 'axes.formatter.limits': [-7, 7], 'figure.subplot.wspace': 0.2, 'savefig.edgecolor': 'w', 'image.cmap': 'jet', 'lines.marker': 'None', 'tk.window_focus': False, 'backend.qt4': 'PyQt4', 'font.serif': ['Bitstream Vera Serif', 'DejaVu Serif', 'New Century Schoolbook', 'Century Schoolbook L', 'Utopia', 'ITC Bookman', 'Bookman', 'Nimbus Roman No9 L', 'Times New Roman', 'Times', 'Palatino', 'Charter', 'serif'], 'savefig.facecolor': 'w', 'ytick.minor.size': 2, 'font.stretch': 'normal', 'text.dvipnghack': None, 'ytick.color': 'k', 'svg.fonttype': 'path', 'lines.linestyle': '-', 'xtick.color': 'k', 'xtick.major.pad': 4, 'patch.facecolor': 'b', 'figure.figsize': [8.0, 6.0], 'axes.linewidth': 1.0, 'keymap.zoom': 'o', 'legend.handletextpad': 0.8, 'mathtext.fallback_to_cm': True, 'lines.linewidth': 1.0, 'savefig.dpi': 100, 'verbose.fileo': 'sys.stdout', 'svg.image_noscale': False, 'docstring.hardcopy': False, 'font.size': 12, 'ps.fonttype': 3, 'path.simplify': True, 'key
map.all_axes': 'a', 'polaraxes.grid': True, 'text.hinting': True, 'pdf.compression': 6, 'grid.linewidth': 0.5, 'legend.frameon': True, 'figure.autolayout': False, 'figure.facecolor': '0.75', 'ps.usedistiller': False, 'legend.isaxes': True, 'figure.edgecolor': 'w', 'mathtext.tt': 'monospace', 'contour.negative_linestyle': 'dashed', 'image.interpolation': 'bilinear', 'lines.markeredgewidth': 0.5, 'keymap.home': ['h', 'r', 'home'], 'axes3d.grid': True, 'axes.edgecolor': 'k', 'legend.shadow': False, 'axes.titlesize': 'large', 'backend': 'GTKAgg', 'xtick.major.size': 4, 'keymap.xscale': ['k', 'L'], 'legend.fontsize': 'large', 'lines.solid_capstyle': 'projecting', 'mathtext.it': 'serif:italic', 'keymap.save': 's', 'font.variant': 'normal', 'toolbar': 'toolbar2', 'xtick.labelsize': 'medium', 'axes.unicode_minus': True, 'ps.distiller.res': 6000, 'axes.labelweight': 'normal', 'pdf.fonttype': 3, 'patch.linewidth': 1.0, 'pdf.inheritcolor': False, 'axes.color_cycle': ['b', 'g', 'r', 'c', 'm', 'y'
, 'k'], 'lines.dash_capstyle': 'butt', 'lines.color': 'b', 'figure.subplot.top': 0.9, 'pdf.use14corefonts': False, 'legend.markerscale': 1.0, 'patch.antialiased': True, 'font.style': 'normal', 'keymap.forward': ['right', 'v'], 'backend_fallback': True, 'legend.fancybox': False, 'grid.linestyle': ':', 'savefig.extension': 'auto', 'text.color': 'k', 'mathtext.rm': 'serif', 'legend.loc': 'upper right', 'interactive': False, 'cairo.format': 'png', 'savefig.orientation': 'portrait', 'svg.image_inline': True, 'ytick.major.size': 4, 'axes.grid': False, 'plugins.directory': '.matplotlib_plugins', 'grid.color': 'k', 'timezone': 'UTC', 'ytick.major.pad': 4, 'legend.borderpad': 0.4, 'examples.download': False, 'lines.dash_joinstyle': 'round', 'datapath': '/usr/share/matplotlib/mpl-data', 'lines.antialiased': True, 'text.latex.unicode': False, 'legend.handleheight': 0.7, 'image.lut': 256, 'figure.subplot.bottom': 0.1, 'text.latex.preamble': [''], 'legend.numpoints': 2, 'legend.handlelength': 2.0,
'keymap.grid': 'g', 'font.sans-serif': ['Bitstream Vera Sans', 'DejaVu Sans', 'Lucida Grande', 'Verdana', 'Geneva', 'Lucid', 'Arial', 'Helvetica', 'Avant Garde', 'sans-serif'], 'axes.labelcolor': 'k', 'font.family': 'sans-serif', 'axes.labelsize': 'medium', 'ytick.minor.pad': 4, 'legend.borderaxespad': 0.5, 'mathtext.sf': 'sans\\-serif', 'axes.hold': True, 'verbose.level': 'silent', 'mathtext.default': 'it', 'figure.subplot.left': 0.125, 'legend.columnspacing': 2.0, 'text.latex.preview': False, 'font.weight': 'normal', 'keymap.yscale': 'l', 'image.resample': False, 'agg.path.chunksize': 0, 'path.simplify_threshold': 0.1111111111111111, 'ytick.labelsize': 'medium', 'ps.papersize': 'letter', 'svg.embed_char_paths': True, 'keymap.back': ['left', 'c', 'backspace']}

font_manager.findfont('cmb10')

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmb10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'/usr/share/fonts/mathematica-fonts/Vera.ttf'

font_manager.findfont('Utopia')

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['Utopia'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'/usr/share/fonts/mathematica-fonts/Vera.ttf'

font_manager.findfont('monospace')

'/usr/share/fonts/mathematica-fonts/VeraMono.ttf'

font_manager.findfont('sans-serif')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1327, in findfont
    font = fontManager.findfont(prop, **kw)
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1172, in findfont
    prop = FontProperties(prop)
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 673, in __init__
    self.set_fontconfig_pattern(family)
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 893, in set_fontconfig_pattern
    for key, val in self._parse_fontconfig_pattern(pattern).items():
  File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 685, in _parse_fontconfig_pattern
    return parse_fontconfig_pattern(pattern)
  File "/usr/lib64/python2.7/site-packages/matplotlib/fontconfig_pattern.py", line 127, in parse
    raise ValueError("Could not parse font string: '%s'\n%s" % (pattern, e))
ValueError: Could not parse font string: 'sans-serif'
Expected end of text (at char 4), (line:1, col:5)

font_manager.findfont('sans\\-serif')

'/usr/share/fonts/mathematica-fonts/Vera.ttf'

[x for x in font_manager.rcParams.keys() if x.startswith('font')]

['font.cursive', 'font.family', 'font.fantasy', 'font.monospace', 'font.sans-serif', 'font.serif', 'font.size', 'font.stretch', 'font.style', 'font.variant', 'font.weight']

[x for x in font_manager.rcParams.values() if x.startswith('cm')]

/usr/lib64/python2.7/site-packages/matplotlib/__init__.py:658: UserWarning: svg.embed_char_paths is deprecated and replaced with svg.fonttype; please use the latter.
  warnings.warn(self.msg_depr % (key, alt))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'startswith'

[x for x in font_manager.rcParams.values() if str(x).startswith('cm')]

['cm']

[x for x in font_manager.rcParams.items() if x[1]=='cm']

[('mathtext.fontset', 'cm')]

[x for x in font_manager.rcParams.items() if x[1]=='10']

[]

[x for x in font_manager.rcParams.items() if x[1]=='bold']

[]

font_manager.findfont('cmb10')

'/usr/share/fonts/mathematica-fonts/Vera.ttf'

font_manager.findfont('cmtt10')

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'/usr/share/fonts/mathematica-fonts/Vera.ttf'

font_manager.findfont('cmss10')

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmss10'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
'/usr/share/fonts/mathematica-fonts/Vera.ttf'

Michael Droettboom wrote:

Hi,
   I wondered why the matplotlib.font_manager.rcParams contains sometimes
escaped minus signs in font names:

font_manager.rcParams['mathtext.sf']

'sans\\-serif'

font_manager.findfont('sans-serif')

Traceback (most recent call last):
   File "<stdin>", line 1, in<module>
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1327, in findfont
     font = fontManager.findfont(prop, **kw)
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 1172, in findfont
     prop = FontProperties(prop)
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 673, in __init__
     self.set_fontconfig_pattern(family)
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 893, in set_fontconfig_pattern
     for key, val in self._parse_fontconfig_pattern(pattern).items():
   File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 685, in _parse_fontconfig_pattern
     return parse_fontconfig_pattern(pattern)
   File "/usr/lib64/python2.7/site-packages/matplotlib/fontconfig_pattern.py", line 127, in parse
     raise ValueError("Could not parse font string: '%s'\n%s" % (pattern, e))
ValueError: Could not parse font string: 'sans-serif'
Expected end of text (at char 4), (line:1, col:5)

font_manager.findfont('sans\\-serif')

'/usr/share/fonts/mathematica-fonts/Vera.ttf'

And there is nothing to read in the help string for this function, definitely
a place where the escaping should be explained.

help(font_manager.findfont)

Anyway, I think the requirement for the escaping should be removed, it should be
escaped internally in the routine. So why is it so? For historic reasons? :wink:

I've never seen this happen before. Are you setting "mathtext.sf" in a
matplotlibrc file or in Python code? Can you provide either so I can
see if I can reproduce the problem here?

See above. In python. I just wondered whether it was typo or escaping. Still think
it should be hidden to the user.

Isn't the escaping a cause for the seemingly missing fonts, like reported here?
http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg22860.html

No -- that appears to be caused by either using the wrong font name or
having an outdated font cache.

I am also getting:

/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmb10'] not found. Falling back to Bitstream Vera Sans
   (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to Bitstream Vera Sans
   (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py:1216: UserWarning: findfont: Font family ['cmss10'] not found. Falling back to Bitstream Vera Sans
   (prop.get_family(), self.defaultFamily[fontext]))

The font to be used is assembled from several fields in the rcParams. I guess from:

[x for x in font_manager.rcParams.keys() if x.startswith('font')]

['font.cursive', 'font.family', 'font.fantasy', 'font.monospace', 'font.sans-serif', 'font.serif', 'font.size', 'font.stretch', 'font.style', 'font.variant', 'font.weight']

[x for x in font_manager.rcParams.values() if x.startswith('cm')]

/usr/lib64/python2.7/site-packages/matplotlib/__init__.py:658: UserWarning: svg.embed_char_paths is deprecated and replaced with svg.fonttype; please use the latter.
   warnings.warn(self.msg_depr % (key, alt))
Traceback (most recent call last):
   File "<stdin>", line 1, in<module>
AttributeError: 'int' object has no attribute 'startswith'

[x for x in font_manager.rcParams.items() if x[1]=='cm']

[('mathtext.fontset', 'cm')]
What else is used to give out the 'cmb10', 'cmtt10' and 'cmss10'?

These are hardcoded in the mathtext engine and used when
`mathtext.fontset` is set to `cm`.

So my case (defaults).

Have you tried deleting your font cache? (~/.matplotlib/fontList.cache)

Ah, thanks for the full PATH, I couldn't find it. :wink: Yes, removing the file helped
me as well to get rid of the UserWarning message.

Martin

···

On 03/26/2012 04:36 PM, Martin Mokrejs wrote: