打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用Pandas作图
关于Pandas的基本使用介绍,请查看另一篇博文:Python中的结构化数据分析利器-Pandas简介
推荐使用ipython的pylab模式,如果要在ipython notebook中嵌入图片,则还需要指定pylab=inline。
ipython --pylab ##ipython的pylab模式ipython notebook --pylab=inline ##notebook的inline模式import pandas as pd
基本画图命令
Pandas通过整合matplotlib的相关功能实现了基于DataFrame的一些 作图功能。下面的数据是每年美国男女出生数据:
url = 'http://s3.amazonaws.com/assets.datacamp.com/course/dasi/present.txt'present = pd.read_table(url, sep=' ')present.shape
(63, 3)
present.columns
Index([u'year', u'boys', u'girls'], dtype='object')
可以看到这个数据集共有63条记录,共有三个字段:Year,boys,girls。为了简化计算将year作为索引。
present_year = present.set_index('year')
plot是画图的最主要方法,Series和DataFrame都有plot方法。
可以这样看一下男生出生比例的趋势图:
present_year['boys'].plot()plt.legend(loc='best')
<matplotlib.legend.Legend at 0x10b9c7610>
这是Series上的plot方法,通过DataFrame的plot方法,你可以将男生和女生出生数量的趋势图画在一起。
present_year.plot()
<matplotlib.axes.AxesSubplot at 0x108ce4910>
present_year.girls.plot(color='g')present_year.boys.plot(color='b')plt.legend(loc='best')
<matplotlib.legend.Legend at 0x10999e510>
可以看到DataFrame提供plot方法与在多个Series调用多次plot方法的效果是一致。
present_year[:10].plot(kind='bar')
<matplotlib.axes.AxesSubplot at 0x10ab31390>
plot默认生成是曲线图,你可以通过kind参数生成其他的图形,可选的值为:line, bar, barh, kde, density, scatter。
present_year[:10].plot(kind='bar')
<matplotlib.axes.AxesSubplot at 0x10bb35890>
present_year[:10].plot(kind='barh')
<matplotlib.axes.AxesSubplot at 0x10eb01890>
如果你需要累积的柱状图,则只需要指定stacked=True。
present_year[:10].plot(kind='bar', stacked=True)
<matplotlib.axes.AxesSubplot at 0x10bbdb3d0>
制作相对的累积柱状图,需要一点小技巧。
首先需要计算每一行的汇总值,可以在DataFrame上直接调用sum方法,参数为1,表示计算行的汇总。默认为0,表示计算列的汇总。
present_year.sum(1)[:5]
year1940 23603991941 25134271942 28089961943 29368601944 2794800dtype: int64
有了每一行的汇总值之后,再用每个元素除以对应行的汇总值就可以得出需要的数据。这里可以使用DataFrame的div函数,同样要指定axis的值为0。
present_year.div(present_year.sum(1),axis=0)[:10].plot(kind='barh', stacked=True)
<matplotlib.axes.AxesSubplot at 0x113223290>
散点图和相关
plot也可以画出散点图。使用kind='scatter', x和y指定x轴和y轴使用的字段。
present_year.plot(x='boys', y='girls', kind='scatter')
<matplotlib.axes.AxesSubplot at 0x1141c9810>
再来载入一下鸢尾花数据。
url_2 = 'https://raw.github.com/pydata/pandas/master/pandas/tests/data/iris.csv'iris = pd.read_csv(url_2)iris.head(5)
SepalLengthSepalWidthPetalLengthPetalWidthName
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
5 rows × 5 columns
iris.corr()
SepalLengthSepalWidthPetalLengthPetalWidth
SepalLength1.000000-0.1093690.8717540.817954
SepalWidth-0.1093691.000000-0.420516-0.356544
PetalLength0.871754-0.4205161.0000000.962757
PetalWidth0.817954-0.3565440.9627571.000000
4 rows × 4 columns
from pandas.tools.plotting import scatter_matrixscatter_matrix(iris, alpha=0.2, figsize=(6, 6), diagonal='kde')
array([[<matplotlib.axes.AxesSubplot object at 0x1141e5290>, <matplotlib.axes.AxesSubplot object at 0x114313610>, <matplotlib.axes.AxesSubplot object at 0x11433fbd0>, <matplotlib.axes.AxesSubplot object at 0x114328e10>], [<matplotlib.axes.AxesSubplot object at 0x11411f350>, <matplotlib.axes.AxesSubplot object at 0x114198690>, <matplotlib.axes.AxesSubplot object at 0x114181b90>, <matplotlib.axes.AxesSubplot object at 0x11436eb90>], [<matplotlib.axes.AxesSubplot object at 0x11438ced0>, <matplotlib.axes.AxesSubplot object at 0x114378310>, <matplotlib.axes.AxesSubplot object at 0x1143e34d0>, <matplotlib.axes.AxesSubplot object at 0x114d0a810>], [<matplotlib.axes.AxesSubplot object at 0x1143ecd50>, <matplotlib.axes.AxesSubplot object at 0x114d40e90>, <matplotlib.axes.AxesSubplot object at 0x114d63210>, <matplotlib.axes.AxesSubplot object at 0x114d4a2d0>]], dtype=object)
箱图
DataFrame提供了boxplot方法可以用来画箱图。
iris.boxplot()
{'boxes': [<matplotlib.lines.Line2D at 0x1141439d0>, <matplotlib.lines.Line2D at 0x11416c1d0>, <matplotlib.lines.Line2D at 0x1141559d0>, <matplotlib.lines.Line2D at 0x11414b210>], 'caps': [<matplotlib.lines.Line2D at 0x11416af90>, <matplotlib.lines.Line2D at 0x1141434d0>, <matplotlib.lines.Line2D at 0x114172790>, <matplotlib.lines.Line2D at 0x114172c90>, <matplotlib.lines.Line2D at 0x114153f90>, <matplotlib.lines.Line2D at 0x1141554d0>, <matplotlib.lines.Line2D at 0x11414f7d0>, <matplotlib.lines.Line2D at 0x11414fcd0>], 'fliers': [<matplotlib.lines.Line2D at 0x114145410>, <matplotlib.lines.Line2D at 0x114145b50>, <matplotlib.lines.Line2D at 0x11416cbd0>, <matplotlib.lines.Line2D at 0x1141530d0>, <matplotlib.lines.Line2D at 0x114151410>, <matplotlib.lines.Line2D at 0x114151b90>, <matplotlib.lines.Line2D at 0x11414bc10>, <matplotlib.lines.Line2D at 0x1141743d0>], 'medians': [<matplotlib.lines.Line2D at 0x114143ed0>, <matplotlib.lines.Line2D at 0x11416c6d0>, <matplotlib.lines.Line2D at 0x114155ed0>, <matplotlib.lines.Line2D at 0x11414b710>], 'whiskers': [<matplotlib.lines.Line2D at 0x11416a7d0>, <matplotlib.lines.Line2D at 0x11416aa10>, <matplotlib.lines.Line2D at 0x114172050>, <matplotlib.lines.Line2D at 0x114172290>, <matplotlib.lines.Line2D at 0x114153590>, <matplotlib.lines.Line2D at 0x114153a90>, <matplotlib.lines.Line2D at 0x11414f090>, <matplotlib.lines.Line2D at 0x11414f2d0>]}
通过by参数可以计算不同分组情况下,各个字段的箱图。
iris.boxplot(by='Name', figsize=(8, 8))
array([[<matplotlib.axes.AxesSubplot object at 0x120dd8f50>, <matplotlib.axes.AxesSubplot object at 0x1218d3410>], [<matplotlib.axes.AxesSubplot object at 0x1218f47d0>, <matplotlib.axes.AxesSubplot object at 0x1218de490>]], dtype=object)
直方图和概率密度分布
iris.ix[:,:-1].hist()iris.plot(kind='kde')
<matplotlib.axes.AxesSubplot at 0x117263890>
多变量的可视化
Radviz
from pandas.tools.plotting import radvizradviz(iris, 'Name')
<matplotlib.axes.AxesSubplot at 0x11412e550>
Andrews Curves
from pandas.tools.plotting import andrews_curvesandrews_curves(iris, 'Name')
<matplotlib.axes.AxesSubplot at 0x1218e2d50>
Parallel Coordinates
from pandas.tools.plotting import parallel_coordinatesparallel_coordinates(iris, 'Name')
<matplotlib.axes.AxesSubplot at 0x1224b36d0>
你也可以查看本文的ipython notebook版本:http://nbviewer.ipython.org/gist/cloga/9171281
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
matplotlib实现一页多图
Matplotlib: Python plotting
利用Python进行数据分析 绘图和可视化(八)
数据可视化,还在使用Matplotlib?是时候让Plotly表演真正的技术了...
小象学院学习---数据分析常用图标的绘制
涨姿势,Python 画图竟然还能这么整
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服