代码来源:Python数据分析与挖掘实战
#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' #定义标签
sizes = [15, 30, 45, 10] #定义每一块的比例
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] #每一块的颜色
explode = (0, 0.1, 0, 0) #突出显示,这里仅仅突出显示第二快(即'Hogs'),0.1表示间距
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%.1f%%', shadow=True, startangle=90) #autopct=.1f%表示标签显示在饼图内,显示为带有一位小数的百分数; startangle不为空表示从x轴逆时针方向旋转饼图的角度,
plt.axis('equal') #显示为圆(避免比例压缩位椭圆)
plt.show()