jupyter matplotlib,以及seaborn在lun

2018-11-20  本文已影响0人  思君颜如玉

最好直接在/user/share/fonts/安装相关字体,如果没有root权限可以在~/.local/share/fonts/安装相关字体
然后用如下代码发现字体的命名:
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, '系统可用的中文字体', '' * 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显示不了中文,多半是因为重新加载了sns的style设置
sns.set_style('white',{"font.sans-serif":['STXinwei']})
是设置了字体,不要省略{"font.sans-serif":['STXinwei']}这个;
也可以不要设置style

上一篇 下一篇

猜你喜欢

热点阅读