最好直接在/user/share/fonts/安裝相關(guān)字體髓涯,如果沒有root權(quán)限可以在~/.local/share/fonts/安裝相關(guān)字體
然后用如下代碼發(fā)現(xiàn)字體的命名:
from matplotlib.font_manager import FontManager
import subprocess
fm = FontManager()
mat_fonts = set(f.name for f in fm.ttflist)
print(mat_fonts)
output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
print( '' * 10, '系統(tǒng)可用的中文字體', '' * 10)
print (output)
zh_fonts = set(f.split(',', 1)[0] for f in output.decode('utf-8').split('\n'))
available = mat_fonts & zh_fonts
print ('' * 10, '可用的字體', '' * 10)
for f in available:
print (f)
********** 可用的字體 **********
STXinwei
最后就可以在
from matplotlib import pyplot as plt
plt.rc('font', family='STXinwei', size=7)中使用了
如果在matplot可以顯示中文虐秋,但是在seaborn顯示不了中文,多半是因?yàn)橹匦录虞d了sns的style設(shè)置
sns.set_style('white',{"font.sans-serif":['STXinwei']})
是設(shè)置了字體纱控,不要省略{"font.sans-serif":['STXinwei']}這個(gè)期贫;
也可以不要設(shè)置style