python 画图–饼图
python Matplotlib 系列教程(六)——绘制饼图
matplotlib.pyplot.pie
pie(x,参数)
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
beijing = [17,17,23,43]
shanghai = ['19%','4%','23%','54%']
guangzhou = ['53%','25%','13%','9%']
shenzhen = ['41%','22%','20%','17%']
label = ['2-3 years','3-4 years','4-5 years','5+ years']
color = ['red','green','yellow','purple']
indic = []
#我们将数据最大的突出显示
for value in beijing:
if value == max(beijing):
indic.append(0.1)
else:
indic.append(0)
plt.pie(
beijing,
labels=label,
colors=color,
startangle=90,
shadow=True,
explode=tuple(indic),#tuple方法用于将列表转化为元组
autopct='%1.1f%%'#是数字1,不是l
)
plt.title(u'饼图示例——统计北京程序员工龄', FontProperties=font)
plt.show()
x:每一块的比例,如果sum(x)大于 1 ,则会进行归一化
labels:设置圆圈外围标签
color:各比例的显示颜色
shadow:是否显示阴影
autopct:显示各比例数值
explode:分割,里面的数据的一个列表,0表示不分割,数值表示分割多少。
explode=[0,0.2,0,0.1,0]