Matplotlib解决中文显示问题


默认情况下,matplotlib是无法显示中文的,主要原因是没有指定中文字体(文件)。因此,有两种方法,一种是在脚本中指出字体文件,当然这是暂时的,另一种是修改matplotlib的配置文件。

在代码中指定中文字体

指定确定路径文件

该方法需要知道字体文件的路径 :

  • Windows字体文件存储路径为:C:\Windows\Fonts

    image-20200330184515727
  • MacOS字体文件存储路径为:/System/Library/Fonts/Library/Fonts

  • Linux字体文件存储路径一般为:/usr/share/fonts~/.local/share/fonts

指定其他位置字体文件易可,字体文件为 .tff

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib

zhfont = matplotlib.font_manager.FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf')
plt.xlabel(u"横座标xlabel",fontproperties=zhfont) #有中文出现的情况,需要u'内容'

使用系统默认中文字体

该方法需要提前查看本地都有哪些中文字体

#coding:utf-8
import matplotlib.pyplot as plt

'''
##  【matplotlib.pyplot在图形上显示中文】
plt.rcParams['font.sans-serif']=['STSong']     ## 中文宋体
plt.rcParams['font.sans-serif']=['SimHei']     ## 中文黑体
plt.rcParams['font.sans-serif']=['Kaiti']      ## 中文楷体
plt.rcParams['font.sans-serif']=['Lisu']       ## 中文隶书
plt.rcParams['font.sans-serif']=['FangSong']   ## 中文仿宋
plt.rcParams['font.sans-serif']=['YouYuan']    ## 中文幼圆
## 以上选择一条语句即可
'''
plt.rcParams['font.sans-serif']=['SimHei']     ##用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False       ##用来正常显示负号
#有中文出现的情况,需要u'内容'

修改配置文件 一劳永逸

第一步:查询当前使用的默认样式文件路径

import matplotlib
matplotlib.matplotlib_fname() #将会获得matplotlib默认样式文件所在文件夹

Windows显示为:

'C:\\Users\lemon\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc'

Windows设置

方法一:替换文件

  1. 打开'C:\Users\lemon\Anaconda3\lib\site-packages\matplotlib\mpl-data'路径下的matplotlibrc文件,找到以#font.serif开头这一行,删除#取消这一行的注释,保存文件
font.serif          : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
  1. 找到中文字体替换掉matplotlib字体库中字体(偷梁换柱)

C:\Windows\Fonts某一中文字体copy到C:\Users\lemon\Anaconda3\Lib\site-packages\matplotlib\mpl-data\fonts\ttf中,并删除ttf文件夹中存在的一个字体文件,并用文件名重命名copy文件

这一步的作用其实就是将matplotlib中一个默认的字体替换为我们复制过来的中文字体,将这个中文字体命名改为matplotlib中有的字体名

3) 重启python环境

方法二:修改配置文件

1)打开'C:\Users\lemon\Anaconda3\lib\site-packages\matplotlib\mpl-data'路径下的matplotlibrc文件,找到以font.sans-serif以及#font.family:开头这两行,删除#取消注释,并在font.sans-serif后添加C:\Windows\Fonts中存在的中文字体名,保存文件

2)重启python环境

MacOS设置

1)添加中文字体(*.tff)

参照本节第一步输出的路径,将matplotlibrc替换为fonts

cd <python库文件路径>/site-packages/matplotlib/mpl-data/fonts/tff

下载或拷贝来的字体文件粘贴到tff目录中

2)删除掉 ~/.matplotlib/下所有缓存文件

rm -rf ~/.matplotlib/*.cache
  1. 修改文件
vi ~/.matplotlib/matplotlibrc

backend:TkAgg
font.family :sans-serif
font.sans-serif: <添加的字体文件名>
axes.unicode_minus:False

Linux设置

参考Mac

1) 拷贝字体到 usr/share/fonts下:

sudo cp ~/<字体文件名>.tff /usr/share/fonts/<字体文件名>.tff

2) 删除~/.cache/matplotlib中的缓存文件

rm -rf ~/.cache/matplotlib/*
  1. 修改配置文件
sudo find -name matplotlibrc

返回结果:

./.virtualenvs/ai/lib/python3.5/site-packages/matplotlib/mpl-data/matplotbrc

打开配置文件:

vi ./.virtualenvs/ai/lib/python3.5/site-packages/matplotlib/mpl-data/matplotbrc

修改其中三项如下:

font.family :sans-serif
font.sans-serif: <添加的字体文件名>
axes.unicode_minus:False

参考文献\博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章