Pandas绘图

来源:互联网 发布:随心安软件 编辑:程序博客网 时间:2024/06/03 05:16

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 npimport pandas as pd%matplotlib inlinets = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))ts.head()
2000-01-01    1.0057842000-01-02    1.6432572000-01-03   -1.0717042000-01-04    0.2420692000-01-05   -0.136696Freq: D, dtype: float64
ts.plot()

ts

ts_cumsum01 = ts.cumsum() # cumsum 累加ts_cumsum01.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x220c8281390>

这里写图片描述
png

在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.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>

df_cumsum