---
title: "mapplotlib绘制饼状图"
author: "cakin24"
date: "2017-08-01T21:33:51Z"
tags: []
categories: ["编程语言"]
slug: "mapplotlib"
draft: false
lastmod: "2017-08-01T21:33:51Z"
id: 2387964
url: "https://cakin24.iteye.com/blog/2387964"
---

**一 代码**

01. `import numpy as np`
02. `import matplotlib.pyplot as plt`
03. `#The slices will be ordered and plotted counter-clockwise.`
04. `labels ='Frogs','Hogs','Dogs','Logs'`
05. `sizes =[15,30,45,10]`
06. `colors =['yellowgreen','gold','#FF0000','lightcoral']`
07. `#使饼状图中第2片和第4片裂开`
08. `explode =(0,0.1,0,0.1)`
09. `fig = plt.figure()`
10. `ax = fig.gca()`
11. `ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,`
12. `autopct='%1.1f%%', shadow=True, startangle=90,`
13. `radius=0.25, center=(0,0), frame=True)`
14. `ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,`
15. `autopct='%1.1f%%', shadow=True, startangle=90,`
16. `radius=0.25, center=(1,1), frame=True)`
17. `ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,`
18. `autopct='%1.1f%%', shadow=True, startangle=90,`
19. `radius=0.25, center=(0,1), frame=True)`
20. `ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,`
21. `autopct='%1.1f%%', shadow=True, startangle=90,`
22. `radius=0.25, center=(1,0), frame=True)`
23. `#设置坐标轴刻度`
24. `ax.set_xticks([0,1])`
25. `ax.set_yticks([0,1])`
26. `#设置坐标轴刻度上显示的标签`
27. `ax.set_xticklabels(["Sunny","Cloudy"])`
28. `ax.set_yticklabels(["Dry","Rainy"])`
29. `#设置坐标轴跨度`
30. `ax.set_xlim((-0.5,1.5))`
31. `ax.set_ylim((-0.5,1.5))`
32. `#设置纵横比相等`
33. `ax.set_aspect('equal')`
34. `plt.show()`

**二 运行结果**

![](http://dl2.iteye.com/upload/attachment/0126/3609/c6060be1-6923-35e0-9aac-426f408ed903.png)