centOS7.4+python2.7.5+matplotlib中文乱码问题解决

其中字体文件可以自行下载或者直接从windows目录下拷贝。
windows下步骤相同,唯一变化的是对应文件目录不同,根据自己情况进行查找即可。
详细步骤如下:
(1)上传.ttf文件至matplotlib的font目录(/usr/lib64/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf)下;
(2)上传.tff文件至系统font目录(/usr/share/fonts/)下;
(3)安装font-config(指令:yum install font-config);
(4)执行下列代码:
   
#    ! /usr/bin/env python
   # -*- coding: utf-8 -*-
   from matplotlib.font_manager import FontManager
   import subprocess

   fm = FontManager()
   mat_fonts = set(f.name for f in fm.ttflist)

  output = subprocess.check_output('fc-list :lang=zh -f "%{family}\n"', shell=True)
  # print '*' * 10, '系统可用的中文字体', '*' * 10
  # print output
  zh_fonts = set(f.split(',', 1)[0] for f in output.split('\n'))
  available = mat_fonts & zh_fonts

  print '*' * 10, '可用的字体', '*' * 10
  for f in available:
    print f
(5)根据代码中获取的字体名称,修改matplotlibrc文件(目录:/usr/lib64/python2.7/site-packages/matplotlib/mpl-data)内容
   font.family         : serif
   font.serif : 获取到的字体名, serif

参考链接:Linux下解决matplotlib中文乱码的方法

                  mac如何matplotlib中文乱码问题

发布了28 篇原创文章 · 获赞 4 · 访问量 7402

猜你喜欢

转载自blog.csdn.net/JuicyActiveGilbert/article/details/87711885
今日推荐