我在獲取matplotlib
時遇到問題,無法找到我知道的系統上安裝的特定字體,並且根據matplotlib.font_manager
顯示可用字體。當我在圖中指定某些重量時,我指定的字體不會使用,鏈中的其他字體也不會使用,而是使用鏈條下方的字體。爲什麼'matplotlib'找不到指定字體的特定字體?
我可以
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
會產生相同的結果。
我們得到相同的行爲。奇怪的是,我可以強制'matplotlob'通過'matplotlib.rcParams ['font.family'] ='Open Sans''來查找和使用Open Sans。 – orome
用'font = {'family':'sans-serif','sans-serif':['Open Sans']}'替換'font',即不列出其他家庭 - 也適用。 Matplotlib正在使用定位字體的啓發式存在一些意想不到的問題(也許有點兒錯誤)。 – orome