2013-12-22 94 views
1

我在獲取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會產生相同的結果。

回答

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 

我想這一點,我的電腦上,我沒有任何打開三世或艾文莉,但很好的可讀性,在「文件露面發現通過文件系統..「循環。然而,當我嘗試過的情節,它看起來像Arial字體:http://nbviewer.ipython.org/gist/olgabot/8099973

我覺得matplotlib正在尋找/Library/Fonts字體,而不是/System/Library/Fonts/Users/.../Library/Fonts。所以我的建議是將.ttf文件複製到/Library/Fonts,如果可以的話。如果不是,則this question指定如何從完整路徑加載字體。

它似乎也有.ttc字體文件的一些問題,所以我會堅持.ttf只要有可能。

+0

我們得到相同的行爲。奇怪的是,我可以強制'matplotlob'通過'matplotlib.rcParams ['font.family'] ='Open Sans''來查找和使用Open Sans。 – orome

+0

用'font = {'family':'sans-serif','sans-serif':['Open Sans']}'替換'font',即不列出其他家庭 - 也適用。 Matplotlib正在使用定位字體的啓發式存在一些意想不到的問題(也許有點兒錯誤)。 – orome

相關問題