matplotlib 中文显示问题的解决办法

来源:互联网 发布:ld3320语音识别算法 编辑:程序博客网 时间:2024/06/05 22:43

  • 下载支持中文的 .ttf 字体文件
  • 将文件放到如下任一目录:
    • linux
      • ”/usr/X11R6/lib/X11/fonts/TTF/”
      • “/usr/X11/lib/X11/fonts”
      • “/usr/share/fonts/”
      • “/usr/local/share/fonts/”
      • “/usr/lib/openoffice/share/fonts/truetype/”
    • Mac
      • ”/Library/Fonts/”
      • “/Network/Library/Fonts/”
      • “/System/Library/Fonts/”
      • “/opt/local/share/fonts”
    • 也可以放到 “/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/fonts/ttf”
  • 清理缓存
rm ~/.cache/matplotlib/fontList.cache
  • 编写代码时,添加如下几行:
import matplotlib.pyplot as pltplt.rcParams['font.sans-serif']=['xxxxx'] # xxxxx 处是你的字体名
  • PS:
    字体名可以通过fc-list :lang=zh
    或者
# -*- coding: utf-8 -*-from matplotlib.font_manager import FontManagerimport subprocessfm = FontManager()for f in fm.ttflist:    print f.name

来获取
ss

0 0