首页 > 技术文章 > JupyterLab 显示中文字体

fade-color 2021-05-02 14:51 原文

查看当前系统JupyterLab支持的所有字体

from matplotlib.font_manager import FontManager

mpl_fonts = set(f.name for f in FontManager().ttflist)

print('all font list get from matplotlib.font_manager:')
for f in sorted(mpl_fonts):
    print('\t' + f)

打印结果如下:

image-20210417092307128

选择一款中文字体即可,这里我选择的是苹果的冬青黑体:"Hiragino Sans GB",windows用户可以选择黑体(SimHei)

删除缓存文件

Mac:

  • cd ~/.matplotlib
    rm -r *
    

Windows:

  • 进入到自己的用户目录文件夹下,在资源管理器的“查看”选项卡中勾选“显示隐藏文件及目录”即可看到.matplotlib文件夹,或者直接在资源管理器的路径栏中在路径后面手动输入.matplotlib后回车即可进入该文件夹

  • 然后把该文件夹内的资源删除即可。

修改matplotlibrc配置文件

Mac:

  • 输入vim ~/.matplotlib/matplotlibrc

    然后将文件内容修改为:

    font.family         : sans-serif
    font.sans-serif         : Hiragino Sans GB
    axes.unicode_minus  : False
    

    font.sans-serif后面改为自己选择的字体即可。

Win:

  • .matplotlib文件夹中新建matplotlibrc文件,把上面内容粘贴进去后.txt后缀名删掉即可。

重启JupyterLab内核

接下来重启一下JupyterLab内核即可看到效果。

推荐阅读