绘制饼状图

pie函数可以绘制饼状图,饼图主要是用来呈现比例的。只要传入比例数据即可

import matplotlib.pyplot as plt

man=71351
woman=68187
man_prec=man/(man+woman)
woman_prec=woman/(man+woman)
labels=['男','女']
colors=['blue','red']
plt.rcParams['font.sans-serif']=['SimHei']
#lables表示图标的名称,color表示颜色,explode表示分裂,autopct表示百分比
#plt.pie([man_prec,woman_prec],labels=labels,colors=colors,explode=(0,0.05),autopct='%.1f%%')

#plt.pie()本身返回值为:return slices, texts, autotexts;
"""
slices:   表示饼状图的每个小部分对象
texts:    表示饼状图的标签文本内容
autotexts:表示饼状图的每个小部分的文本内容
"""
slices, texts, autotexts=plt.pie([man_prec,woman_prec],labels=labels,colors=colors,explode=(0,0.05),autopct='%.1f%%')

#设置饼状图里的字体颜色
for text in autotexts:      #其实就是遍历饼状图的每个小部分
    text.set_color('white')

#设置字体大小:饼状图的标签文本+饼状图的每个小部分的文本
for text in texts+autotexts:
    text.set_fontsize(20)

plt.show()
饼状图.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容