已经有很多线程(Force matplotlibrc to use Arial (path of font),How to load .ttf file in matplotlib using mpl.rcParams?)。在我的案例中似乎没有任何帮助。我的系统上安装了许多字体并在matplotlib中注册。当我在shell中时,一切似乎都有效:
>>> import matplotlib as mpl
>>> from matplotlib import font_manager
>>> path = '/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf'
>>> props = font_manager.FontProperties(fname=path)
>>> print props
family=Times New Roman ... file=/usr/.../Times_New_Roman.ttf ...
>>> mpl.rcParams['font.family'] = props.get_name()
>>> mpl.rcParams
RcParams({...,u'font.family': [u'Times New Roman'], ... })
然而,当我尝试在脚本中执行相同操作(并且我已经尝试了十几次)时,我得到:
/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:1282: UserWarning: findfont: Font family [u'Times New Roman'] not found.
为什么?