欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ubuntu系统下matplotlib中文乱码问题

程序员文章站 2022-03-24 15:28:42
...

在ubuntu 下利用matplotlib 绘图的时候,图像上中文显示乱码

  1. 下载中文字体simhei.ttf, 网址为http://fontzone.net/download/simhei
  2. 搜索 matplotlib 字体的安装位置
locate -b '\mpl-data'

会得到 这个路径/usr/share/matplotlib/mpl-data下面有fonts/ttf这个目录,进入这个目录,把刚才下载的simhei.ttf 字体复制到这个目录***意权限和归属是否与其它字体一致,我的是归于root用户的,所以用root 用户复制过来。
3. 删除当前用户matplotlib 的缓冲文件(如果没有直接进入第四步)

cd ~/.cache/matplotlib
rm -rf *.*
# test.py

import itchat, time
import matplotlib.pyplot as plot

rName = 'Group_name'


def chatProportion(rName):
    need_user = []

    itchat.auto_login(True)
    male = female = other = 0
    # rName = rName
    roomSum = 0
    chatRooms = itchat.search_chatrooms(name=rName)
    print(chatRooms)
    if chatRooms is None:
        print("no this:" + rName)
    else:
        chatRoom = itchat.update_chatroom(chatRooms[0]['UserName'], detailedMember=True)
        index = 0
        mem_list = chatRoom['MemberList']
        roomSum = len(mem_list)
        for friend in mem_list:
            dis = friend['DisplayName']
            nick = friend['NickName']
            sex = friend['Sex']
            city = friend['City']

            if sex == 1:
                male += 1
            elif sex == 2:
                female += 1
                if city in ['东城',
                            '西城',
                            '海淀',
                            '朝阳',
                            '丰台',
                            '门头沟',
                            '石景山',
                            '房山',
                            '通州',
                            '顺义',
                            '昌平',
                            '大兴',
                            '怀柔',
                            '平谷',
                            '延庆',
                            '密云',
                            'Dongcheng',
                            'Xicheng',
                            'Haidian',
                            'Zhaoyang',
                            'Fengtai',
                            'Mentougou',
                            'Shijingshan',
                            'Fangshan',
                            'Tongzhou',
                            'Shunyi',
                            'Changping',
                            'Daxing',
                            'Huairou',
                            'Pinggu',
                            'Yanqing',
                            'Miyun']:
                    need_user.append((index, dis, nick, sex, city))
                print(index, dis, nick, sex, city)
            else:
                other += 1
            index += 1

    labels = ['男:' + str(male), '女' + str(female), '其他' + str(other)]
    sizes = [male, female, other]
    colors = ['green', 'red', 'gray']
    # 几个分量向外偏移
    explode = (0.1, 0.1, 0)
    plot.pie(sizes, explode, labels, colors, '%2.0f%%')
    plot.axis('equal')
    plot.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))

    plot.rcParams[u'font.sans-serif'] = ['simhei']
    plot.rcParams['axes.unicode_minus'] = False

    plot.title("群名:" + str(rName) + "[总人数:" + str(roomSum) + "]\n" + str("(男女比例分析-流年master出品)"))
    plot.grid()
    plot.show()
    print(need_user)

4.代码中调整字体
其中#coding:utf-8 说明文件编码格式
mpl.rcParams[u’font.sans-serif’] = [‘simhei’] 用simhei 字体显示中文
mpl.rcParams[‘axes.unicode_minus’] = False 这个用来正常显示负号
plt.title(u’标题’)这里的u 最好不要少