星期日, 十一月 30, 2014
星期四, 十月 16, 2014
pandas包绘图函数初步
python中绘图有很多包来支持,但总是觉得没有ggplot2有用。尽管如此,在探索数据时还是需要画些图来观察数据。尝试了很多方式,感觉比较方便的还是利用pandas包的附带绘图函数,可以做一些初步的探索性画图。它本身还是对matplotlib包的一个封装。下面的代码是画一些基本的图形,如线图,直方图,条形图,散点图。散点图映射点的color时要注意,不能直接把字符串进行映射,需要用scatter函数,并且需要用数值来映射到颜色。
In [6]:
%pylab inline
import pandas as pd
df= pd.read_csv('iris.csv')
df.head()
Populating the interactive namespace from numpy and matplotlib
Out[6]:
| Sepal_Length | Sepal_Width | Petal_Length | Petal_Width | Species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
In [8]:
df.Sepal_Length.hist(by=df['Species'])
Out[8]:
array([[, ], [ , ]], dtype=object)
In [9]:
df.ix[:,:4].plot(kind='line')
Out[9]:
In [10]:
df1= df.groupby('Species').mean()
df1.plot(kind='bar', stacked=True )
Out[10]:
In [12]:
df.ix[:,:4].plot(kind='scatter',x='Sepal_Length',y='Sepal_Width',by=df['Species'])
Out[12]:
In [13]:
scatter(df.Petal_Length,df.Petal_Width, c=tile([1,2,3],50))
Out[13]:
In []:
订阅:
博文 (Atom)