环境
Mac OS X Catalina 10.15
Python3.7
anaconda
方法一
网上各种通过下载simhei字体,配置matplotlib/matplotlibrc文件夹的方法过于繁琐,且年代久远,Mac OS X10.15已不适用该方法,如下图:
import matplotlib
# 查找字体路径
print(matplotlib.matplotlib_fname())
# 查找字体缓存路径
print(matplotlib.get_cachedir())
/Users/user/anaconda3/envs/opencv/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
/Users/user/.matplotlib
我们cd查看相关文件夹,可以看到:
cd /Users/user/.matplotlib
(base) user@xxxMacBook-Pro .matplotlib % ls
fontlist-v310.json fontlist-v330.json tex.cache
两个json文件中不再提供ttf字体的相关地址信息。
方法二
首先查看./Lib下有什么字体支持:
(base) user@xxxMacBook-Pro ~ % cd /Library/Fonts/
(base) user@xxxMacBook-Pro Fonts % ls
Arial Unicode.ttf
我们在matplotlib中plt时只需添加一句:
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
或者直接在plt时选择字体Arial Unicode:
plt.xlabel("原图像", fontproperties='Arial Unicode MS')
成功!!!