【matplotlib 】中文显示-负号显示(一个统计微信好友性别,饼图显示的例子)

博客已经搬家到“捕获完成”:

https://www.v2python.com

 

<span style="color:#4f4f4f">一、每次编写代码时进行参数设置</span>
#coding:utf-8
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
#有中文出现的情况,需要u'内容'
labels = u'男性好友', u'女性好友', u'未填性别'

下面是Windows的字体对应名称,Follow your heart, 各种换!

黑体SimHei
微软雅黑Microsoft YaHei
微软正黑体Microsoft JhengHei
新宋体NSimSun
新细明体PMingLiU
细明体MingLiU
标楷体DFKai-SB
仿宋FangSong
楷体KaiTi
仿宋_GB2312FangSong_GB2312
楷体_GB2312KaiTi_GB2312

 

二、坐标轴的负号正常显示

#去掉了注释符号,同时,改为False
axes.unicode_minus  : False

#coding:utf-8
import matplotlib.pyplot as plt
plt.plot((1,2,3),(4,3,-1))
plt.xlabel(u'横坐标')
plt.ylabel(u'纵坐标')
plt.show()

三、微信好友性别统计、饼图显示、图片信息发送到手机。

 
# -*- coding: UTF-8 -*-
import itchat
'''
微信好友性别比例
'''

# 先登录
itchat.login()

# 获取好友列表
friends = itchat.get_friends(update=True)[0:]

# 初始化计数器,有男有女,当然,有些人是不填的
male = female = other = 0

# 遍历这个列表,列表里第一位是自己,所以从"自己"之后开始计算
# 1表示男性,2女性
for i in friends[1:]:
    sex = i["Sex"]
    if sex == 1:
        male += 1
    elif sex == 2:
        female += 1
    else:
        other += 1

# 总数算上,好计算比例啊~
total = len(friends[1:])


# 好了,打印结果
print ("男性:%.2f%%" % (float(male) / total * 100))
print ("女性:%.2f%%" % (float(female) / total * 100))
print ("未填:%.2f%%" % (float(other) / total * 100))


import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
#有中文出现的情况,需要u'内容'

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = u'男性', u'女性', u'未填'
sizes = [male,female,other]
explode = (0, 0.1, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
        shadow=True, startangle=90)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.



# 保存图片 并发送到手机
plt.savefig("wechat_fri_sexual.png")

# 并发送到手机
itchat.send_image("wechat_fri_sexual.png", 'filehelper')

itchat.send_msg(str("男性:%.0f 位" % male),'filehelper')
itchat.send_msg(str("女性:%.0f 位" % female),'filehelper')
itchat.send_msg(str("未填:%.0f 位" % other),'filehelper')


# 注意展示的要放到最后,否则会出现保存为空的图片
plt.show()



参考资料:

Matplotlib输出中文显示问题
https://my.oschina.net/u/1180306/blog/279818

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值