python pandas dataframe绘图

有个DataFrame数据如下:

In [158]: data
Out[158]:
                 A         B         C
20100315  0.000000  0.000000  0.000000
20100316  0.005900  0.005770  0.006509
20100317  0.026345  0.027776  0.028105
20100318  0.023450  0.027573  0.026160
20100319  0.032334  0.035372  0.034182
20100322  0.034249  0.037088  0.036837
20100323  0.026219  0.030485  0.028611
20100324  0.025663  0.032470  0.028946
20100325  0.011923  0.018423  0.014330
20100326  0.025779  0.032858  0.028437
20100329  0.052822  0.053019  0.053625
20100330  0.053975  0.054229  0.056054
20100331  0.046467  0.049928  0.049767
20100401  0.059008  0.063859  0.063519
20100402  0.063189  0.068782  0.068052
20100406  0.061864  0.069658  0.067406

想画出A、B、C三条曲线,并且横坐标以日期显示。首先将index转换成日期格式,代码如下:

import datetime

dts = [datetime.datetime.strptime(str(t), '%Y%m%d') for t in data.index]
data.index = dts

data变为:

In [162]: data
Out[162]:
                   A         B         C
2010-03-15  0.000000  0.000000  0.000000
2010-03-16  0.005900  0.005770  0.006509
2010-03-17  0.026345  0.027776  0.028105
2010-03-18  0.023450  0.027573  0.026160
2010-03-19  0.032334  0.035372  0.034182
2010-03-22  0.034249  0.037088  0.036837
2010-03-23  0.026219  0.030485  0.028611
2010-03-24  0.025663  0.032470  0.028946
2010-03-25  0.011923  0.018423  0.014330
2010-03-26  0.025779  0.032858  0.028437
2010-03-29  0.052822  0.053019  0.053625
2010-03-30  0.053975  0.054229  0.056054

然后就可直接绘图,代码如下:

import matplotlib.pyplot as plt

data.plot()
plt.grid(True)
plt.show()

最后绘图效果如下:

猜你喜欢

转载自blog.csdn.net/bodybo/article/details/88036814
今日推荐