饼图英文学名为Sector Graph,又名Pie Graph。经常使用于统计学模块。html
画饼图用到的方法为:matplotlib.pyplot.
pie
( )python
#!/usr/bin/env python #!-*-coding:utf-8 -*- #!@Author : Biyoulin #!@Time : 2018/9/4 10:45 import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 labels = ['娱乐','育儿','饮食','房贷','交通','其它'] sizes = [2,5,12,70,2,9] explode = (0,0,0,0.1,0,0) plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=150) plt.title("饼图示例-8月份家庭支出") plt.show()
上面的图形为椭圆形,可加入如下一条命令将之显示为长宽相等的饼图。api
plt.axis('equal') #该行代码使饼图长宽相等
def pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, hold=None, data=None)
pie 函数参数详解,英文原版请参见:matplotlib官网pie函数: 数组
x :(每一块)的比例,若是sum(x) > 1会使用sum(x)归一化; labels :(每一块)饼图外侧显示的说明文字; explode :(每一块)离开中心距离; startangle :起始绘制角度,默认图是从x轴正方向逆时针画起,如设定=90则从y轴正方向画起; shadow :在饼图下面画一个阴影。默认值:False,即不画阴影; labeldistance :label标记的绘制位置,相对于半径的比例,默认值为1.1, 如<1则绘制在饼图内侧; autopct :控制饼图内百分比设置,可使用format字符串或者format function '%1.1f'指小数点先后位数(没有用空格补齐); pctdistance :相似于labeldistance,指定autopct的位置刻度,默认值为0.6; radius :控制饼图半径,默认值为1;
counterclock :指定指针方向;布尔值,可选参数,默认为:True,即逆时针。将值改成False便可改成顺时针。
wedgeprops :字典类型,可选参数,默认值:None。参数字典传递给wedge对象用来画一个饼图。例如:wedgeprops={'linewidth':3}设置wedge线宽为3。 textprops :设置标签(labels)和比例文字的格式;字典类型,可选参数,默认值为:None。传递给text对象的字典参数。 center :浮点类型的列表,可选参数,默认值:(0,0)。图标中心位置。 frame :布尔类型,可选参数,默认值:False。若是是true,绘制带有表的轴框架。 rotatelabels :布尔类型,可选参数,默认为:False。若是为True,旋转每一个label到指定的角度。
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 labels = 'A','B','C','D' sizes = [10,10,10,70] plt.pie(sizes,labels=labels) plt.title("饼图详解示例") plt.text(1,-1.2,'By:Biyoulin') plt.show()
sizes = [10,10,20,60]
labels = 'A','B','C','Change'
explode = (0,0,0.1,0) #将第三块分离出来
colors = ['r','g','y','b'] #自定义颜色列表 plt.pie(sizes,explode=explode,labels=labels,colors=colors)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,shadow=True) # 添加阴影
'%1.1f':指小数点后保留一位有效数值;框架
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f',shadow=True)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%',shadow=True) #保留两位小数点,增长百分号(%);ide
plt.pie(sizes,explode=explode,labels=labels, colors=colors,autopct='%1.2f%%',shadow=True,startangle=30)
plt.pie(sizes,explode=explode,labels=labels,
colors=colors,autopct='%1.2f%%',shadow=True,startangle=30,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors, autopct='%1.2f%%',shadow=True,labeldistance=0.8,startangle=30,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', shadow=True,labeldistance=0.8,startangle=30,radius=1.3,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', pctdistance=0.4,shadow=True,labeldistance=0.8,startangle=30,radius=1.3,counterclock=False)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', pctdistance=0.4,shadow=True,labeldistance=0.8,startangle=30,radius=1.3, counterclock=False,textprops={'fontsize':20,'color':'black'})
plt.axis('equal')
plt.legend(loc="upper right",fontsize=10,bbox_to_anchor=(1.1,1.05),borderaxespad=0.3) # loc = 'upper right' 位于右上角 # bbox_to_anchor=[0.5, 0.5] # 外边距 上边 右边 # ncol=2 分两列 # borderaxespad = 0.3图例的内边距
plt.savefig("C:\\饼图02.png",dpi=200,bbox_inches='tight')
1 #!/usr/bin/env python 2 #!-*-coding:utf-8 -*- 3 #!@Author : Biyoulin 4 #!@Time : 2018/9/4 16:43 5 6 import matplotlib.pyplot as plt 7 plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 8 9 # labels = 'A','B','C','D' 10 labels = 'A','B','C','Change' 11 # sizes = [10,10,10,70] 12 sizes = [10,10,20,60] 13 explode = (0,0,0.1,0) 14 colors = ['r','g','y','b'] 15 16 plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%', 17 pctdistance=0.4,shadow=True,labeldistance=0.8,startangle=30,radius=1.3, 18 counterclock=False,textprops={'fontsize':20,'color':'black'}) 19 20 plt.title("饼图详解示例") 21 plt.text(1,-1.2,'By:Biyoulin') 22 plt.axis('equal') 23 plt.legend(loc="upper right",fontsize=10,bbox_to_anchor=(1.1,1.05),borderaxespad=0.3) 24 # loc = 'upper right' 位于右上角 25 # bbox_to_anchor=[0.5, 0.5] # 外边距 上边 右边 26 # ncol=2 分两列 27 # borderaxespad = 0.3图例的内边距 28 29 plt.savefig("C:\\饼图02.png",dpi=200,bbox_inches='tight') 30 plt.show()
做者:biyoulin函数
出处:http://www.cnblogs.com/biyoulin/spa
版权声明:本文版权归做者全部,欢迎转载,但未经做者赞成必须保留此段声明,且在文章页面明显位置给出原文链接,不然保留追究法律责任的权利。 3d