Python数据分析:手把手教你用Pandas生成可视化图表的教程


Posted in Python onDecember 15, 2018

大家都知道,Matplotlib 是众多 Python 可视化包的鼻祖,也是Python最常用的标准可视化库,其功能非常强大,同时也非常复杂,想要搞明白并非易事。但自从Python进入3.0时代以后,pandas的使用变得更加普及,它的身影经常见于市场分析、爬虫、金融分析以及科学计算中。

作为数据分析工具的集大成者,pandas作者曾说,pandas中的可视化功能比plt更加简便和功能强大。实际上,如果是对图表细节有极高要求,那么建议大家使用matplotlib通过底层图表模块进行编码。当然,我们大部分人在工作中是不会有这样变态的要求的,所以一句import pandas as pd就足够应付全部的可视化工作了。

下面,我们总结一下PD库的一些使用方法和入门技巧。

一、线型图

对于pandas的内置数据类型,Series 和 DataFrame 都有一个用于生成各类 图表 的 plot 方法。 默认情况下, 它们所生成的是线型图。其实Series和DataFrame上的这个功能只是使用matplotlib库的plot()方法的简单包装实现。参考以下示例代码 -

import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.randn(10,4),index=pd.date_range('2018/12/18',
 periods=10), columns=list('ABCD'))
 
df.plot()

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

如果索引由日期组成,则调用gct().autofmt_xdate()来格式化x轴,如上图所示。

我们可以使用x和y关键字绘制一列与另一列。

s = Series( np. random. randn( 10). cumsum(), index= np. arange( 0, 100, 10))
s. plot()

Python数据分析:手把手教你用Pandas生成可视化图表的教程

pandas 的大部分绘图方法都有 一个 可选的ax参数, 它可以是一个 matplotlib 的 subplot 对象。 这使你能够在网格 布局 中 更为灵活地处理 subplot 的位置。 DataFrame的plot 方法会在 一个 subplot 中为各列绘制 一条 线, 并自动创建图例( 如图所示):

df = DataFrame( np. random. randn( 10, 4). cumsum( 0), ...: columns=[' A', 'B', 'C', 'D'], index= np. arange( 0, 100, 10)) 
 
df. plot()

Python数据分析:手把手教你用Pandas生成可视化图表的教程

二、柱状图

在生成线型图的代码中加上 kind=' bar'( 垂直柱状图) 或 kind=' barh'( 水平柱状图) 即可生成柱状图。 这时,Series 和 DataFrame 的索引将会被用 作 X( bar) 或 (barh)刻度:

In [59]: fig, axes = plt. subplots( 2, 1) 
 
In [60]: data = Series( np. random. rand( 16), index= list(' abcdefghijklmnop')) 
 
In [61]: data. plot( kind=' bar', ax= axes[ 0], color=' k', alpha= 0. 7) 
 
Out[ 61]: < matplotlib. axes. AxesSubplot at 0x4ee7750> 
 
In [62]: data. plot( kind=' barh', ax= axes[ 1], color=' k', alpha= 0.

对于 DataFrame, 柱状 图 会 将 每一 行的 值 分为 一组, 如图 8- 16 所示:

In [63]: df = DataFrame( np. random. rand( 6, 4), ...: index=[' one', 'two', 'three', 'four', 'five', 'six'], ...: columns= pd. Index([' A', 'B', 'C', 'D'], name=' Genus')) 
 
In [64]: df 
 
Out[ 64]: 
 
Genus 
 
   A   B   C   D 
one 0. 301686 0. 156333 0. 371943 0. 270731 
two 0. 750589 0. 525587 0. 689429 0. 358974 
three 0. 381504 0. 667707 0. 473772 0. 632528 
four 0. 942408 0. 180186 0. 708284 0. 641783 
five 0. 840278 0. 909589 0. 010041 0. 653207 
six 0. 062854 0. 589813 0. 811318 0. 060217 
 
In [65]: df. plot( kind=' bar')

Python数据分析:手把手教你用Pandas生成可视化图表的教程

三、条形图

现在通过创建一个条形图来看看条形图是什么。条形图可以通过以下方式来创建 -

import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
df.plot.bar()

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

要生成一个堆积条形图,通过指定:pass stacked=True -

import pandas as pd
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
df.plot.bar(stacked=True)

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

要获得水平条形图,使用barh()方法 -

import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
 
df.plot.barh(stacked=True)

四、直方图

可以使用plot.hist()方法绘制直方图。我们可以指定bins的数量值。

import pandas as pd
import numpy as np
 
df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
 
df.plot.hist(bins=20)

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

要为每列绘制不同的直方图,请使用以下代码 -

import pandas as pd
import numpy as np
 
df=pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
 
df.hist(bins=20)

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

五、箱型图

Boxplot可以绘制调用Series.box.plot()和DataFrame.box.plot()或DataFrame.boxplot()来可视化每列中值的分布。

例如,这里是一个箱形图,表示对[0,1)上的统一随机变量的10次观察的五次试验。

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

六、块型图

可以使用Series.plot.area()或DataFrame.plot.area()方法创建区域图形。

import pandas as pd
import numpy as np
 
df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area()

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

七、散点图

可以使用DataFrame.plot.scatter()方法创建散点图。

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b')

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

八、饼状图

饼状图可以使用DataFrame.plot.pie()方法创建。

import pandas as pd
import numpy as np
 
df = pd.DataFrame(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], columns=['x'])
df.plot.pie(subplots=True)

执行上面示例代码,得到以下结果 -

Python数据分析:手把手教你用Pandas生成可视化图表的教程

以上这篇Python数据分析:手把手教你用Pandas生成可视化图表的教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的lambda匿名函数的简单介绍
Apr 25 Python
python使用arp欺骗伪造网关的方法
Apr 24 Python
Python更新数据库脚本两种方法及对比介绍
Jul 27 Python
python队列通信:rabbitMQ的使用(实例讲解)
Dec 22 Python
Flask框架URL管理操作示例【基于@app.route】
Jul 23 Python
pandas 空的dataframe 插入列名的示例
Oct 30 Python
Python 获取div标签中的文字实例
Dec 20 Python
Django Form 实时从数据库中获取数据的操作方法
Jul 25 Python
python几种常用功能实现代码实例
Dec 25 Python
Python实现银行账户资金交易管理系统
Jan 03 Python
在keras里面实现计算f1-score的代码
Jun 15 Python
Python实现小黑屋游戏的完整实例
Jan 06 Python
浅谈python 导入模块和解决文件句柄找不到问题
Dec 15 #Python
对python当中不在本路径的py文件的引用详解
Dec 15 #Python
对python3 中方法各种参数和返回值详解
Dec 15 #Python
对python中的argv和argc使用详解
Dec 15 #Python
Python输出\u编码将其转换成中文的实例
Dec 15 #Python
对python:print打印时加u的含义详解
Dec 15 #Python
Python 最大概率法进行汉语切分的方法
Dec 14 #Python
You might like
phpize的深入理解
2013/06/03 PHP
PHP安全的URL字符串base64编码和解码
2014/06/19 PHP
WordPress中访客登陆实现邮件提醒的PHP脚本实例分享
2015/12/14 PHP
PHP生成随机数的方法总结
2018/03/01 PHP
自己动手开发jQuery插件教程
2011/08/25 Javascript
jquery随机展示头像代码
2011/12/21 Javascript
基于Jquery制作图片文字排版预览效果附源码下载
2015/11/18 Javascript
jQuery实现textarea自动增长宽高的方法
2015/12/18 Javascript
jQuery CSS3相结合实现时钟插件
2016/01/08 Javascript
jQuery页面刷新(局部、全部)问题分析
2016/01/09 Javascript
JSP防止网页刷新重复提交数据的几种方法
2016/11/19 Javascript
Ajax验证用户名或昵称是否已被注册
2017/04/05 Javascript
vue项目引入字体.ttf的方法
2018/09/28 Javascript
Vue+Element UI+Lumen实现通用表格分页功能
2019/02/02 Javascript
layui自定义工具栏的方法
2019/09/19 Javascript
详解webpack的文件监听实现(热更新)
2020/09/11 Javascript
[03:55]TI9战队采访——TNC Predator
2019/08/22 DOTA
Python抓取京东图书评论数据
2014/08/31 Python
零基础写python爬虫之爬虫的定义及URL构成
2014/11/04 Python
Python简单获取网卡名称及其IP地址的方法【基于psutil模块】
2018/05/24 Python
python 对给定可迭代集合统计出现频率,并排序的方法
2018/10/18 Python
解决python xlrd无法读取excel文件的问题
2018/12/25 Python
opencv与numpy的图像基本操作
2019/03/08 Python
pandas的to_datetime时间转换使用及学习心得
2019/08/11 Python
Django单元测试中Fixtures的使用方法
2020/02/26 Python
Python 添加文件注释和函数注释操作
2020/08/09 Python
详解Open Folder as PyCharm Project怎么添加的方法
2020/12/29 Python
CSS3制作Dropdown下拉菜单的方法
2015/07/18 HTML / CSS
HTML5实现视频弹幕功能
2019/08/09 HTML / CSS
Yves Rocher伊夫·黎雪美国官网:法国始创植物美肌1959
2019/01/09 全球购物
行政助理工作职责范本
2014/03/04 职场文书
爱心捐款倡议书范文
2014/05/12 职场文书
白岩松演讲
2014/05/21 职场文书
学校食品安全实施方案
2014/06/14 职场文书
干部作风建设年活动剖析材料
2014/10/23 职场文书
springboot利用redis、Redisson处理并发问题的操作
2021/06/18 Java/Android