解决Matplotlib中文乱码

1. 问题描述

用Matplotlib作图,使用中文会出现乱码,如下图红框所示:

plt.xlabel('t')
plt.ylabel('文件大小')

image-20220602165105905

2. 解决方法

修改Matplotlib配置文件matplotlibrc,运行以下代码找到配置文件的路径。

import matplotlib
matplotlib.matplotlib_fname()

配置文件路径为C:\Anaconda3\lib\site-packages\matplotlib\mpl-data\matplotlibrc

打开配置文件,找到#font.family

#font.family:  sans-serif
#font.style:   normal
#font.variant: normal
#font.weight:  normal
#font.stretch: normal
#font.size:    10.0

#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

取消font.familyfont.serif注释,并将字体Microsoft YaHei添加到font.family

font.family:Microsoft YaHei, sans-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

设置字体:

import matplotlib.pyplot as plt

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

3. Ubuntu服务器

在本地解决了,但在Ubuntu服务器(英文版)运行,再次出现了中文乱码。原因是系统中没有中文字体。

查看方法:用命令fc-list :lang=zh(需要先安装apt-get install fontconfig)列出系统已安装的字体。

3.1 安装中文字体

从Windows找一个中文字体(路径在C:\Windows\Fonts)上传到服务器/usr/share/fonts/目录下。举例,Windows微软雅黑字体,

msyh.ttc                                                                                 
msyhbd.ttc                                                                               
msyhl.ttc

拷贝到服务器上,运行fc-list :lang=zh就有:

# fc-list :lang=zh
/usr/share/fonts/msyh.ttc: Microsoft YaHei UI:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/msyhl.ttc: Microsoft YaHei,微软雅黑,Microsoft YaHei Light,微软雅黑 Light:style=Light,Regular
/usr/share/fonts/msyh.ttc: Microsoft YaHei,微软雅黑:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/usr/share/fonts/msyhbd.ttc: Microsoft YaHei UI:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/usr/share/fonts/msyhbd.ttc: Microsoft YaHei,微软雅黑:style=Bold,Negreta,tučné,fed,Fett,Έντονα,Negrita,Lihavoitu,Gras,Félkövér,Grassetto,Vet,Halvfet,Pogrubiony,Negrito,Полужирный,Fet,Kalın,Krepko,Lodia
/usr/share/fonts/msyhl.ttc: Microsoft YaHei UI,Microsoft YaHei UI Light:style=Light,Regular

还是不行。

3.2

查询matplotlib系统所有字体,发现刚才安装的微软雅黑并没有在里面。

>>> from matplotlib.font_manager import fontManager
>>> fontManager.ttflist

/usr/local/lib/python3.8/dist-packages/matplotlib/mpl-data/fonts/ttf/

/usr/local/lib/python3.8/dist-packages/matplotlib/mpl-data/matplotlibrc

取消font.familyfont.serif注释,并将字体Microsoft YaHei添加到font.familyfont.serif

font.family:Microsoft YaHei, sans-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
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['Microsoft YaHei']
本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-07-28 11:38

results matching ""

    No results matching ""