Pandas绘图
Pandas的绘图方法封装了Matplotlib的pyplot方法,可以提供简单的绘图功能,对于DataFrame来说,.plot是一种将所有列及其标签进行绘制的简便方法
不常用,实际应用中,一般仍使用Matplotlib绘图
Jupyter notebook中如不显示Pandas绘制图像,解决方法:
- 载入
import Matplotlib.pyplot as plt
,Pandas绘图代码最后加plt.show()
- 或者直接载入IPython魔术命令
%matplotlib inline
,或%pylab inline
(不推荐)(非IPython的py文档载入from pylab import *
)
import numpy as np
import pandas as pd
%matplotlib inline
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.head()
2000-01-01 1.005784
2000-01-02 1.643257
2000-01-03 -1.071704
2000-01-04 0.242069
2000-01-05 -0.136696
Freq: D, dtype: float64
ts.plot()
ts_cumsum01 = ts.cumsum() # cumsum 累加
ts_cumsum01.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x220c8281390>
在DataFrame中,plot()可以绘制所有带有标签的列
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,columns=['A', 'B', 'C', 'D'])
df.head()
A | B | C | D | |
---|---|---|---|---|
2000-01-01 | 1.126518 | 0.543304 | 0.275398 | 0.484449 |
2000-01-02 | 0.338547 | -0.585352 | -0.910767 | -1.470676 |
2000-01-03 | -1.738527 | 1.137119 | -0.886466 | 0.913649 |
2000-01-04 | -0.335878 | -1.697271 | 1.406224 | -0.101550 |
2000-01-05 | 0.609466 | 1.164434 | -0.452121 | 0.690371 |
df.plot()
df_cumsum = df.cumsum()
df_cumsum.head()
A | B | C | D | |
---|---|---|---|---|
2000-01-01 | 1.126518 | 0.543304 | 0.275398 | 0.484449 |
2000-01-02 | 1.465065 | -0.042048 | -0.635369 | -0.986227 |
2000-01-03 | -0.273462 | 1.095071 | -1.521835 | -0.072578 |
2000-01-04 | -0.609340 | -0.602201 | -0.115611 | -0.174128 |
2000-01-05 | 0.000126 | 0.562233 | -0.567733 | 0.516243 |
df_cumsum.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x220c945b7b8>