matplotlib显示中文

来源:互联网 发布:淘宝管控记录违规记录 编辑:程序博客网 时间:2024/06/06 02:28

原因

__大致就是matplotlib库中没有中文字体。1
我安装的anaconda,这是对应的matplotlib的字体所在文件夹(怎么找到matplotlib配置文件夹所在,下面解决方案会叙述,easy man~)。__

C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

解决方案

现在整理一下我认为比较好的解决方案:

每次编写代码时进行参数设置
#coding:utf-8import matplotlib.pyplot as pltplt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签plt.rcParams['axes.unicode_minus']=False #用来正常显示负号#有中文出现的情况,需要u'内容'
一劳永逸(推荐)
import matplotlibmatplotlib.matplotlib_fname() #将会获得matplotlib包所在文件夹然后进入C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data该文件夹下就能看到matplotlibrc配置文件。21)打开该配置文件,找到下面这行:#font.serif : Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif然后,将前面的注释去掉!2)找中文字体放到matplotlib字体库中。在Windows文件夹下:C:\Windows\Fonts\Microsoft YaHei UI复制该字体,然后粘贴到C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data\fonts\ttf文件夹,并给它重命名为Vera.ttf。注明:这一步的作用其实就是将matplotlib中一个默认的字体替换为我们复制过来的中文字体,将这个中文字体命名改为matplotlib中有的字体名。
原创粉丝点击