matplotlib解决不显示中文问题

下载微软雅黑字体

找到字体文件夹

import matplotlib print(matplotlib.matplotlib_fname()) 

以个人为例子,输出为css

C:\Users\adobe\app\anaconda3\lib\site-packages\matplotlib\mpl-data\matplotlibrc

因此说,以我使用的anaconda为例子,它在lib\site-packages\matplotlib\mpl-data\matplotlibrc文件夹下面.
进入fonts\ttf目录,把第一步下载的msyh.ttf放到该目录下面python

 

 

修改matplotlibrc文件

使用任何一个文件编辑器(推荐vscode),修改该文件,经过ctrl+f搜索找到git

#axes.unicode_minus : True ## use unicode for the minus symbol #font.family : sans-serif #font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif 

分别修改成如下三行github

axes.unicode_minus : False ## use unicode for the minus symbol font.family : Microsoft YaHei font.sans-serif : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif 

解释:缓存

  1. 首先三行都须要删除第一个#,取消注释
  2. 第一行,修改True为False,是为了正常显示负号
  3. 第二行和第三行是为了使用微软雅黑做为默认字体

删除缓存

使用下面的代码,获取缓存文件夹.app

import matplotlib print(matplotlib.get_cachedir()) 

个人输出为编辑器

C:\Users\adobe\.matplotlib 

因此,通常在用户目录的matplotlib,删除该目录下的全部文件测试

 
image.png


重启Python便可(意思是关闭全部正在运行的Python窗口,而后从新打开,要否则没法生效)字体

 

测试

使用以下的简单代码进行测试spa

# coding:utf-8 import matplotlib.pyplot as plt plt.plot((1,2,3),(4,3,-1)) plt.xlabel(u'横坐标') # python3 能够不用u,已是默认了 plt.ylabel(u'纵坐标') plt.show()

相关文章
相关标签/搜索