我无法让matplotlib
找到我知道在系统上安装的特定字体,并根据matplotlib.font_manager
显示为可用字体。当我指定某些权重在我的图中使用时,我指定的字体不被使用,链中也没有其他字体,而是使用了链下面的字体。
我可以用
验证这一点import matplotlib
import matplotlib.font_manager as font_manager
# The backend doesn't matter
#matplotlib.use('agg')
font = {'family': 'sans-serif',
'sans-serif': ['Open Sans', 'Lucida Grande', 'Avenir', 'Arial', 'sans-serif']}
matplotlib.rc('font', **font)
for font_weight in range(100, 1000, 100):
matplotlib.rc('font', **{'weight': font_weight})
print str(font_manager.FontProperties().get_weight()) + ": " + font_manager.FontProperties().get_name()
产生
100: Open Sans
200: Open Sans
300: Arial
400: Arial
500: Arial
600: Open Sans
700: Open Sans
800: Open Sans
900: Open Sans
对应于我在我的数字中得到的结果:对于'正常',或者对于300到500之间的权重,而不是获得Open Sans,我得到Arial。
这里发生了什么?为什么matplotlib
无法找到并使用我系统中存在的字体?
FWIW,我可以用
来检查字体管理器知道的内容import matplotlib
import matplotlib.font_manager as font_manager
import glob
the_files = font_manager.findSystemFonts(fontpaths=None)
# Using the_files = font_manager.OSXInstalledFonts() produces the same results
for font_name in ['Open Sans', 'Lucida Grande', 'Avenir', 'Arial']:
print('')
print('** ' + font_name)
print("Names reported by font_manager for fonts in font_manager-found files with '" + font_name + "' in file name:")
for the_file in the_files:
if font_name.replace(' ', '').lower() in the_file.replace(' ', '').lower():
the_props = font_manager.FontProperties(fname=the_file)
print "\t" + the_props.get_name()
print('')
font_in_file_name = font_name.replace(' ', '')
print("Files found through file system with '" + font_in_file_name + "' in file name:")
for font_dir in ['/Users/Rax/Library/Fonts/', '/Library/Fonts/', '/System/Library/Fonts/']:
for file in glob.glob(font_dir + font_in_file_name + '*.*'):
print("\t" + file)
确认存在所需的字体
** Open Sans
Names reported by font_manager for fonts in font_manager-found files with 'Open Sans' in file name:
Open Sans
Open Sans
Open Sans
Open Sans
Open Sans
Open Sans
Open Sans
Open Sans
Open Sans
Open Sans
Files found through file system with 'OpenSans' in file name:
/Users/Rax/Library/Fonts/OpenSans-Bold.ttf
/Users/Rax/Library/Fonts/OpenSans-BoldItalic.ttf
/Users/Rax/Library/Fonts/OpenSans-ExtraBold.ttf
/Users/Rax/Library/Fonts/OpenSans-ExtraBoldItalic.ttf
/Users/Rax/Library/Fonts/OpenSans-Italic.ttf
/Users/Rax/Library/Fonts/OpenSans-Light.ttf
/Users/Rax/Library/Fonts/OpenSans-LightItalic.ttf
/Users/Rax/Library/Fonts/OpenSans-Regular.ttf
/Users/Rax/Library/Fonts/OpenSans-Semibold.ttf
/Users/Rax/Library/Fonts/OpenSans-SemiboldItalic.ttf
** Lucida Grande
Names reported by font_manager for fonts in font_manager-found files with 'Lucida Grande' in file name:
Files found through file system with 'LucidaGrande' in file name:
/System/Library/Fonts/LucidaGrande.ttc
** Avenir
Names reported by font_manager for fonts in font_manager-found files with 'Avenir' in file name:
Files found through file system with 'Avenir' in file name:
/System/Library/Fonts/Avenir Next Condensed.ttc
/System/Library/Fonts/Avenir Next.ttc
/System/Library/Fonts/Avenir.ttc
** Arial
Names reported by font_manager for fonts in font_manager-found files with 'Arial' in file name:
Arial
Arial Black
Arial
Arial
Arial Narrow
Arial Narrow
Arial Narrow
Arial Rounded MT Bold
Arial
Arial Unicode MS
Arial Narrow
Files found through file system with 'Arial' in file name:
/Library/Fonts/Arial Black.ttf
/Library/Fonts/Arial Bold Italic.ttf
/Library/Fonts/Arial Bold.ttf
/Library/Fonts/Arial Italic.ttf
/Library/Fonts/Arial Narrow Bold Italic.ttf
/Library/Fonts/Arial Narrow Bold.ttf
/Library/Fonts/Arial Narrow Italic.ttf
/Library/Fonts/Arial Narrow.ttf
/Library/Fonts/Arial Rounded Bold.ttf
/Library/Fonts/Arial Unicode.ttf
/Library/Fonts/Arial.ttf
/Library/Fonts/ArialHB.ttc
我正在运行OSX 10.9.1,并且如上面的代码中所述,使用OSXInstalledFonts
代替findSystemFonts
会产生相同的结果。
答案 0 :(得分:1)
输出the_props._file
时会发生什么?
import matplotlib
import matplotlib.font_manager as font_manager
import glob
the_files = font_manager.findSystemFonts(fontpaths=None)
# Using the_files = font_manager.OSXInstalledFonts() produces the same results
for font_name in ['Open Sans', 'Lucida Grande', 'Avenir', 'Arial']:
print('')
print('** ' + font_name)
print("Names reported by font_manager for fonts in font_manager-found files with '" + font_name + "' in file name:")
for the_file in the_files:
if font_name.replace(' ', '').lower() in the_file.replace(' ', '').lower():
the_props = font_manager.FontProperties(fname=the_file)
print "\t" + the_props.get_name(),
print '\t\t', the_props._file
print('')
font_in_file_name = font_name.replace(' ', '')
print("Files found through file system with '" + font_in_file_name + "' in file name:")
for font_dir in ['/Users/Rax/Library/Fonts/', '/Library/Fonts/', '/System/Library/Fonts/']:
for file in glob.glob(font_dir + font_in_file_name + '*.*'):
print("\t" + file)
我输出
** Open Sans
Names reported by font_manager for fonts in font_manager-found files with 'Open Sans' in file name:
Files found through file system with 'OpenSans' in file name:
** Lucida Grande
Names reported by font_manager for fonts in font_manager-found files with 'Lucida Grande' in file name:
Files found through file system with 'LucidaGrande' in file name:
/System/Library/Fonts/LucidaGrande.ttc
** Avenir
Names reported by font_manager for fonts in font_manager-found files with 'Avenir' in file name:
Files found through file system with 'Avenir' in file name:
/System/Library/Fonts/Avenir Next Condensed.ttc
/System/Library/Fonts/Avenir Next.ttc
/System/Library/Fonts/Avenir.ttc
** Arial
Names reported by font_manager for fonts in font_manager-found files with 'Arial' in file name:
Arial /Library/Fonts/Arial Bold Italic.ttf
Arial Black /Library/Fonts/Arial Black.ttf
Arial /Library/Fonts/Microsoft/Arial.ttf
Arial /Library/Fonts/Microsoft/Arial Italic.ttf
Arial /Library/Fonts/Arial Italic.ttf
Arial /Library/Fonts/Microsoft/Arial Bold Italic.ttf
Arial /Library/Fonts/Arial.ttf
Arial Narrow /Library/Fonts/Arial Narrow.ttf
Arial Narrow /Library/Fonts/Arial Narrow Bold.ttf
Arial Narrow /Library/Fonts/Arial Narrow Italic.ttf
Arial /Library/Fonts/Microsoft/Arial Bold.ttf
Arial Rounded MT Bold /Library/Fonts/Arial Rounded Bold.ttf
Arial /Library/Fonts/Arial Bold.ttf
Arial Unicode MS /Library/Fonts/Arial Unicode.ttf
Arial Narrow /Library/Fonts/Arial Narrow Bold Italic.ttf
Files found through file system with 'Arial' in file name:
/Library/Fonts/Arial Black.ttf
/Library/Fonts/Arial Bold Italic.ttf
/Library/Fonts/Arial Bold.ttf
/Library/Fonts/Arial Italic.ttf
/Library/Fonts/Arial Narrow Bold Italic.ttf
/Library/Fonts/Arial Narrow Bold.ttf
/Library/Fonts/Arial Narrow Italic.ttf
/Library/Fonts/Arial Narrow.ttf
/Library/Fonts/Arial Rounded Bold.ttf
/Library/Fonts/Arial Unicode.ttf
/Library/Fonts/Arial.ttf
/Library/Fonts/ArialHB.ttc
我在计算机上尝试了这个,我没有Open Sans或Avenir,但是Lucida Grande出现在“通过文件系统找到的文件......”循环中。但是,当我尝试绘图时,它看起来像Arial:http://nbviewer.ipython.org/gist/olgabot/8099973
我认为matplotlib
正在寻找/Library/Fonts
而不是/System/Library/Fonts
或/Users/.../Library/Fonts
中的字体。所以我的建议是将.ttf
文件复制到/Library/Fonts
,如果可以的话。如果没有,则this question指定如何从完整路径加载字体。
似乎.ttc
字体文件存在一些问题,所以我会尽可能坚持.ttf
。