python使用matplotlib绘制饼图
制作人:全心全意
示例代码如下:
#!/usr/bin/python
#-*- coding: utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
def bingtu(name,count):
#启用中文
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
#设置图大小
plt.figure(figsize=(8, 5), dpi=100)
# 通过pie
#每个扇形图离圆心的距离
explode = (0.05, 0, 0,0,0,0,0,0,0,0)
color = ('b','r','magenta','y','lime','m','yellow','pink','c','g')
#count为次数,name为次数对应名称
patches,l_text,p_text =plt.pie(count, labels=name, autopct='%1.2f%%', colors=color,explode=explode)
#设置扇形外文字的大小
for t in l_text:
t.set_size(12)
#设置百分比文本的大小
for t in p_text:
t.set_size(9)
# 指定显示的pie是正圆
plt.axis('equal')
#对图例进行设置
plt.legend(loc='lower right',bbox_to_anchor=(1.1, -0.1),markerscale=0.5)
#对标题进行设置
plt.title("数据量来源占比排名",fontdict={'weight':'normal','size': 13},y=-0.1)
#plt.show()
return plt
name = ['微信', '今日头条', '懂车帝', '新浪微博', '搜狐新闻', '一点资讯', '看点快报', '腾讯网', '网易号', '其他']
cunt = [187, 139, 126, 88, 75, 28, 25, 23, 22, 387]
bingtu(name,cunt)
