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