首页 > 技术文章 > Matplotlib如何显示中文

chenqionghe 2020-02-16 17:02 原文

问题:matplotlib不能渲染中文

想设定为中文字体,网上搜索的方法几乎都是下面这样,已经把字体拷贝到了程序目录下了,然而并没有生效

 plt.rcParams [ font.sans-serif'] = ['SimHei.ttf'] 

解决

设置字体路径字体名

import matplotlib.font_manager as font_manager
from matplotlib import pyplot as plt

font_dirs = ['你的字体路径']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
plt.rcParams['font.family'] = '你的字体名

下面是本人用的代码

import matplotlib.font_manager as font_manager
font_dirs = ['/Users/chenqionghe/me/project/python/python-test/jupyter/myfonts']
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
font_list = font_manager.createFontList(font_files)
font_manager.fontManager.ttflist.extend(font_list)
plt.rcParams['font.family'] = 'SimHei'

推荐阅读