# 使用matplotlib绘制饼图
import numpy as np
import matplotlib.pyplot as plt
# 设置全局字体
plt.rcParams['font.sans-serif'] = ['SimHei']
# 解决‘-’表现为方块的问题
plt.rcParams['axes.unicode_minus'] = False
data = {
'南京':(60, '#7199cf'),
'上海':(45, '#4fc4aa'),
'北京':(120, '#ffff10'),
}
# 设置绘图对象的大小
fig = plt.figure(figsize=(8,8))
cities = data.keys()
values = [x[0] for x in data.values()]
colors = [x[1] for x in data.values()]
ax1 = fig.add_subplot(111)
ax1.set_title('饼图')
labels = ['{}:{}'.format(city, value) for city, value in zip(cities,values)]
# 设置饼图的凸出显示
explode = [0, 0.1, 0 ]
# 画饼状图, 并且指定标签和对应的颜色
# 指定阴影效果
ax1.pie(values, labels = labels, colors=colors, explode=explode, shadow=True)
plt.savefig('pie.jpg')
plt.show()