centos下python使用matplotlib绘图出现中文乱码

目录

 

1. 当前环境

2. 放置文件到虚拟环境

3. 修改matplotlibrc

3.1 修改font.family

3.2 修改font.sans-serif

4. 删除缓存

5.代码调用片段

6.重启你的python应用


1. 当前环境

当前centos中,使用fc-list :lang=zh查询不到合适的系统中文字体文件

(venv) [root@VM_0_3_centos mpl-data]# fc-list :lang=zh
(venv) [root@VM_0_3_centos mpl-data]# fc-list
/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique

但是手上有一个可用的中文字体文件:simhei.ttf,来看看这个ttf文件的基本信息

(venv) [root@VM_0_3_centos mpl-data]# fc-scan ~/simhei.ttf 
Pattern has 25 elts (size 32)
        family: "SimHei"(s)
        familylang: "en"(s)
        style: "Normal"(s) "Regular"(s)
        stylelang: "fr"(s) "en"(s)
        fullname: "黑体"(s) "SimHei"(s)
        fullnamelang: "zh-cn"(s) "en"(s)
        slant: 0(i)(s)
        weight: 80(f)(s)
        width: 100(f)(s)
        spacing: 90(i)(s)
        foundry: "ZYEC"(s)
        file: "/root/simhei.ttf"(s)
        index: 0(i)(s)
        outline: True(s)
        scalable: True(s)
        charset: 
        0000: 00000000 ffffffff ffffffff 7fffffff 00000000 00830190 00800000 168c3703
        0001: 08080002 00000800 00002110 00000800 00000000 00000000 15554000 02000000
        0002: 00000000 00000000 00020000 00000002 00000000 00000000 02000e80 00000000
        0003: 00000000 00000000 00000000 00000000 fffe0000 fffe03fb 000003fb 00000000
        0004: ffff0002 ffffffff 0002ffff 00000000 00000000 00000000 00000000 00000000

 

2. 放置文件到虚拟环境

python代码是使用的工程的虚拟环境,虚拟环境下有自己的ttf文件。放置一个支持中文的ttf文件,例如simhei.ttf:

cd venv/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/
(venv) [root@VM_0_3_centos ttf]# ll

-rw-r--r-- 1 root root 9751960 Aug 28 01:17 simhei.ttf

3. 修改matplotlibrc

原来是没有simhei.ttf这个文件的,新增以后,需要在matplotlibrc中指出来,目录位置,

venv/lib/python3.7/site-packages/matplotlib/mpl-data

(venv) [root@VM_0_3_centos ttf]# cd ../..
(venv) [root@VM_0_3_centos mpl-data]# ll
total 52
drwxr-xr-x 5 root root  4096 Aug 20 01:00 fonts
drwxr-xr-x 2 root root  4096 Aug 20 01:00 images
-rw-r--r-- 1 root root 33534 Aug 30 03:42 matplotlibrc
drwxr-xr-x 3 root root  4096 Aug 20 01:00 sample_data
drwxr-xr-x 2 root root  4096 Aug 20 01:00 stylelib
(venv) [root@VM_0_3_centos mpl-data]# vi matplotlibrc 

3.1 修改font.family

在matplotlibrc中,找到font.family ,将注释打开。

3.2 修改font.sans-serif

在font.sans-serif中新增项目:simhei

font.family         : sans-serif
#
#
#
font.sans-serif     : simhei,  WenQuanYi Zen Hei Mono, 123, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

4. 删除缓存

rm -rf ~/.cache/matplotlib/*

5.代码调用片段

#导入中文字体,避免显示乱码
import pylab as mpl    


#
#
#

    mpl.rcParams['font.sans-serif'] = ['simhei']
    mpl.rcParams['font.family'] = 'sans-serif'
    mpl.rcParams['axes.unicode_minus'] = False

6.重启你的python应用

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值