在这个答案使用DateFormatter后,我尝试使用pandas 0.15.0和matplotlib 1.4.2绘制时间序列并用x年标记其x轴:
import datetime as dt
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas.io.data as pdio
import scipy as sp
t1 = dt.datetime(1960, 1, 1)
t2 = dt.datetime(2014, 6, 1)
data = pdio.DataReader("GS10", "fred", t1, t2).resample("Q", how=sp.mean)
fig, ax1 = plt.subplots()
ax1.plot(data.index, data.GS10)
ax1.set_xlabel("Year")
ax1.set_ylabel("Rate (%)")
ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter("%Y"))
fig.suptitle("10-yr Treasury Rate", fontsize=14)
fig.savefig('test.eps')
Run Code Online (Sandbox Code Playgroud)
最后一行引发错误:OverflowError: Python int too large to convert to C long
使用此回溯:
C:\ Anaconda3\lib\site-packages\IPython\core\formatters.py:239:FormatterWarning:image/png格式化程序中的异常:Python int太大而无法转换为C long FormatterWarning,Traceback(最近一次调用最后一次):
文件"",第1行,在runfile中('D:/username/latex_template/new_pandas_example.py',wdir ='D:/ username/latex_template')
文件"C:\ Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py",第580行,在runfile execfile(filename,namespace)中 …