matplotlib与pyecharts:pie饼图

一、代码和效果图

#coding:utf-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
#用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['simhei']
plt.rcParams['font.family']='sans-serif'
#用来正常显示负号
#mpl.rcParams['axes.unicode_minus'] = False
#mpl.rcParams['font.size']=10
from matplotlib.font_manager import FontProperties
myfont = FontProperties(fname='/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc')
fig = plt.figure()
ax1 = fig.add_subplot(111)    
df = pd.read_excel('/home/soft/person.xls')
edu=df.loc[:,['num','educational']].groupby('educational').count()
#labels = map(lambda x:x.encode('utf8'),list(edu.index))
expl=[] 
for x in edu.index:
    if x==u'本科' or x==u'大专':
        expl.append(0.2)
    else:
        expl.append(0)
attches,texts,autotexts=ax1.pie(edu.values,explode=expl,autopct='%1.1f%%',shadow=False,labels=edu.index,startangle=90,pctdistance=0.8,labeldistance=1.1)
ax1.axis('equal')  
ax1.set_title(u'学历比例图',bbox={'facecolor':'0.8', 'pad':5})
for t in texts:
    t.set_size('smaller')  
autotexts[0].set_color('w')
autotexts[0].set_size(20)
ax1.legend(ncol=3,loc='lower right')
plt.show()


二、参数说明

patches,l_text,p_text = plt.pie(sizes,explode=explode,labels=labels,colors=colors,
                                labeldistance = 1.1,autopct = '%3.1f%%',shadow = False,
                                startangle = 90,pctdistance = 0.6)

#labeldistance,文本的位置离远点有多远,1.1指1.1倍半径的位置
#autopct,圆里面的文本格式,%3.1f%%表示小数有三位,整数有一位的浮点数
#shadow,饼是否有阴影
#startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
#pctdistance,百分比的text离圆心的距离
#patches, l_texts, p_texts,为了得到饼图的返回值,p_texts饼图内部文本的,l_texts饼图外label的文本
#explode = (0.05,0,0),数值的大小是分割出来的与其他两块的间隙
#改变文本的大小
#方法是把每一个text遍历。调用set_size方法设置它的属性
for t in l_text:
    t.set_size(30)
for t in p_text:
    t.set_size(20)
# 设置x,y轴刻度一致,这样饼图才能是圆的
plt.axis('equal')
plt.legend()
plt.show()


三、pyecharts饼图和换图

#coding:utf-8
import numpy as np
import pandas as pd
from pyecharts import Pie
df = pd.read_excel('/home/soft/person.xls')
edu=df.loc[:,['num','educational']].groupby('educational').count()
attr = edu.index
value = []
for i in range(len(edu)):
    value.append(edu.loc[list(edu.index)[i],'num'])

pie = Pie('学历分布图',title_pos='center')
#pie.add('饼图',attr,value,is_label_show=True,legend_top='bottom')
pie.add('环图',attr,value,is_label_show=True,radius=[30,75],legend_orient='vertical',legend_pos='left')
pie.show_config()
pie.render('/home/soft/pie.html')

环图


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值