Python可视化——饼图【学习官方文档】

浏览: 255

前言

官方文档是很好的学习资料,我一直深信不疑。

              ——王大伟

照猫画虎

在matplotlib官方文档的example中,我发现了很多漂亮的图

巧笑倩兮,美目盼兮


我们打开一个看看:

http://matplotlib.org/examples/pie_and_polar_charts/pie_demo_features.html


官方代码如下:

"""
===============
Basic pie chart
===============

Demo of a basic pie chart plus a few additional features.

In addition to the basic pie chart, this demo shows a few optional features:

* slice labels
* auto-labeling the percentage
* offsetting a slice with "explode"
* drop-shadow
* custom start angle

Note about the custom start angle:

The default ``startangle`` is 0, which would start the "Frogs" slice on the
positive x-axis. This example sets ``startangle = 90`` such that everything is
rotated counter-clockwise by 90 degrees, and the frog slice starts on the
positive y-axis.
"""
import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.

plt.show()

运行一下:

Clipboard Image.png

发现matplotlib画的图    还是挺好看的!

我们来分析一下她美在哪里?是眼睛、眉毛、还是?

鬓若刀裁,眉如墨画,面如桃瓣,目若秋波


其实  ,官方文档都告诉我们啦,哈哈哈哈

Clipboard Image.png


今天被宝强哥的新闻刷屏了

宝强哥即官方,那宋喆呢?你懂的


思绪收回。。。


刚才代码里有一段注释:

"""
===============
Basic pie chart
===============

Demo of a basic pie chart plus a few additional features.

In addition to the basic pie chart, this demo shows a few optional features:

* slice labels
* auto-labeling the percentage
* offsetting a slice with "explode"
* drop-shadow
* custom start angle

Note about the custom start angle:

The default ``startangle`` is 0, which would start the "Frogs" slice on the
positive x-axis. This example sets ``startangle = 90`` such that everything is
rotated counter-clockwise by 90 degrees, and the frog slice starts on the
positive y-axis.
"""

意思如下:

除了基本的饼图,此演示还展示了一些可选功能:

  *每块饼打上标签

  *自动计算标记百分比

  *用“爆炸”偏移出一片

  *添加阴影

  *定制起始角度


我们一个个看:


(1)每块饼打上标签

这个很好理解

Clipboard Image.png

每块饼都是有标签的,

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)

把labels=labels去掉

完整代码:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspectratio ensures that pie is drawn as a circle.

plt.show()

Clipboard Image.png

这下就没标签了!


(2)自动计算标记百分比

我们看官方代码:

sizes = [15, 30, 45, 10]

这部分数字加起来是100,正好对应了图中的百分比

思考:现实数据可能是个数,不是算好的百分比能直接绘图么?

答案是可以的!

例如:我们改一下代码这部分为:

sizes = [15, 3.333333, 74, 10.002]

完整代码如下:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 3.333333, 74, 10.002]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspectratio ensures that pie is drawn as a circle.

plt.show()

Clipboard Image.png

结果是自动算出了百分比,不信你可以自己算算试试哦~


(3)用“爆炸”偏移出一片

我们看出这饼就像pizza一样,被切出了一片

Clipboard Image.png


说实话  我饿了。。。

然后一块不够吃啊,我切两块行不行?

看看官方代码!

explode = (0, 0.1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

这里有个0.1很特殊,我们改成1试试

explode = (0, 1, 0, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

完整代码如下:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspectratio ensures that pie is drawn as a circle.

plt.show()

Clipboard Image.png

原来这个数字是控制切出的pizza偏移的距离啊!(假装恍然大悟)


不过,我要切的是两片啊!

宝宝饿,宝宝要吃两片才行

相信你已经会啦~

explode = (0, 0.3, 0.5, 0)  # only "explode" the 2nd slice (i.e. 'Hogs')

完整代码如下:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.3, 0.5, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspectratio ensures that pie is drawn as a circle.

plt.show()

Clipboard Image.png

切出两块,偏移还不一样呢~


(4)添加阴影

像爬虫一样定位:

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)

把shadow改为False试试

完整代码:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=False, startangle=90)
ax1.axis('equal') # Equal aspectratio ensures that pie is drawn as a circle.

plt.show()

Clipboard Image.png

这下就没阴影咯~


(5)定制起始角度

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)

这里 startangle=90 起始角度是90度,那0度是什么样的呢?

ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=0)

完整代码:

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered and plotted counter-clockwise:
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, autopct='%1.1f%%',
shadow=True, startangle=0)
ax1.axis('equal') # Equal aspectratio ensures that pie is drawn as a circle.

plt.show()

Clipboard Image.png


对比发现,0度是x轴正向,90度是y轴正向

猜想:是逆时针的方向,那120度试试?

Clipboard Image.png

果然是这样!


举一反三

更多参数功能等你发现:

?ax1.pie

Clipboard Image.png

Clipboard Image.png


学习方法你掌握了么?

这里还有一堆美丽的她等你探索呢~

来啊,快活啊

猛戳以下链接:

http://matplotlib.org/examples/index.html


文中如有错误请留言评论,感谢~

推荐 1
本文由 ID王大伟 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册