博客已经搬家到“捕获完成”:
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 |
仿宋_GB2312 | FangSong_GB2312 |
楷体_GB2312 | KaiTi_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