后浪云Python教程:python饼状图的介绍及绘制

说明
1、用于表示不同分类的比例,通过弧度大小比较各种分类。
2、pie函数可以绘制饼图,饼图主要用于表现比例。只需输入比例数据即可。
语法
plt.pie(x, labels=,autopct=,colors)
实例
#导入模块
import matplotlib.pyplot as plt
import numpy as np
#准备男、女的人数及比例
man=71351
woman=68187
man_perc=man/(woman+man)
woman_perc=woman/(woman+man)
#添加名称
labels=['男','女']
#添加颜色
colors=['blue','red']
#绘制饼状图 pie
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
# labels 名称 colors:颜色,explode=分裂 autopct显示百分比
paches,texts,autotexts=plt.pie([man_perc,woman_perc],labels=labels,colors=colors,explode=(0,0.05),autopct='%0.1f%%')
#设置饼状图中的字体颜色
for text in autotexts:
text.set_color('white')
#设置字体大小
for text in texts+autotexts:
text.set_fontsize(20)
plt.show()以上就是python饼状图的介绍及绘制,希望对大家有所帮助。更多Python学习指路:后浪云python教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
版权声明:
作者:后浪云
链接:https://idc.net/help/175953/
文章版权归作者所有,未经允许请勿转载。
THE END
