如何在Python中利用matplotlib绘制饼状图

本篇文章为大家展示了如何在Python中利用matplotlib绘制饼状图,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

python主要应用领域有哪些

1、云计算,典型应用OpenStack。2、WEB前端开发,众多大型网站均为Python开发。3.人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python。4、系统运维工程项目,自动化运维的标配就是python+Django/flask。5、金融理财分析,量化交易,金融分析。6、大数据分析。

一 代码

import numpy as np
import matplotlib.pyplot as plt
#The slices will be ordered and plotted counter-clockwise.
labels ='Frogs','Hogs','Dogs','Logs'
sizes =[15,30,45,10]
colors =['yellowgreen','gold','#FF0000','lightcoral']
#使饼状图中第2片和第4片裂开
explode =(0,0.1,0,0.1)
fig = plt.figure()
ax = fig.gca()
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90,
radius=0.25, center=(0,0), frame=True)
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90,
radius=0.25, center=(1,1), frame=True)
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90,
radius=0.25, center=(0,1), frame=True)
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=90,
radius=0.25, center=(1,0), frame=True)
#设置坐标轴刻度
ax.set_xticks([0,1])
ax.set_yticks([0,1])
#设置坐标轴刻度上显示的标签
ax.set_xticklabels(["Sunny","Cloudy"])
ax.set_yticklabels(["Dry","Rainy"])
#设置坐标轴跨度
ax.set_xlim((-0.5,1.5))
ax.set_ylim((-0.5,1.5))
#设置纵横比相等
ax.set_aspect('equal')
plt.show()

二 运行结果

如何在Python中利用matplotlib绘制饼状图

上述内容就是如何在Python中利用matplotlib绘制饼状图,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。