Python的matplotlib库画图不能显示中文问题解决

有两种解决办法:
一种是在代码里设置为能显示中文的字体,如微软雅黑(msyh.ttf)和黑体(simsun.ttc)
如下在要画图的代码前添加:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:WindowsFontsmsyh.ttf",size=10)
plt.figure()
plt.title('标题',fontproperties = font)
plt.xlabel('横坐标',fontproperties = font)
plt.ylabel('纵坐标',fontproperties = font)
plt.show()
 
另一种是在文件里修改,就不用每次在写代码都要重新设置,省的那么麻烦
首先在D:PythonLibsite-packagesmatplotlibmpl-data下找到matplotlibrc文件修改

1,找到

#font.family          : sans-serif

去掉注释

2,找到

#font.sans-serif     : Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

修改为

font.sans-serif     : Microsoft YaHei ,Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
即去掉注释,并在配置值中添加 Microsoft YaHei 
然后在C:WindowsFonts     下找到微软雅黑,点进去,选择常规(msyh.ttf)
复制到 D:PythonLibsite-packagesmatplotlibmpl-datafonts tf文件夹下即可
最后在运行代码处添加
plt.rcParams[ 'font.sans-serif'] = ['Microsoft YaHei' ]
原文地址:https://www.cnblogs.com/CQUTWH/p/5930365.html