Matplotlib 西里尔字母支持

Matplotlib cyrillic support

我想将西里尔文用于 matplotlib 标签,所以我找到了 these 个示例,表明可以通过以下方式实现:

matplotlib.rcdefaults()
matplotlib.rcParams['font.family'] = 'fantasy'
matplotlib.rcParams['font.fantasy'] = 'Times New Roman', 'Ubuntu','Arial','Tahoma','Calibri'

当我尝试这样做时,出现以下错误:

/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:1279: UserWarning: findfont: Font family [u'fantasy'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

我正在使用 matplotlibprettyplotlib

Matpltlib 找不到字体,可能是格式错误或 matplotlib/mpl-data/fonts/ 文件夹下没有。尝试转换为 .ttf 并使用此代码进行调试:

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.font_manager as font_manager

path = '/home/username/fantasy.ttf'
prop = font_manager.FontProperties(fname=path)
mpl.rcParams['font.family'] = prop.get_name()

fig, ax = plt.subplots()
ax.set_title('Test text', fontproperties=prop, size=40)

您还可以查看所有可用的字体:

import matplotlib.font_manager
print matplotlib.font_manager.findSystemFonts(fontpaths=None)